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

 

posted on 2015-12-15 15:13  freexiaoyu  阅读(2561)  评论(0编辑  收藏  举报