RecyclerView+RecyclerView.Adapter+RecyclerView.ViewHolder的使用

本文详细介绍了如何使用RecyclerView组件来展示列表数据。包括自定义适配器RecycleAdapter和ViewHolder,实现列表项的加载和数据绑定。此外,还展示了如何通过GridLayoutManager布局管理器设置列表的网格显示。
摘要由CSDN通过智能技术生成

一、RecycleViewActivity 

/**
 * RecycleView+recycleView.Adapter+RecycleView.ViewHolder
 */
public class RecycleViewActivity extends AppCompatActivity {
    private List<DataModel> models;
    private RecyclerView mRecyclerView;
    private RecycleAdapter adapter;

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

    /**
     * 初始化
     */
    private void initView() {
        getData();
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_container);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        adapter = new RecycleAdapter(models);
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 构建模拟数据
     */
    private void getData() {
        models = new ArrayList<>();
        DataModel model;
        for (int i = 0; i < 20; i++) {
            model = new DataModel("列表标题" + i, "说明文字" + i);
            models.add(model);
        }
    }
}

二RecycleAdapter 

/**
 * RecyclerView.Adapter
 * 三个方法,外加一个构造方法
 * Created by Devin Chen on 2016/12/17.
 */

public class RecycleAdapter extends RecyclerView.Adapter<RecycleViewHolder> implements View.OnClickListener {
    private Context context;
    private List<DataModel> dataModels;

    public RecycleAdapter(List<DataModel> dataModels) {
        this.dataModels = dataModels;
    }

    @Override
    public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_data_model, null);
        //RecyclerView没有setOnItemClickListener监听,所以可以在这里写,也可以在ViewHolder里面写
        //但是这里不能获取position
        //所以可以在这里把view交给一个属性,在onBindViewHolder里面去设置监听
        view.setOnClickListener(this);
        return new RecycleViewHolder(context, view);
    }

    @Override
    public void onBindViewHolder(RecycleViewHolder holder, int position) {
        holder.setItem(dataModels.get(position));
        holder.refreshView();
    }

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

    @Override
    public void onClick(View v) {

    }
}

三、RecycleViewHolder 

/**
 * RecyclerView.ViewHolder
 * BaseAdapterViewHolder写法基本一样
 * Created by Devin Chen on 2016/12/17.
 */

public class RecycleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private Context context;
    private ImageView icon;
    private TextView title,desc;
    private DataModel item;

    public RecycleViewHolder(Context context,View itemView) {
        super(itemView);
        this.context=context;
        icon= (ImageView) itemView.findViewById(R.id.img_icon);
        title= (TextView) itemView.findViewById(R.id.txt_title);
        desc= (TextView) itemView.findViewById(R.id.txt_desc);
        //RecyclerView没有setOnItemClickListener监听,所以可以在这里写
        //getAdapterPosition()可以获取position
        itemView.setOnClickListener(this);
    }

    public void setItem(DataModel item) {
        this.item = item;
    }

    public void refreshView(){
        icon.setImageResource(R.mipmap.ic_launcher);
        title.setText(item.getTitle());
        desc.setText(item.getDesc());
    }

    @Override
    public void onClick(View v) {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值