RecyclerView 基础使用

使用方法
1、添加引用
implementation 'com.android.support:recyclerview-v7:27.1.1'
2、创建布局文件




   

3 、创建item



    

    

4、创建Adapter
RecyclerView的apater 和listview、Gradview 的adapter有些不同 ,Holider 需要继承RecyclerView.ViewHolder,adapter 需要继承RecyclerView.Adapter
主要方法:
创建ViewHolder

    @NonNull
    @Override
    public MyHolider onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      //ViewHolider 和布局文件的绑定
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_name, null);
        MyHolider myHolider = new MyHolider(view);
        return myHolider;
    }

设置item显示的内容

 @Override
    public void onBindViewHolder(@NonNull MyHolider holder, int position) {
        holder.name.setText(mList.get(position));
    }

item 的数量

 @Override
   public int getItemCount() {
       return mList.size();
   }

ViewHolider

 public class MyHolider extends RecyclerView.ViewHolder {
  //此处可以添加自己显示的属性
        private TextView name;

        public MyHolider(View itemView) {
            super(itemView);
//..........
            name = itemView.findViewById(R.id.name);
        }
    }

adapter 全部代码

public class MyAdapter extends RecyclerView.Adapter {

    private List mList;
    private Context mContext;

    public MyAdapter(List mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public MyHolider onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_name, null);
        MyHolider myHolider = new MyHolider(view);
        return myHolider;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolider holder, int position) {
        holder.name.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class MyHolider extends RecyclerView.ViewHolder {

        private TextView name;

        public MyHolider(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
        }
    }

}


Activity 代码

public class RecyclerActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        mRecyclerView = findViewById(R.id.recyclerview);
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this,5));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new MyAdapter(getData(),this));
    }


    private List getData() {
        List list = new ArrayList<>();
        for (int i = 1; i < 30; i++) {
            list.add(" 张三  " + i + "  号");
        }
        return list;
    }
}

正常listView效果
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
效果

image.png

GridView 效果

后面的数字是一行显示几个
 mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));

效果


image.png

你可能感兴趣的:(RecyclerView 基础使用)