美文网首页
最基础的RecyclerView适配器写法

最基础的RecyclerView适配器写法

作者: 爱我O就直说 | 来源:发表于2020-04-27 11:03 被阅读0次

public class FirstFragmentAdapter extends RecyclerView.Adapter<FirstFragmentAdapter.ViewHolder> {
    private Context mContext;
    private ArrayList<String> list;

    private FirstFragmentAdapter() {
    }

    public FirstFragmentAdapter(Context context, ArrayList<String> strList) {
        mContext = context;
        list = strList;
    }

    @NonNull
    @Override
    public FirstFragmentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(new TextView(mContext));
    }

    @Override
    public void onBindViewHolder(@NonNull FirstFragmentAdapter.ViewHolder holder, int position) {
        holder.setValue(list.get(position));
    }

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


    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = (TextView) itemView;
        }

        public void setValue(String string) {
            tv.setText(string);
        }
    }
}

相关文章

网友评论

      本文标题:最基础的RecyclerView适配器写法

      本文链接:https://www.haomeiwen.com/subject/llqywhtx.html