android学习之RecyclerView

android学习之RecyclerView

首先在xml文件中添加RecyclerView


而recyclerview中的每个item视图需要另外写一个xml文件,rv_list_first.xml




    

    



recycleview有适配器,适配器是向recycleview传输数据,pick_rvAdapter.java文件如下:

//注意这里的类pick_rvAdapter需要与文件名一样
public class pick_rvAdapter extends RecyclerView.Adapter {
    private Context context;
    private List datas;

//下面这个pickActivity.java中setAdapter()函数中用到
    public pick_rvAdapter(Context context, List datas) {
        this.context = context;
        this.datas = datas;
    }
    
//重写onCreateViewHolder函数,目的是渲染recyclerview(每个item)视图
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View contentView= LayoutInflater.from(context).inflate(R.layout.rv_list_first,parent,false);//填充视图的意思,确定用rv_list_first.xml作为视图
        MyViewHolder viewHolder=new MyViewHolder(contentView);
        return viewHolder;
    }
    
//重写onBindViewHolder函数,绑定视图,position代表每个视图的位置
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.imageView.setImageResource(datas.get(position));
        holder.textViewv.setText("分类"+position);
        holder.itemView.setTag(position);
        //给item添加点击事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v ) {
                int mposition = position;
                switch (mposition){
                    case 0 :
                        /*
                           这里写点击每个分类的逻辑
                         */
                        Toast.makeText(context,  "我点击了图片" + 0,Toast.LENGTH_SHORT).show();
                        break;
                    case 1 :
                        Toast.makeText(context,  "我点击了图片" + 1,Toast.LENGTH_SHORT).show();
                        break;
                    case 2 :
                        Toast.makeText(context,  "我点击了图片" + 2,Toast.LENGTH_SHORT).show();
                        break;
                    case 3 :
                        Toast.makeText(context,  "我点击了图片" + 3,Toast.LENGTH_SHORT).show();
                        break;
                }

            }
        });
    }

//返回item数量
    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

//建立一个viewholder类吧,照着网上代码写的
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        public TextView textViewv;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.symbol_one_image);
            textViewv = (TextView) itemView.findViewById(R.id.symbol_one_text);
            //itemView.setOnClickListener(this);
        }
    }
}

接着在activity代码中与适配器“连接”,pickActivity.java代码:

public class pickActivity extends AppCompatActivity {
    private RecyclerView recyclerView_one,recyclerView_two,recyclerView_three;
    private List datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick);
  
        recyclerView_one = (RecyclerView) findViewById(R.id.recyclerView_one); //当时是因为三个recyclerview
        recyclerView_two = (RecyclerView) findViewById(R.id.recyclerView_two);
        recyclerView_three = (RecyclerView) findViewById(R.id.recyclerView_three);
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        List datas = new ArrayList<>();
        for (int i = 1; i < 8; i++) {
            Resources res = getResources();
            datas.add(res.getIdentifier("test" + i, "drawable", getPackageName()));
        }
        /**
         *用来确定每一个item如何进行排列摆放
         * LinearLayoutManager 相当于ListView的效果
         GridLayoutManager相当于GridView的效果
         StaggeredGridLayoutManager 瀑布流
         */
        
        recyclerView_one.setLayoutManager(new LinearLayoutManager(this, StaggeredGridLayoutManager.HORIZONTAL,false));
        recyclerView_two.setLayoutManager(new LinearLayoutManager(this, StaggeredGridLayoutManager.HORIZONTAL,false));
        recyclerView_three.setLayoutManager(new LinearLayoutManager(this, StaggeredGridLayoutManager.HORIZONTAL,false));

//实现每个item的位置
        recyclerView_one.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

                outRect.left = 20;
                outRect.right = 30;
                outRect.top = 10;
                outRect.top = 10;
            }
        });

        recyclerView_two.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

                outRect.left = 20;
                outRect.right = 30;
                outRect.top = 10;
                outRect.top = 10;
            }
        });

        recyclerView_three.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

                outRect.left = 20;
                outRect.right = 30;
                outRect.top = 10;
                outRect.top = 10;
            }
        });

//将适配器的数据传给recyclerview
        recyclerView_one.setAdapter(new pick_rvAdapter(this, datas));
        recyclerView_two.setAdapter(new pick_rvAdapter(this, datas));
        recyclerView_three.setAdapter(new pick_rvAdapter(this, datas));
    }
}

你可能感兴趣的:(android学习之RecyclerView)