初学RecyclerView

利用RecyclerView可以实现多种灵活的布局方式,使用RecyclerView有如下几个步骤:

  • 初始化

private RecyclerView textCustomMenuRV;
  • 赋值

textCustomMenuRV = (RecyclerView) findViewById(R.id.text_custom_menu);
  • 设置LayoutManager

textCustomMenuRV.setLayoutManager(new LinearLayoutManager(this));
  • 设置adapter,用于绑定数据

private TextCustomMenuAdapter textCustomMenuAdapter = new TextCustomMenuAdapter();
//使用自定义的adapter
textCustomMenuRV.setAdapter(textCustomMenuAdapter);
这里需要自定义adapter以完成更多的自定义
public class TextCustomMenuAdapter extends RecyclerView.Adapter<TextCustomMenuAdapter.Holder> {
    @Override
    public TextCustomMenuAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TextCustomMenuAdapter.Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.textcustom_menu_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(TextCustomMenuAdapter.Holder holder, int position) {

    }

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

    class Holder extends RecyclerView.ViewHolder {

        ImageView colorimg;

        public Holder(View itemView) {
            super(itemView);
        }

    }
}

对于RecyclerView, 需要一个额外的layout来定制RecyclerView内部的布局样式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容