Android学习笔记之RecyclerView

写在前面:

想要让RecyclerView向下兼容,需要在app/build.gradle中添加库:

implementation 'com.android.support:recyclerview-v7:27.1.1'

一、通过RecyclerView实现纵向滚动(像ListView那样)

1.在布局文件中引入RecyclerView控件

2.编辑RecyclerView的适配器,让其继承RecyclerView.Adapter

public class FruitRecyclerAdapter extends RecyclerView.Adapter

3.给FruitRecyclerAdapter添加数据集合,构造器,以及内部类ViewHolder(ViewHolder需要继承RecyclerView.ViewHolder)

private List fruitList;
public FruitRecyclerAdapter(List fruitList){
    this.fruitList = fruitList;
}
static class ViewHolder extends RecyclerView.ViewHolder{
    ImageView fruitImage;
    TextView fruitName;
    public ViewHolder(View itemView) {
        super(itemView);
        fruitImage = itemView.findViewById(R.id.imageView);
        fruitName = itemView.findViewById(R.id.textView);
    }
}

4.将FruitRecyclerAdapter的父类泛型指定为其内部类ViewHolder

public class FruitRecyclerAdapter extends RecyclerView.Adapter 

5.重写FruitRecyclerAdapter的三个方法

@NonNull
@Override
//重写该方法创建一个ViewHolder实例
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // 加载单条Item的布局文件,并使之在父布局中的layout属性生效,但不为这个View添加父布局
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
//重写该方法给RecyclerView子项的数据进行赋值,会在每个子项滚动到屏幕内时执行
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Fruit fruit = fruitList.get(position);
    holder.fruitName.setText(fruit.getName());
    holder.fruitImage.setImageResource(fruit.getImageId());
}

@Override
//返回RecyclerView一共有多少子项
public int getItemCount() {
    if(fruitList!=null)
        return fruitList.size();
    return 0;
}

6.最后在Activity中给RecyclerView添加上布局管理器和适配器即可

initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitRecyclerAdapter adapter = new FruitRecyclerAdapter(fruitList);
recyclerView.setAdapter(adapter);

二、通过RecyclerView实现瀑布流布局

1.注意修改R.layout.fruit_item.xml布局文件,让item的布局适用于瀑布流布局。

2.其余步骤同纵向滚动时一样(上述的1-5步),只需要在最后给RecyclerView设置布局管理器时,更改为瀑布流布局,同时指定列数和排列方式即可。

// 让RecyclerView采用瀑布流布局,第一个参数用于指定布局的列数,第二个参数用于指定布局的排列方向
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);

三、通过RecyclerView实现网格布局

同样只用在最后给RecyclerView设置布局管理器时,使用网格布局管理器即可。

//让RecyclerView采用网格布局,第一个参数为Context,第二个参数为列数
GridLayoutManager layoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);

四、RecyclerView的点击事件

1.在适配器的内部类ViewHolder中添加一个成员变量View,并在构造器中为其赋值

static class ViewHolder extends RecyclerView.ViewHolder{
    ImageView fruitImage;
    TextView fruitName;
    View fruitView;
    public ViewHolder(View itemView) {
        super(itemView);
        fruitView = itemView;
        fruitImage = itemView.findViewById(R.id.imageView);
        fruitName = itemView.findViewById(R.id.textView);
    }
}

2.在onCreateViewHolder()方法中添加点击事件监听器

final ViewHolder viewHolder = new ViewHolder(view);
viewHolder.fruitView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int positon = viewHolder.getAdapterPosition();
        Fruit fruit = fruitList.get(positon);
        Toast.makeText(v.getContext(),"You clicked view "+fruit.getName(),Toast.LENGTH_LONG).show();
    }
});

你可能感兴趣的:(Android,RecyclerView,Android)