RecyclerView的实现(小白进阶路 “单条目,单视图”)

Impl (build.gradle文件)

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

布局基础(显示视图.xml

 

条目显示(item.xml)


    
    
        

复用属性(styles.xml)

 

JAVA代码(MainActivity.java)

1.获得控件

 private RecyclerView mRecyclerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        }

    private void initView() {
        mRecyclerList =  findViewById(R.id.list);
    }

2.简单的设置一下数据

   ArrayList mList = new ArrayList<>();
        for (int i = 0; i <100 ; i++) {
            mList .add(i+"");
        }

3.RecyclerView要设置布局管理(setLayoutManager)
(1)常用类型

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL);
staggeredGridLayoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);

(2)设置

  mRecyclerList .setLayoutManager(布局管理器name);

4.RecyclerView的适配器设置

(1)基础设置

 //GoodsAdapter 临时创建的适配器类
 GoodsAdapter 适配器name = new GoodsAdapter(当前视图(this), 数据name(mList);
 mRecyclerList .setAdapter(适配器name);

(2) GoodsAdapter 中继承RecyclerView.Adapter<内容ViewHolder>

1.先将ViewHolder类实现

 class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }

2.将继承方法实现

GoodsAdapter extends RecyclerView.Adapter {
	//要用到的属性
    private Context context;
    private ArrayList mAlist;
    public GoodsAdapter(Context context, ArrayList mAlist) {
        this.context = context;
        this.mAlist= mAlist;
    }
   //三个最基础的方法
    @NonNull
    @Override
    public MyViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHodler myViewHodler, int i) {

    }

    @Override
    public int getItemCount() {
        return 0;
    }
 }

3.关联ViewHolder

 @NonNull
 @Override
 public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
 //viewGroup.getContext()==context
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_goods(条目视图xml),viewGroup,false);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);
        return myViewHolder;
 }

4.绑定onBindViewHolder (List数据)

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        String s = mAlist.get(i);
        //非空判断操作
        if (!TextUtils.isEmpty(s)) {
            myViewHolder.textView.setText(s);
        }else {
            myViewHolder.textView.setText("");
        }
     }

5.条目数量方法的设置(getItemCount)

    @Override
    public int getItemCount() {
        //三元运算符的判断返回参数
        return mAlist==null ? 0:mAlist.size();
    }

6.优化有参构造

 private List listData = new ArrayList<>();

 public GoodsAdapter(Context context, List mAlist) {
        this.context = context;
        if (mAlist!=null&&mAlist.size()>0) {
            this.mAlist.addAll(mAlist);
        }
    }

此时运行代码便会看到效果了

RecyclerView的实现(小白进阶路 “单条目,单视图”)_第1张图片

**只是初步实现一个特别简单的RecyclerView,勿喷 **

你可能感兴趣的:(安卓RecyclerView,RecyclerView,初级,实用优化)