Android 适配器(Adapter)二次封装

适配器父类
 

/**
  *版权:
  *@创建者:shuquan
  *创建时间:2020/9/23
  *模块:
  *描述:适配器父类
  */

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

    List<T> mData;//集合对象
    int mLayoutResId;//布局LayoutID
    Context context;//上下文
    public BaseAdapter(@LayoutRes int layoutResId, @Nullable List<T> data, Context context) {
        this.mData = data == null ? new ArrayList<T>() : data;
        this.context = context;
        if (layoutResId != 0) {
            this.mLayoutResId = layoutResId;
        }
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(mLayoutResId, parent, false);
        return new BaseViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, int position) {
        bindViewHolder(holder, mData.get(position), position);
    }
     @Override
     public int getItemViewType(int position) {
         return position;
     }
     @Override
     public long getItemId(int position) {
         return position;
     }

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

    public abstract void bindViewHolder(BaseViewHolder holder, T itemData, int position);

    public Context getContext() {
        return context;
    }

    /**
     * @param:
     *创建时间:2020/10/23
     *描述:获取集合对象
    */
    public List<T> getDataList() {
        return mData;
    }

    /**
     * @param: dataList 数据源
     *创建时间:2020/10/23
     *描述:设置数据
    */
    public void setDataList(List<T> dataList) {
        this.mData = dataList;
        notifyDataSetChanged();
    }

}

子类使用
 

/**
 * 版权:
 * 创建者:shuquan
 * 创建时间:2020/9/23
 * 描述:主页菜单适配器
 */
public class ItmeAdapter extends BaseAdapter<ItmeData>{
    ItmeApadterCallback callback;//点击接口回调
    public ItmeAdapter(Context context, List<ItmeData> dataList, ItmeApadterCallback callback) {
        super(R.layout.home_itme_layout, dataList, context);//设置布局和对象
        this.callback = callback;
    }

    @Override
    public void bindViewHolder(BaseViewHolder holder,final ItmeData itemData, int position) {
        holder.setText(R.id.tv_tiem,getContext().getResources().getString(itemData.tiem));
        holder.setVisible(R.id.tvp_number,itemData.number==0?false:true);
    }
}
BaseViewHolder(主要类)
 /**
  *版权:
  *@创建者:shuquan
  *创建时间:2020/10/23
  *模块
  *描述:BaseViewHolder
  */

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private final SparseArray<View> views;


    public View convertView;


    public BaseViewHolder(final View view) {
        super(view);
        this.views = new SparseArray<>();
        convertView = view;
    }


    /**
     * 返回传染ID的View
     * @param viewId
     * @return
     */
    public View getViews(@IdRes int viewId) {
        View view = getView(viewId);
        return view;
    }

    public void setText(@IdRes int viewId, CharSequence value) {
        TextView view = getView(viewId);
        view.setText(value);
    }

    public void setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
    }

    public void setVisible(@IdRes int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    @Deprecated
    public void setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
    }

    @Deprecated
    public void setOnTouchListener(@IdRes int viewId, View.OnTouchListener listener) {
        View view = getView(viewId);
        view.setOnTouchListener(listener);
    }

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

 

可以根据自己的实际需求进行封装

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值