zoukankan      html  css  js  c++  java
  • RecyclerAdapter封装

    import java.util.LinkedList;
    import java.util.List;
    
    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public abstract class BaseRecyclerAdapter<T> extends
            RecyclerView.Adapter<BaseRecyclerViewHolder> {
        protected Context mContext;
        protected LayoutInflater mInflater;
        protected List<T> mDatas = new LinkedList<T>();
        public OnItemClickListener<T> mOnItemClickListener;
    
        public BaseRecyclerAdapter(Context context, List<T> datas) {
            mContext = context;
            mInflater = LayoutInflater.from(context);
            if (datas != null) {
                mDatas = datas;
            }
        }
    
        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }
    
        @Override
        public int getItemCount() {
            int count = 0;
            if (mDatas.size() > 0) {
                count = mDatas.size();
            }
            return count;
        }
    
        public void addItemLast(List<T> datas) {
            mDatas.addAll(datas);
        }
    
        public void addItemTop(List<T> datas) {
            mDatas = datas;
        }
    
        public void remove(int position) {
            mDatas.remove(position);
        }
    
        public void removeAll() {
            mDatas.clear();
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public List<T> getDatas() {
            return mDatas;
        }
    
        // 点击事件接口
        public interface OnItemClickListener<T> {
            void onItemClick(View view, int position, T model);
    
            void onItemLongClick(View view, int position, T model);
        }
    
        public void setOnItemClickListener(OnItemClickListener<T> listener) {
            this.mOnItemClickListener = listener;
        }
    
        @Override
        public void onBindViewHolder(BaseRecyclerViewHolder holder, int position) {
            showViewHolder(holder,position);
        }
    
        @Override
        public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return createViewHOldeHolder(parent, viewType);
        }
    
        
        protected abstract void showViewHolder(BaseRecyclerViewHolder holder, int position);
        
        /***
         * 
         * @param parent
         * @param viewType
         * @return
         */
        protected abstract BaseRecyclerViewHolder createViewHOldeHolder(ViewGroup parent,
                int viewType);
    
    }
    View Code
    import android.support.v7.widget.RecyclerView.ViewHolder;
    import android.view.View;
    
    public abstract class BaseRecyclerViewHolder extends ViewHolder {
    
        public BaseRecyclerViewHolder(View view) {
            super(view);
        }
    
    }
    View Code

    使用方法

    /***
     * 用户列表adapter
     * 
     */
    public class UserListAdapter extends BaseRecyclerAdapter<UserDetails> {
    
        public UserListAdapter(Context context, List<UserDetails> datas) {
            super(context, datas);
        }
    
        @Override
        protected void showViewHolder(BaseRecyclerViewHolder holder, final int position) {
            final ViewHolder viewHolder=(ViewHolder)holder;
            final UserDetails model=mDatas.get(position);
            if (TextUtils.isEmpty(model.getIcon())) {
                FangDaImageLoader.getInstance().setImage("drawable://" + R.drawable.face_default, viewHolder.img_icon);
            } else {
                FangDaImageLoader.getInstance().setImage(model.getIcon(), viewHolder.img_icon);
            } 
            viewHolder.txt_message.setText(model.getIntro());         
            viewHolder.txt_username.setText(model.getUserName());
            viewHolder.btn_user_state.setSelected(true);
            viewHolder.btn_user_state.setText(mContext.getString(R.string.btn_add_text));
            viewHolder.btn_user_state.setVisibility(View.GONE);
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(viewHolder.itemView, position,model);
                    }
                });
    
                // 长点击事件
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mOnItemClickListener.onItemLongClick(viewHolder.itemView, position,model);
                        return true;
                    }
                });
    
            }  
        }
    
        @Override
        protected BaseRecyclerViewHolder createViewHOldeHolder(ViewGroup parent,
                int viewType) {
            View mView = null;
            BaseRecyclerViewHolder mViewHolder = null;
            mView = mInflater.inflate(R.layout.row_invite_msg, parent, false);
            mViewHolder = new ViewHolder(mView);
            return mViewHolder;
        }
    
        static class ViewHolder extends BaseRecyclerViewHolder {
            @Bind(R.id.img_icon)
            RoundImageView img_icon;
            @Bind(R.id.txt_message)
            CustomTextView txt_username;
            @Bind(R.id.txt_username)
            CustomTextView txt_message;
            @Bind(R.id.btn_user_state)
            CustomButton btn_user_state;
    
            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    
    }
    View Code
    adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<UserDetails>() {
    
                @Override
                public void onItemClick(View view, int position, UserDetails model) { 
                    intent=new Intent(context, PersonalSpaceActivity.class);
                    intent.putExtra("uid",model.getUid());
                    intent.putExtra("username", model.getUserName());
                    startActivity(intent);
                }
    
                @Override
                public void onItemLongClick(View view, int position,
                        UserDetails model) {
                     
                }
            }); 
    View Code
  • 相关阅读:
    财务报表分析(张新民教授)-第六章 企业报表战略分析
    zend studio 10破解/汉化
    使用jQuery动态加载js脚本文件的方法
    手机端的META你有多了解?
    js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME)
    理解CSS Clip属性及用法
    js touch触屏原理分析
    解决xp共享的批处理文件
    jQuery Tools:Web开发必备的 jQuery UI 库
    使用CSS3线性渐变实现图片闪光划过效果
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/5048382.html
Copyright © 2011-2022 走看看