一、直接上代码
1、创建CommonAdapter.java
public class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {
private List<T> mList;
private OnBindDataListener<T> onBindDataListener;
private OnMoreBindDataListener<T> onMoreBindDataListener;
public CommonAdapter(List<T> mList, OnBindDataListener<T> onBindDataListener) {
this.mList = mList;
this.onBindDataListener = onBindDataListener;
}
public CommonAdapter(List<T> mList, OnMoreBindDataListener<T> onMoreBindDataListener) {
this.mList = mList;
this.onBindDataListener = onMoreBindDataListener;
this.onMoreBindDataListener = onMoreBindDataListener;
}
public interface OnBindDataListener<T>{
void onBindViewHolder(T model,CommonViewHolder viewHolder,int type,int position);
int getLayoutId(int type);
}
public interface OnMoreBindDataListener<T> extends OnBindDataListener<T>{
int getItemType(int position);
}
@Override
public int getItemViewType(int position) {
if (onMoreBindDataListener != null) {
return onMoreBindDataListener.getItemType(position);
}
return 0;
}
@NonNull
@NotNull
@Override
public CommonViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
int layoutId = onBindDataListener.getLayoutId(viewType);
CommonViewHolder viewHolder = CommonViewHolder.getViewHolder(parent, layoutId);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull @NotNull CommonViewHolder holder, int position) {
onBindDataListener.onBindViewHolder(mList.get(position),holder,getItemViewType(position),position);
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
}
2、创建CommonViewHolder.java
public class CommonViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> mView;
private View mContentView;
public CommonViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
mView = new SparseArray<>();
mContentView = itemView;
}
public static CommonViewHolder getViewHolder(ViewGroup parent, int layoutId){
return new CommonViewHolder(View.inflate(parent.getContext(), layoutId,null));
}
public <T extends View>T getView(int viewId){
View view = mView.get(viewId);
if (view == null) {
view = mContentView.findViewById(viewId);
mView.put(viewId,view);
}
return (T) view;
}
public CommonViewHolder setText(int viewId,String text){
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
public CommonViewHolder setImageUrl(Context context,int viewId, String url){
ImageView iv = getView(viewId);
GlideHelper.loadUrl(context,url,iv);
return this;
}
public CommonViewHolder setImageResource(int viewId,int resId){
ImageView iv = getView(viewId);
iv.setImageResource(resId);
return this;
}
public CommonViewHolder setVisibility(int viewId,int visibility){
LinearLayout ll = getView(viewId);
ll.setVisibility(visibility);
return this;
}
}
二、使用
1、使用前的配置
1.1、创建layout_search_user_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<RelativeLayout
android:layout_width="60dp"
android:layout_height="60dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/iv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="@drawable/img_boy_icon" />
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:maxLines="1"
android:text="NickName"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:maxLines="1"
android:text="Age" />
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/ll_contact_info"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name" />
<TextView
android:id="@+id/tv_contact_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="phone" />
</LinearLayout>
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Desc" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img_right_arrow" />
</LinearLayout>
</RelativeLayout>
1.2、创建layout_search_title_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="16sp"
android:textColor="@android:color/white"
android:paddingLeft="15dp"
android:gravity="center_vertical"
android:background="@color/colorAccent"
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="50dp" />
</RelativeLayout>
1.3、创建AddFriendModel.java
public class AddFriendModel {
private int type;
private String title;
private String userId;
private String photo;
private boolean sex;
private int age;
private String nickName;
private String desc;
private boolean isContact = false;
private String contactName;
private String contactPhone;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public boolean isContact() {
return isContact;
}
public void setContact(boolean contact) {
isContact = contact;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getContactPhone() {
return contactPhone;
}
public void setContactPhone(String contactPhone) {
this.contactPhone = contactPhone;
}
}
1.4、创建activity_add_friend.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mSearchResultView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
2、开始使用
2.1、多种类型
public static final int TYPE_TITLE = 0;
public static final int TYPE_CONTENT = 1;
private CommonAdapter<AddFriendModel> mAddFriendAdapter;
private List<AddFriendModel> mList = new ArrayList<>();
@Override
protected void initView() {
binding.mSearchResultView.setLayoutManager(new LinearLayoutManager(this));
binding.mSearchResultView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
mAddFriendAdapter = new CommonAdapter<AddFriendModel>(mList, new CommonAdapter.OnMoreBindDataListener<AddFriendModel>() {
@Override
public int getItemType(int position) {
return mList.get(position).getType();
}
@Override
public void onBindViewHolder(AddFriendModel model, CommonViewHolder viewHolder, int type, int position) {
if (model.getType() == TYPE_TITLE){
viewHolder.setText(R.id.tv_title,model.getTitle());
}else if (model.getType() == TYPE_CONTENT){
viewHolder.setImageUrl(AddFriendActivity.this,R.id.iv_photo,model.getPhoto());
viewHolder.setImageResource(R.id.iv_sex,model.isSex() ? R.drawable.img_boy_icon : R.drawable.img_girl_icon);
viewHolder.setText(R.id.tv_nickname,model.getNickName());
viewHolder.itemView.setOnClickListener(v -> {});
}
}
@Override
public int getLayoutId(int type) {
if (type == TYPE_TITLE) {
return R.layout.layout_search_title_item;
}else if (type == TYPE_CONTENT){
return R.layout.layout_search_user_item;
}
return 0;
}
});
binding.mSearchResultView.setAdapter(mAddFriendAdapter);
}
2.2、单个类型
private CommonAdapter<AddFriendModel> mCommonAdapter;
private List<AddFriendModel> mList = new ArrayList<>();
@Override
protected void initView() {
binding.mContactView.setLayoutManager(new LinearLayoutManager(this));
binding.mContactView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
mCommonAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<AddFriendModel>() {
@Override
public void onBindViewHolder(AddFriendModel model, CommonViewHolder viewHolder, int type, int position) {
viewHolder.setImageUrl(ContactFriendActivity.this,R.id.iv_photo,model.getPhoto());
viewHolder.setImageResource(R.id.iv_sex,model.isSex() ? R.drawable.img_boy_icon : R.drawable.img_girl_icon);
viewHolder.setText(R.id.tv_nickname,model.getNickName());
viewHolder.itemView.setOnClickListener(v -> {});
}
@Override
public int getLayoutId(int type) {
return R.layout.layout_search_user_item;
}
});
binding.mContactView.setAdapter(mCommonAdapter);
}
三、效果
1、多种类型 2、单个类型
四、注意事项
1、代码中的binding指的是使用ViewBinding绑定的布局,ViewBinding官方教程:链接。
2、TYPE_TITLE 和 TYPE_CONTENT 在使用前就设置到AddFriendModel里了,所以直接使用getType()方法是有数据的。
3、RecyclerView官方教程:链接。
|