RecyclerView.Adapter封装,多布局Adapter的使用,最简单实用的BaseRecyclerViewAdapter;只需重写一个方法,设置数据链式调用;

之前对ListView的BaseAdapter进行过封装,只需重写一个getView方法;

现在慢慢的RecyclerView成为主流,下面是RecyclerView.Adapter的封装;

https://github.com/CuiChenbo/BaseRecyclerAdapter

BaseRecyclerViewHolder类:

public class BaseRecyclerHolder extends RecyclerView.ViewHolder {
    private SparseArrayCompat mViews;
    public BaseRecyclerHolder(View itemView) {
        super(itemView);
        mViews = new SparseArrayCompat<>();
    }
    public  V getView(@IdRes int res){
        View v = mViews.get(res);
        if (v == null){
            v = itemView.findViewById(res);
            mViews.put(res,v);
        }
        return (V)v;
    }
    /**
     * 直接赋值textview
     * @param TvRes
     * @param text
     */
    public BaseRecyclerHolder setText(@IdRes int TvRes,CharSequence text){
        TextView textView = getView(TvRes);
        textView.setText(text);
        return this;
    }
}

BaseRecyclerViewAdapter类:

public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter {
    public Context context;
    public int layoutRes;
    public List items;
 
    public BaseRecyclerAdapter(Context context, @LayoutRes int layoutRes){
      this.context = context;
      this.layoutRes = layoutRes;
      items = new ArrayList();
    }
 
    public BaseRecyclerAdapter(Context context,@LayoutRes int layoutResId, @Nullable List data) {
        this.context = context;
        this.items = data == null ? new ArrayList() : data;
        if (layoutResId != 0) {
            this.layoutRes = layoutResId;
        }
    }
 
    @Override
    public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseRecyclerHolder(LayoutInflater.from(context).inflate(layoutRes,parent,false));
    }
 
    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {
        convert(holder,items ,position);
    }
 
    @Override
    public int getItemCount() {
        return items.size();
    }
 
 
    public void setNewData(List items) {
        this.items = items == null ? new ArrayList() : items;
        notifyDataSetChanged();
    }

    public List getDatas() {
       return items;
    }

    /**
     * change data
     * 改变某一位置数据
     */
    public void setData(int index, T data) {
        if (index >= this.items.size()) {
            return;
        }
        this.items.set(index , data);
        notifyItemChanged(index);
    }
 
    public void addData(@IntRange(from = 0) int position, @NonNull T data) {
        items.add(position, data);
        notifyItemInserted(position);
        compatibilityDataSizeChanged(1);
    }
    public void addData( @NonNull T data) {
        items.add(data);
        notifyItemInserted(items.size());
    }
 
 
    public void remove(int position) {
        if (position >= items.size()) return;

        items.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, items.size() - position);
    }

    public void remove(T item) {
        final int position = items.indexOf(item);
        if (-1 == position)
            return;
        remove(position);
    }

 
    private void compatibilityDataSizeChanged(int size) {
        final int dataSize = items == null ? 0 : items.size();
        if (dataSize == size) {
            notifyDataSetChanged();
        }
    }
 
    /**
     * 需要重写的方法
     * @param holder
     * @param position
     */
    public abstract void convert(BaseRecyclerHolder holder , List items, int position);
}

 

 

如何使用:MainActivity类:

 

public class MainActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private List d = Arrays.asList(
            "A","B","C","D","E","F","G"
            ,"H","I","J","K","L","M","N"
            ,"O","P","Q","R","S","T"
            ,"U","V","W","X","Y","Z");
    private RecyclerView rv ;
    private BaseRecyclerAdapter ap;
    private List datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
//        ap = new BaseRecyclerAdapter(MainActivity.this, R.layout.item_rv){
//      还可以这样用
//            @Override
//            public void convert(BaseRecyclerHolder holder, int position) {
//
//            }
//        };
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(this,3));
        ap = new Ap(this,R.layout.item_rv);
        rv.setAdapter(ap);
        ap.setData(datas);
    }

    private void initData() {
        datas = new ArrayList<>();
        for (int i = 0; i < d.size(); i++) {
            datas.add(d.get(i));
        }
    }

    class Ap extends BaseRecyclerAdapter{

        public Ap(Context c, int layoutRes) {
            super(c, layoutRes);
        }

        @Override
        public void convert(BaseRecyclerHolder holder, final int position) {
            //链式调用
              holder.setText(R.id.tv,items.get(position))
                    .setOnClickListener(R.id.tv, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ap.remove(position);
                        }
                    });
        }
    }

}

其实就只有这一个是你关注的,需要实现的Adapter:

 

class Ap extends BaseRecyclerAdapter{

    public Ap(Context c, int layoutRes) {
        super(c, layoutRes);
    }

    @Override
    public void convert(BaseRecyclerHolder holder, final int position) {
        //链式调用
          holder.setText(R.id.tv,items.get(position))
                .setOnClickListener(R.id.tv, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ap.remove(position);
                    }
                });
    }
}

是不是超简单:

这个只是Demo,下面的下载地址里面封装的更加全面;

CSDN资源地址:https://download.csdn.net/download/qq_35605213/10388770

Github地址:https://github.com/CuiChenbo/BaseRecyclerAdapter

 

附:多布局Adapter的封装

public abstract class BaseRecyclerViewAdater extends RecyclerView.Adapter {

    private List mData;
    private Context context;
    private List resLayout;
    public BaseRecyclerViewAdater(Context context , int resLayout){
        this.resLayout = new ArrayList<>();
        this.context = context;
        this.resLayout.add(resLayout);
        mData = new ArrayList<>();
    }

    //需要重写getItemViewType方法自行给布局分类
    public BaseRecyclerViewAdater(Context context , List resLayouts){
        this.context = context;
        this.resLayout = resLayouts;
        mData = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public List getmData() {
        return mData;
    }

    @NonNull
    @Override
    public BaseRecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       return new BaseRecyclerHolder(LayoutInflater.from(context).inflate(resLayout.get(viewType), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRecyclerHolder holder, int position) {
        convert(holder,position);
    }

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

    /**
     * 需要重写的方法
     * @param holder
     * @param position
     */
    public abstract void convert(BaseRecyclerHolder holder, int position);
public class RecyclerActivity extends AppCompatActivity {

    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        List layouts = new ArrayList<>();
        layouts.add(R.layout.item);
        layouts.add(R.layout.item2);
        Ap ap = new Ap(this,layouts);
        rv.setAdapter(ap);
        ap.setData(Arrays.asList("0","","0","","","","","","","","0","","","","","0","","","","0","","0","0","",""));
    }


    class Ap extends BaseRecyclerViewAdater {

        public Ap(Context context, List resLayouts) {
            super(context, resLayouts);
        }

        @Override
        public void convert(BaseRecyclerHolder holder, int position) {

        }

        @Override
        public int getItemViewType(int position) {
            if (TextUtils.isEmpty(getmData().get(position))){
                return 0;
            }else {
                return 1;
            }
        }
    }
}

 

你可能感兴趣的:(Android)