最基础的RecyclerView适配器写法


public class FirstFragmentAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList list;

    private FirstFragmentAdapter() {
    }

    public FirstFragmentAdapter(Context context, ArrayList 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适配器写法)