RecyclerView 瀑布流 点击按钮添加或删除条目

来源:互联网 发布:淘宝企业店铺需要资料 编辑:程序博客网 时间:2024/05/29 08:43
 导入依赖
compile 'com.android.support:recyclerview-v7:25.3.1'//效果//主布局文件  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwei.administrator.dianshang.activity.SecondActivity">   <LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content">      <Button       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="添加"          android:onClick="add"       />      <Button          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="删除"          android:onClick="del"          />   </LinearLayout>   <android.support.v7.widget.RecyclerView       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:id="@+id/recycle_view"       ></android.support.v7.widget.RecyclerView></LinearLayout>============子布局文件=============
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tv"        android:gravity="center"        /></LinearLayout>

+++++++++++++MainActivity+++++++++++++++
private RecyclerView recyclerView;private List<String> list;private WaterAdapter waterAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_second);    //获取控件    recyclerView = (RecyclerView) findViewById(R.id.recycle_view);    //数据    list = new ArrayList<>();    for (int i=0; i<1000;i++){        list.add("这是条目"+i);    }    //布局管理器    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));     //设置适配器    waterAdapter = new WaterAdapter(SecondActivity.this,list);        recyclerView.setAdapter(waterAdapter);}//点击添加按钮public void add(View v){     list.add(2,"ggggg");    waterAdapter.notifyItemInserted(2);}//点击删除按钮public void del(View view){  list.remove(6);    waterAdapter.notifyItemRemoved(6);}
_____________________Adapter____________________
public class WaterAdapter extends RecyclerView.Adapter<WaterHolder> {    Context context;    List<String> list;    private final List<Integer> hlist;    public WaterAdapter(Context context, List<String> list) {        this.context = context;        this.list = list;        //设置随机高度        hlist = new ArrayList<>();      for(int i=0;i<list.size();i++){          hlist.add((int)(Math.random()*100+200));      }    }    @Override    public WaterHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = LayoutInflater.from(context).inflate(R.layout.water_item, parent, false);        WaterHolder holder = new WaterHolder(view);        return holder;    }    @Override    public void onBindViewHolder(WaterHolder holder, int position) {        ViewGroup.LayoutParams layoutParams = holder.tv.getLayoutParams();        layoutParams.height=hlist.get(position);        //设置背景颜色        holder.tv.setBackgroundColor(Color.rgb((int)(Math.random()*100+155),(int)(Math.random()*100+155),(int)(Math.random()*100+155)));           holder.tv.setText(list.get(position));    }    @Override    public int getItemCount() {        return list.size();    }}
++++++++++++++holder+++++++++++++++
public class WaterHolder extends RecyclerView.ViewHolder {   public TextView tv;    public WaterHolder(View itemView) {        super(itemView);        tv = (TextView) itemView.findViewById(R.id.tv);    }}



阅读全文
0 0
原创粉丝点击