recycleView通用Adapter

R-B發表於2021-09-09

public class CreditManagerAdapter extends RecyclerView.Adapter {

    private Context mContext;

    private List datas;

    private final LayoutInflater mLayoutInflater;

    private MyItemClickListener mItemClickListener;

    public CreditManager getDataItem(int position) {

        return datas == null ? null : datas.get(position);

    }

    public CreditManagerAdapter(Context mContext, List mDatas) {

        this.mContext = mContext;

        this.datas = mDatas;

        mLayoutInflater = LayoutInflater.from(mContext);

    }

    public void update(List mDatas) {

        this.datas = mDatas;

        //this.notifyDataSetChanged();

    }

    public void notifyData(List mDatas) {

        this.datas = mDatas;

        this.notifyDataSetChanged();

    }

    public int getLayout() {

        return R.layout.creditmanager_dialog_item;

    }

    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(getLayout(), parent, false);

        RecyclerView.ViewHolder viewHolder = new ViewHolder(v, mItemClickListener);

        return viewHolder;

    }

   

    public void setOnItemClickListener(MyItemClickListener listener) {

        mItemClickListener = listener;

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    @Override

    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int p) {

        final ViewHolder mHolder = (ViewHolder) holder;

        CreditManager data = datas.get(p);

    }

    @Override

    public int getItemCount() {

        return datas == null ? 0 : datas.size();

    }

    public interface MyItemClickListener {

        public void OnClickListener(int position);

    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private MyItemClickListener mItemClickListener;

        ViewHolder(View view, MyItemClickListener mItemClickListener) {

            super(view);

            ButterKnife.bind(this, view);

            this.mItemClickListener = mItemClickListener;

            view.setOnClickListener(this);

        }

        @Override

        public void onClick(View v) {

            if (mItemClickListener != null) {

                mItemClickListener.OnClickListener(getPosition());

            }

        }

    }

}

原文連結:http://www.apkbus.com/blog-796354-68598.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4560/viewspace-2813777/,如需轉載,請註明出處,否則將追究法律責任。

相關文章