android recyclerview adaper封装

关于Recyclerview,是比较好用的一个控件,代替了listview和gridview,灵活性比较强。基础的就不说了,我要讲的是如何封装一个基类的recyclerview adaper

代码:

/**  * @author:guobiaoWang on 2015/12/24  */ public abstract class BaseRecylerAdapter extends RecyclerView.Adapter<BaseRecylerHolder> implements View.OnClickListener {
    public List<Object> mList;
    public Activity mContext;
    public RecyclerViewItemClickListener mListener;

    public BaseRecylerAdapter(Activity context, Object list) {
        this.mList = new ArrayList<>();
        //noinspection unchecked,unchecked,unchecked  this.mList.addAll((List<Object>) list);
        this.mContext = context;
    }

    @Override
    public BaseRecylerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = getView(parent,viewType);
        return createViewHolder(view,viewType);
    }


    @Override
    public void onBindViewHolder(BaseRecylerHolder baseHolder, int position) {

    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    public void setOnItemClickListner(RecyclerViewItemClickListener listner) {
        this.mListener = listner;
    }

    public abstract BaseRecylerHolder createViewHolder(View view,int viewType);

    public abstract View getView(ViewGroup parent,int viewType);
}
 
 
 
 
item回调接口:
public interface RecyclerViewItemClickListener {
    void onItemClick(View view, int position);
}

 
 
基础Holder类:
/**  * @author:guobiaoWang on 2015/12/24  */ public class BaseRecylerHolder extends RecyclerView.ViewHolder {
    public BaseRecylerHolder(View itemView) {
        super(itemView);
    }
}

下面是调用:大家别看实体类和布局名称,看如何实现就行了,只有一个item布局时:
public class ChatHeadAdapter extends BaseRecylerAdapter {
    public ChatHeadAdapter(Activity context, Object list) {
        super(context, list);
    }

    @Override
    public BaseRecylerHolder createViewHolder(View view, int viewType) {
        return new ChatHeadHolder(view);
    }

    @Override
    public View getView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(mContext).inflate(R.layout.chat_head_item, null);
    }

    @Override
    public void onBindViewHolder(BaseRecylerHolder baseHolder, final int position) {
        ChatHeadHolder holder = (ChatHeadHolder) baseHolder;
        ChatScope chatScope = (ChatScope) mList.get(position);
        holder.name.setText(chatScope.getName());
        if (chatScope.getIcon() instanceof String)
            holder.head.setImageURI(Uri.parse(chatScope.getIcon().toString()));
        else  holder.head.setBackgroundResource((int) chatScope.getIcon());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    private class ChatHeadHolder extends BaseRecylerHolder {
        private SimpleDraweeView head;
        private TextView name;

        public ChatHeadHolder(View itemView) {
            super(itemView);
            head = (SimpleDraweeView) itemView.findViewById(R.id.chat_head_item_image);
            name = (TextView) itemView.findViewById(R.id.chat_head_item_name);
        }
    }
}

 
 
多个item布局时候:
 
 
package chat.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.blk.activity.R;
import com.blk.activity.base.BaseRecylerAdapter;
import com.blk.activity.base.BaseRecylerHolder;
import com.facebook.drawee.view.SimpleDraweeView;

import chat.model.Assistants;
import chat.model.CustomerService;

/**  * @author:guobiaoWang on 2016/4/29  */ public class ChatChooseContactAdapter extends BaseRecylerAdapter {

    public ChatChooseContactAdapter(Activity context, Object list) {
        super(context, list);
    }

    public enum ChatType {
        title, content;
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position) instanceof CustomerService) {
            return ChatType.title.ordinal();
        } else {
            return ChatType.content.ordinal();
        }
    }

    @Override
    public BaseRecylerHolder createViewHolder(View view, int viewType) {
        if (viewType == ChatType.title.ordinal()) {
            return new ContactTitleHolder(view);
        } else if (viewType == ChatType.content.ordinal()) {
            return new ContactContentHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(BaseRecylerHolder baseHolder, final int position) {
        Object object = mList.get(position);
        if (object instanceof CustomerService) {
            ContactTitleHolder holder = (ContactTitleHolder) baseHolder;
            CustomerService customerService = (CustomerService) object;
            holder.title.setText(customerService.getMerchantsName());
            holder.address.setText(customerService.getMerchantsAddress());
        } else if (object instanceof Assistants) {
            ContactContentHolder holder = (ContactContentHolder) baseHolder;
            holder.speak.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v, position);
                }
            });
            Assistants assistants = (Assistants) object;
            holder.name.setText(assistants.getNickName());
        }
    }

    @Override
    public View getView(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == ChatType.title.ordinal()) {
            view = LayoutInflater.from(mContext).inflate(R.layout.chat_choose_contact_pop_title_item, null);
        } else if (viewType == ChatType.content.ordinal()) {
            view = LayoutInflater.from(mContext).inflate(R.layout.chat_choose_contact_pop_content_item, null);
        }
        return view;
    }

    @Override
    public void onClick(View v) {

    }

    private class ContactTitleHolder extends BaseRecylerHolder {
        private TextView title;
        private TextView address;

        public ContactTitleHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.chat_choose_contact_pop_title_business);
            address = (TextView) itemView.findViewById(R.id.chat_choose_contact_pop_title_address);
        }
    }

    private class ContactContentHolder extends BaseRecylerHolder {
        private SimpleDraweeView head;
        private TextView name;
        private Button speak;

        public ContactContentHolder(View itemView) {
            super(itemView);
            head = (SimpleDraweeView) itemView.findViewById(R.id.chat_choose_contact_pop_content_head);
            name = (TextView) itemView.findViewById(R.id.chat_choose_contact_pop_content_name);
            speak = (Button) itemView.findViewById(R.id.chat_choose_contact_pop_content_speak);
        }
    }

    public Assistants getAssistants(int position) throws Exception {
        if (mList.get(position) instanceof Assistants) {
            return (Assistants) mList.get(position);
        } else {
            throw new Exception();
        }
    }
}

 
 



你可能感兴趣的:(android,Class,控件,RecyclerView)