-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimization of multiple item use code
- Loading branch information
Showing
10 changed files
with
175 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...rc/main/java/com/chad/baserecyclerviewadapterhelper/adapter/MultipleItemQuickAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.chad.baserecyclerviewadapterhelper.adapter; | ||
|
||
import android.content.Context; | ||
|
||
import com.chad.baserecyclerviewadapterhelper.R; | ||
import com.chad.baserecyclerviewadapterhelper.entity.MultipleItem; | ||
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter; | ||
import com.chad.library.adapter.base.BaseViewHolder; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper | ||
*/ | ||
public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<MultipleItem> { | ||
|
||
public MultipleItemQuickAdapter(Context context, List data) { | ||
super(context, data); | ||
addItmeType(MultipleItem.TEXT, R.layout.text_view); | ||
addItmeType(MultipleItem.IMG, R.layout.image_view); | ||
} | ||
|
||
@Override | ||
protected void convert(BaseViewHolder helper, MultipleItem item) { | ||
switch (helper.getItemViewType()) { | ||
case MultipleItem.TEXT: | ||
helper.setImageUrl(R.id.tv, item.getContent()); | ||
break; | ||
case MultipleItem.IMG: | ||
helper.setImageUrl(R.id.iv, item.getContent()); | ||
break; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/chad/baserecyclerviewadapterhelper/entity/MultipleItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.chad.baserecyclerviewadapterhelper.entity; | ||
|
||
import com.chad.library.adapter.base.entity.MultiItemEntity; | ||
|
||
/** | ||
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper | ||
*/ | ||
public class MultipleItem extends MultiItemEntity { | ||
public static final int TEXT = 1; | ||
public static final int IMG = 2; | ||
|
||
private String content; | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
library/src/main/java/com/chad/library/adapter/base/BaseMultiItemQuickAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.chad.library.adapter.base; | ||
|
||
import android.content.Context; | ||
import android.util.SparseArray; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.chad.library.adapter.base.entity.MultiItemEntity; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper | ||
*/ | ||
public abstract class BaseMultiItemQuickAdapter<T extends MultiItemEntity> extends BaseQuickAdapter { | ||
protected List<T> mData; | ||
private SparseArray<Integer> layouts; | ||
|
||
/** | ||
* Same as QuickAdapter#QuickAdapter(Context,int) but with | ||
* some initialization data. | ||
* @param context The context. | ||
* @param data A new list is created out of this one to avoid mutable list | ||
*/ | ||
public BaseMultiItemQuickAdapter(Context context, List<T> data) { | ||
super(context, data); | ||
this.mData = data == null ? new ArrayList<T>() : new ArrayList<T>(data); | ||
this.mContext = context; | ||
} | ||
|
||
@Override | ||
protected int getDefItemViewType(int position) { | ||
return mData.get(position).getItemType(); | ||
} | ||
|
||
|
||
@Override | ||
protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) { | ||
int layoutId = getLayoutId(viewType); | ||
View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false); | ||
return new BaseViewHolder(mContext, view); | ||
} | ||
|
||
private int getLayoutId(int viewType) { | ||
return layouts.get(viewType); | ||
} | ||
|
||
protected void addItmeType(int type, int layoutResId) { | ||
if (layouts == null) { | ||
layouts = new SparseArray<>(); | ||
} | ||
layouts.put(type, layoutResId); | ||
} | ||
|
||
@Override | ||
protected void convert(BaseViewHolder helper, Object item) { | ||
convert(helper, (T) item); | ||
} | ||
|
||
protected abstract void convert(BaseViewHolder helper, T item); | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
library/src/main/java/com/chad/library/adapter/base/entity/MultiItemEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.chad.library.adapter.base.entity; | ||
|
||
/** | ||
* 项目名称:base-adapter-helper-recyclerview-master | ||
* 类描述: | ||
* 创建人:Chad | ||
* 创建时间:16/4/22 上午11:04 | ||
*/ | ||
public abstract class MultiItemEntity { | ||
protected int itemType; | ||
|
||
public int getItemType() { | ||
return itemType; | ||
} | ||
|
||
public void setItemType(int itemType) { | ||
this.itemType = itemType; | ||
} | ||
} |