-
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.
Merge pull request #2429 from NFLeo/master
Add section multiple combination type and demo
- Loading branch information
Showing
9 changed files
with
515 additions
and
3 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
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionMultipleItemUseActivity.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,60 @@ | ||
package com.chad.baserecyclerviewadapterhelper; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
import com.chad.baserecyclerviewadapterhelper.adapter.SectionMultipleItemAdapter; | ||
import com.chad.baserecyclerviewadapterhelper.base.BaseActivity; | ||
import com.chad.baserecyclerviewadapterhelper.data.DataServer; | ||
import com.chad.baserecyclerviewadapterhelper.entity.SectionMultipleItem; | ||
import com.chad.library.adapter.base.BaseQuickAdapter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* to get SectionMultipleItem you need follow two things | ||
* 1.create entity which extend SectionMultiEntity | ||
* 2.create adapter which extend BaseSectionMultiItemQuickAdapter | ||
*/ | ||
public class SectionMultipleItemUseActivity extends BaseActivity { | ||
private RecyclerView mRecyclerView; | ||
private List<SectionMultipleItem> mData; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_section_uer); | ||
setBackBtn(); | ||
setTitle("SectionMultiple Use"); | ||
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); | ||
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); | ||
|
||
// 1. create entityList which item data extend SectionMultiEntity | ||
mData = DataServer.getSectionMultiData(); | ||
|
||
// create adapter which extend BaseSectionMultiItemQuickAdapter provide your headerResId | ||
SectionMultipleItemAdapter sectionAdapter = new SectionMultipleItemAdapter(R.layout.def_section_head, mData); | ||
sectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { | ||
@Override | ||
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { | ||
SectionMultipleItem item = (SectionMultipleItem) adapter.getData().get(position); | ||
switch (view.getId()) { | ||
case R.id.card_view: | ||
// 获取主体item相应数据给后期使用 | ||
if (item.getVideo() != null) { | ||
Toast.makeText(SectionMultipleItemUseActivity.this, item.getVideo().getName(), Toast.LENGTH_LONG).show(); | ||
} | ||
break; | ||
default: | ||
Toast.makeText(SectionMultipleItemUseActivity.this, "OnItemChildClickListener " + position, Toast.LENGTH_LONG).show(); | ||
break; | ||
|
||
} | ||
} | ||
}); | ||
mRecyclerView.setAdapter(sectionAdapter); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionMultipleItemAdapter.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,57 @@ | ||
package com.chad.baserecyclerviewadapterhelper.adapter; | ||
|
||
import com.chad.baserecyclerviewadapterhelper.R; | ||
import com.chad.baserecyclerviewadapterhelper.entity.MultipleItem; | ||
import com.chad.baserecyclerviewadapterhelper.entity.SectionMultipleItem; | ||
import com.chad.library.adapter.base.BaseSectionMultiItemQuickAdapter; | ||
import com.chad.library.adapter.base.BaseViewHolder; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper | ||
*/ | ||
public class SectionMultipleItemAdapter extends BaseSectionMultiItemQuickAdapter<SectionMultipleItem, BaseViewHolder> { | ||
/** | ||
* init SectionMultipleItemAdapter | ||
* 1. add your header resource layout | ||
* 2. add some kind of items | ||
* | ||
* @param sectionHeadResId The section head layout id for each item | ||
* @param data A new list is created out of this one to avoid mutable list | ||
*/ | ||
public SectionMultipleItemAdapter(int sectionHeadResId, List data) { | ||
super(sectionHeadResId, data); | ||
addItemType(SectionMultipleItem.TEXT, R.layout.item_text_view); | ||
addItemType(SectionMultipleItem.IMG_TEXT, R.layout.item_img_text_view); | ||
} | ||
|
||
@Override | ||
protected void convertHead(BaseViewHolder helper, final SectionMultipleItem item) { | ||
// deal with header viewHolder | ||
helper.setText(R.id.header, item.header); | ||
helper.setVisible(R.id.more, item.isMore()); | ||
helper.addOnClickListener(R.id.more); | ||
} | ||
|
||
@Override | ||
protected void convert(BaseViewHolder helper, SectionMultipleItem item) { | ||
// deal with multiple type items viewHolder | ||
helper.addOnClickListener(R.id.card_view); | ||
switch (helper.getItemViewType()) { | ||
case MultipleItem.TEXT: | ||
helper.setText(R.id.tv, item.getVideo().getName()); | ||
break; | ||
case MultipleItem.IMG_TEXT: | ||
switch (helper.getLayoutPosition() % 2) { | ||
case 0: | ||
helper.setImageResource(R.id.iv, R.mipmap.animation_img1); | ||
break; | ||
case 1: | ||
helper.setImageResource(R.id.iv, R.mipmap.animation_img2); | ||
break; | ||
} | ||
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
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/chad/baserecyclerviewadapterhelper/entity/SectionMultipleItem.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,49 @@ | ||
package com.chad.baserecyclerviewadapterhelper.entity; | ||
|
||
import com.chad.library.adapter.base.entity.MultiItemEntity; | ||
import com.chad.library.adapter.base.entity.SectionMultiEntity; | ||
|
||
/** | ||
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper | ||
*/ | ||
public class SectionMultipleItem extends SectionMultiEntity<Video> implements MultiItemEntity { | ||
|
||
public static final int TEXT = 1; | ||
public static final int IMG = 2; | ||
public static final int IMG_TEXT = 3; | ||
private int itemType; | ||
private boolean isMore; | ||
private Video video; | ||
|
||
public SectionMultipleItem(boolean isHeader, String header, boolean isMore) { | ||
super(isHeader, header); | ||
this.isMore = isMore; | ||
} | ||
|
||
public SectionMultipleItem(int itemType, Video video) { | ||
super(video); | ||
this.video = video; | ||
this.itemType = itemType; | ||
} | ||
|
||
public boolean isMore() { | ||
return isMore; | ||
} | ||
|
||
public void setMore(boolean more) { | ||
isMore = more; | ||
} | ||
|
||
public Video getVideo() { | ||
return video; | ||
} | ||
|
||
public void setVideo(Video video) { | ||
this.video = video; | ||
} | ||
|
||
@Override | ||
public int getItemType() { | ||
return itemType; | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
library/src/main/java/com/chad/library/adapter/base/BaseSectionMultiItemQuickAdapter.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,148 @@ | ||
package com.chad.library.adapter.base; | ||
|
||
import android.support.annotation.IntRange; | ||
import android.support.annotation.LayoutRes; | ||
import android.util.SparseIntArray; | ||
import android.view.ViewGroup; | ||
|
||
import com.chad.library.adapter.base.entity.IExpandable; | ||
import com.chad.library.adapter.base.entity.MultiItemEntity; | ||
import com.chad.library.adapter.base.entity.SectionMultiEntity; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper | ||
*/ | ||
public abstract class BaseSectionMultiItemQuickAdapter<T extends SectionMultiEntity, K extends BaseViewHolder> extends BaseQuickAdapter<T, K> { | ||
|
||
/** | ||
* layouts indexed with their types | ||
*/ | ||
private SparseIntArray layouts; | ||
|
||
private static final int DEFAULT_VIEW_TYPE = -0xff; | ||
public static final int TYPE_NOT_FOUND = -404; | ||
|
||
protected int mSectionHeadResId; | ||
protected static final int SECTION_HEADER_VIEW = 0x00000444; | ||
|
||
/** | ||
* Same as QuickAdapter#QuickAdapter(Context,int) but with | ||
* some initialization data. | ||
* | ||
* @param sectionHeadResId The section head layout id for each item | ||
* @param data A new list is created out of this one to avoid mutable list | ||
*/ | ||
public BaseSectionMultiItemQuickAdapter(int sectionHeadResId, List<T> data) { | ||
super(data); | ||
this.mSectionHeadResId = sectionHeadResId; | ||
} | ||
|
||
@Override | ||
protected int getDefItemViewType(int position) { | ||
T item = mData.get(position); | ||
|
||
if (item != null) { | ||
// check the item type include header or not | ||
return item.isHeader ? SECTION_HEADER_VIEW : item.getItemType(); | ||
} | ||
return DEFAULT_VIEW_TYPE; | ||
} | ||
|
||
protected void setDefaultViewTypeLayout(@LayoutRes int layoutResId) { | ||
addItemType(DEFAULT_VIEW_TYPE, layoutResId); | ||
} | ||
|
||
@Override | ||
protected K onCreateDefViewHolder(ViewGroup parent, int viewType) { | ||
// add this to check viewType of section | ||
if (viewType == SECTION_HEADER_VIEW) | ||
return createBaseViewHolder(getItemView(mSectionHeadResId, parent)); | ||
|
||
return createBaseViewHolder(parent, getLayoutId(viewType)); | ||
} | ||
|
||
private int getLayoutId(int viewType) { | ||
return layouts.get(viewType, TYPE_NOT_FOUND); | ||
} | ||
|
||
/** | ||
* collect layout types you need | ||
* | ||
* @param type The key of layout type | ||
* @param layoutResId The layoutResId of layout type | ||
*/ | ||
protected void addItemType(int type, @LayoutRes int layoutResId) { | ||
if (layouts == null) { | ||
layouts = new SparseIntArray(); | ||
} | ||
layouts.put(type, layoutResId); | ||
} | ||
|
||
@Override | ||
protected boolean isFixedViewType(int type) { | ||
return super.isFixedViewType(type) || type == SECTION_HEADER_VIEW; | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(K holder, int position) { | ||
switch (holder.getItemViewType()) { | ||
case SECTION_HEADER_VIEW: | ||
setFullSpan(holder); | ||
convertHead(holder, getItem(position - getHeaderLayoutCount())); | ||
break; | ||
default: | ||
super.onBindViewHolder(holder, position); | ||
break; | ||
} | ||
} | ||
|
||
protected abstract void convertHead(K helper, T item); | ||
|
||
@Override | ||
public void remove(@IntRange(from = 0L) int position) { | ||
if (mData == null || position < 0 | ||
|| position >= mData.size()) return; | ||
|
||
T entity = mData.get(position); | ||
if (entity instanceof IExpandable) { | ||
removeAllChild((IExpandable) entity, position); | ||
} | ||
removeDataFromParent(entity); | ||
super.remove(position); | ||
} | ||
|
||
/** | ||
* 移除父控件时,若父控件处于展开状态,则先移除其所有的子控件 | ||
* | ||
* @param parent 父控件实体 | ||
* @param parentPosition 父控件位置 | ||
*/ | ||
protected void removeAllChild(IExpandable parent, int parentPosition) { | ||
if (parent.isExpanded()) { | ||
List<MultiItemEntity> chidChilds = parent.getSubItems(); | ||
if (chidChilds == null || chidChilds.size() == 0) return; | ||
|
||
int childSize = chidChilds.size(); | ||
for (int i = 0; i < childSize; i++) { | ||
remove(parentPosition + 1); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 移除子控件时,移除父控件实体类中相关子控件数据,避免关闭后再次展开数据重现 | ||
* | ||
* @param child 子控件实体 | ||
*/ | ||
protected void removeDataFromParent(T child) { | ||
int position = getParentPosition(child); | ||
if (position >= 0) { | ||
IExpandable parent = (IExpandable) mData.get(position); | ||
parent.getSubItems().remove(child); | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.