对RecyclerView的适配器的封装

2020/12/19 10:50
阅读数 36

对RecyclerView的适配器的封装

效果:
封装前代码量:

package com.example.androidtestshoping.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.androidtestshoping.R;
import com.example.androidtestshoping.bean.MicroTao;
import java.util.List;

/**
 * Created by Ivan on 15/9/30.
 */
public class MicroTaoAdapter extends RecyclerView.Adapter<MicroTaoAdapter.ViewHolder> {
   
   

    private  static int VIEW_TYPE_L=0;
    private  static int VIEW_TYPE_R=1;
    private LayoutInflater mInflater;
    private List<MicroTao> mDatas;
//    private MicroTaoListener listener;

    public MicroTaoAdapter(List<MicroTao> datas){
   
   
        mDatas = datas;
    }
//    public void setMicroTaoListener(MicroTaoListener listener){
   
   
//        this.listener=listener;
//    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
   
   

        mInflater = LayoutInflater.from(viewGroup.getContext());
        return  new ViewHolder(mInflater.inflate(R.layout.mitaorefreshlayout,viewGroup,false));

    }
    public MicroTao getData(int position){
   
   

        return mDatas.get(position);
    }

    public List<MicroTao> getDatas(){
   
   

        return  mDatas;
    }
    public void clearData(){
   
   

        mDatas.clear();
        notifyItemRangeRemoved(0,mDatas.size());
    }

    public void addData(List<MicroTao> datas){
   
   

        addData(0,datas);
    }

    public void addData(int position,List<MicroTao> datas){
   
   

        if(datas !=null && datas.size()>0) {
   
   

            mDatas.addAll(datas);
            notifyItemRangeChanged(position,4);
        }

    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
   
   

        MicroTao microTao = mDatas.get(i);
        viewHolder.textTitle.setText(microTao.getTitle());
        viewHolder.mPrice.setText(microTao.getPrice());
        viewHolder.imageView.setImageResource(microTao.getImageView());

    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
   
   

        TextView textTitle,mPrice;
        ImageView imageView;

        public ViewHolder(View itemView) {
   
   
            super(itemView);

            textTitle =  itemView.findViewById(R.id.text_title);
            mPrice=itemView.findViewById(R.id.text_price);
            imageView =  itemView.findViewById(R.id.drawee_view);

        }
    }
//    public interface MicroTaoListener{
   
   
//        void onClick(View v, int position, String title);
//    }
}

封装后:

import android.widget.TextView;
import com.example.androidtestshoping.R;
import com.example.androidtestshoping.bean.MicroTao;
import java.util.List;
public class MTAdapter extends SimpleAdapter<MicroTao> {
   
   

    public MTAdapter(Context context, List<MicroTao > datas) {
   
   
        super(context, R.layout.mitaorefreshlayout, datas);
    }
    @Override
    protected void convert(BaseViewHolder viewHolder, MicroTao microTao) {
   
   
        TextView textView = viewHolder.getTextView(R.id.text_title);
        TextView mPrice=viewHolder.getTextView(R.id.text_price);
        ImageView draweeView =  (ImageView) viewHolder.getView(R.id.drawee_view);
        textView.setText(microTao.getTitle());
        mPrice.setText(microTao.getPrice());
        draweeView.setImageResource(microTao.getImageView());
    }
}

以后需要RecyclerView适配器,就直接写一个实现类就好了,提高了代码复用率,辛苦一次,偷懒n次。

一、封装知识介绍
封装知识介绍是我引用一位博主关于“java 封装是什么?”的回答。我觉得他描述的非常好,我想了一下,我就不自己去写知识介绍了,大家可以去这篇博客去看看。详情请点击封装知识介绍
二、RecyclerView的适配器的封装

BaseAdapter的代码:

package com.example.androidtestshoping.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidtestshoping.R;
import com.example.androidtestshoping.bean.MicroTao;

import java.util.List;

public abstract class BaseAdapter<T,H extends  BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder> {
   
   

    protected final Context context;
    protected final int layoutResId;
    protected List<T> datas;

    public BaseAdapter(Context context, int layoutResId) {
   
   
        this(context, layoutResId, null);
    }
    public BaseAdapter(Context context,int layoutResId,List<T> datas){
   
   
        this.context=context;
        this.layoutResId=layoutResId;
        this.datas=datas;
    }
    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
   
   
        View view=LayoutInflater.from(parent.getContext()).inflate(layoutResId,parent,false);
        BaseViewHolder viewHolder=new BaseViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
   
   
            T item=getItem(position);
            convert((H)holder,item);
    }
    @Override
    public int getItemCount() {
   
   
        if(datas==null||datas.size()<=0)
                return 0;
        return datas.size();
    }
    public T getData(int position){
   
   

        return datas.get(position);
    }

    public List<T> getDatas(){
   
   
        return  datas;
    }

    public void addData(List<T> datas){
   
   

        addData(0,datas);
    }

    public void addData(int position,List<T> datas){
   
   
        if(datas !=null && datas.size()>0) {
   
   

            this.datas.addAll(datas);
            this.notifyItemRangeChanged(position, datas.size());
        }
    }
    public void clearData(){
   
   
        datas.clear();
        notifyItemRangeRemoved(0,datas.size());
    }
    protected  T getItem(int position){
   
   
        if(position>=datas.size())  return null;
        return datas.get(position);
    }
    protected abstract void convert(H viewHoder,T item);
}

BaseViewHolder的代码

package com.example.androidtestshoping.Adapter;

import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class BaseViewHolder  extends RecyclerView.ViewHolder {
   
   

    private SparseArray<View> views;
    public BaseViewHolder(@NonNull View itemView) {
   
   
        super(itemView);
        this.views=new SparseArray<View>();
    }
    public TextView getTextView(int viewId) {
   
   
        return retrieveView(viewId);
    }

    public Button getButton(int viewId) {
   
   
        return retrieveView(viewId);
    }
    public ImageView getImageView(int viewId) {
   
   
        return retrieveView(viewId);
    }

    public View getView(int viewId) {
   
   
        return retrieveView(viewId);
    }
    protected  <T extends View> T retrieveView(int viewId){
   
   
        View view=views.get(viewId);
        if(view==null){
   
   
            view=itemView.findViewById(viewId);
            views.put(viewId,view);
        }
        return (T)view;
    }
}

BaseAdapter的实现类SimpleAdapter的代码

package com.example.androidtestshoping.Adapter;
import android.content.Context;
import java.util.List;


public abstract class SimpleAdapter<T> extends BaseAdapter<T,BaseViewHolder> {
   
   
    public SimpleAdapter(Context context, int layoutResId) {
   
   
        super(context, layoutResId);
    }
    public SimpleAdapter(Context context, int layoutResId, List<T> datas) {
   
   
        super(context, layoutResId, datas);
    }
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部