美文网首页android 技术知识AndroidAndroid
Android 中获取 RecycleView列表Item的Ed

Android 中获取 RecycleView列表Item的Ed

作者: 追梦小乐 | 来源:发表于2018-08-21 10:06 被阅读59次

    在帮朋友做的一个小项目中,发现了这么的一个需求:要求列表中的某一项是可编辑输入的,说实话,做了差不多两年android开发,我还是第一次做列表中的item是可以输入编辑的,只能说我们的需求确实是变化莫测........不多说,继续记录一下实现过程

    1、实现思路

    在adapter中保存一个Hashmap,同时监听EditText控件的输入,然后保存相对应的值

    2、核心代码

    public class PurchaseAndStorageAdapter extends SimpleAdapter<PurorderBean> {
    
        private Context mContext;
    
        public HashMap<Integer, String> contents = new HashMap<>();
    
        public PurchaseAndStorageAdapter(Context context, List<PurorderBean> datas) {
            super(context, R.layout.adapter_purchase_and_storage_item, datas);
            mContext = context;
        }
        @Override
        protected void convert(BaseViewHolder viewHoder, PurorderBean item) {
            Log.i("purchaseAndStorage", "convert: name = " + item.getName());
            //列表的数据在这里绑定
            viewHoder.getTextView(R.id.tv_name).setText(item.getName());
            viewHoder.getTextView(R.id.tv_large).setText(item.getModel());
            viewHoder.getTextView(R.id.tv_order_num).setText(item.getTaskCount()+"");
            viewHoder.getTextView(R.id.tv_num).setText(item.getPurOrderCount()+"");
    
            EditText etNum = (EditText)viewHoder.getTextView(R.id.tv_num);
    
            etNum.addTextChangedListener(new MyTextChangedListener(viewHoder,contents));
    
    
        }
    
    
        public class MyTextChangedListener implements TextWatcher {
    
            public BaseViewHolder holder;
            public HashMap<Integer, String> contents;
    
            public MyTextChangedListener(BaseViewHolder holder,HashMap<Integer, String> contents){
                this.holder = holder;
                this.contents = contents;
            }
    
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
                if(holder != null && contents != null){
                    int adapterPosition = holder.getAdapterPosition();
                    LogUtil.d(TAG,"adapterPosition==================="+adapterPosition);
                    contents.put(adapterPosition,editable.toString());
                }
            }
        }
    }
    

    3、获取列表中我们输入的值

      HashMap<Integer, String> contents = adapter.contents;
    

    4、完整的BaseViewHolder 、BaseAdapter封装如下

    BaseViewHolder 文件
    public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
    
        private SparseArray<View> views;
    
        private BaseAdapter.OnItemClickListener mOnItemClickListener ;
    
        public BaseViewHolder(View itemView, BaseAdapter.OnItemClickListener onItemClickListener){
            super(itemView);
            itemView.setOnClickListener(this);
    
            this.mOnItemClickListener =onItemClickListener;
            this.views = new SparseArray<View>();
        }
    
        public TextView getTextView(int viewId) {
            return retrieveView(viewId);
        }
    
        public CheckBox getCheckBox(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;
        }
    
        @Override
        public void onClick(View v) {+
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v,getLayoutPosition());
            }
        }
    }
    
    
    BaseAdapter文件
    public abstract class BaseAdapter<T,H extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder>{
    
    
    
        protected static final String TAG = BaseAdapter.class.getSimpleName();
    
        protected final Context context;
    
        protected  int layoutResId;
    
        protected List<T> datas;
    
    
        private OnItemClickListener mOnItemClickListener = null;
    
    
    
        public  interface OnItemClickListener {
            void onItemClick(View view, int position);
        }
    
    
    
        public BaseAdapter(Context context, int layoutResId) {
            this(context, layoutResId, null);
        }
    
    
        public BaseAdapter(Context context, int layoutResId, List<T> datas) {
            this.datas = datas == null ? new ArrayList<T>() : datas;
            this.context = context;
            this.layoutResId = layoutResId;
        }
    
    
    
    
        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup,  int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
            BaseViewHolder vh = new BaseViewHolder(view,mOnItemClickListener);
            return vh;
        }
    
        @Override
        public void onBindViewHolder(BaseViewHolder viewHoder,  int position) {
            T item = getItem(position);
            convert((H)viewHoder, item);
        }
    
    
    
        @Override
        public int getItemCount() {
            if(datas==null || datas.size()<=0)
                return 0;
    
            return datas.size();
        }
    
    
        public T getItem(int position) {
            if (position >= datas.size()) return null;
            return datas.get(position);
        }
    
    
        public void clear(){
    //        int itemCount = datas.size();
    //        datas.clear();
    //        this.notifyItemRangeRemoved(0,itemCount);
    
            if(datas==null || datas.size()<=0)
                return;
    
            for (Iterator it=datas.iterator();it.hasNext();){
    
                T t = (T) it.next();
                int position = datas.indexOf(t);
                it.remove();
                notifyItemRemoved(position);
            }
        }
    
        /**
         * 从列表中删除某项
         * @param t
         */
        public  void removeItem(T t){
    
            int position = datas.indexOf(t);
            datas.remove(position);
            notifyItemRemoved(position);
        }
    
    
        public int getSizes(){
    
            return  datas.size();
        }
    
    
        public List<T> getDatas(){
    
            return  datas;
        }
    
    
    
    
        public void addData(List<T> datas){
    
            addData(0,datas);
        }
    
        public void addData(int position,List<T> list){
    
            if(list !=null && list.size()>0) {
    
                for (T t:list) {
                    datas.add(position, t);
                    notifyItemInserted(position);
                }
    
            }
        }
    
    
    
        public void refreshData(List<T> list){
    
            LogUtil.i(TAG,"list.size()======="+list.size());
            clear();
            if(list !=null && list.size()>0){
    
    
                int size = list.size();
                for (int i=0;i<size;i++){
                    datas.add(i,list.get(i));
                    notifyItemInserted(i);
                }
    
            }
        }
    
        public void loadMoreData(List<T> list){
    
            if(list !=null && list.size()>0){
    
                int size = list.size();
                int begin = datas.size();
                for (int i=0;i<size;i++){
                    datas.add(list.get(i));
                    notifyItemInserted(i+begin);
                }
    
            }
    
        }
    
    
    
        /**
         * Implement this method and use the helper to adapt the view to the given item.
         * @param viewHoder A fully initialized helper.
         * @param item   The item that needs to be displayed.
         */
        protected abstract void convert(H viewHoder, T item);
    
    
    
    
        public void setOnItemClickListener(OnItemClickListener listener) {
            this.mOnItemClickListener = listener;
    
        }
    
    }
    
    
    SimpleAdapter文件
    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);
        }
    
    
    }
    

    相关文章

      网友评论

      • dsx:感觉用position来做key值不太保险,会造成hashmap获取值的时候不可控。。。
      • d82dff120182:好像不走convert()这个方法哦
        d82dff120182:@追梦小乐 最近的这个和你的一样还得把EditText输入的值和图片ID对应上在传回后台:joy:
        d82dff120182:@追梦小乐 我打印的Log都没有显示呢
        追梦小乐:@小帅帥 会走的 相当于走了一遍, 然后主要是EditText的监听方法去获取改变的值

      本文标题:Android 中获取 RecycleView列表Item的Ed

      本文链接:https://www.haomeiwen.com/subject/yqrdiftx.html