Skip to content

Commit

Permalink
Merge pull request #2429 from NFLeo/master
Browse files Browse the repository at this point in the history
Add section multiple combination type and demo
  • Loading branch information
AllenCoder authored Aug 30, 2018
2 parents 64c9c76 + b7610d2 commit 243ec7f
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
android:name=".HomeActivity"
android:launchMode="singleTask"></activity>
<activity android:name=".UpFetchUseActivity" />
<activity android:name=".SectionMultipleItemUseActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public class HomeActivity extends AppCompatActivity {
private static final Class<?>[] ACTIVITY = {AnimationUseActivity.class, ChooseMultipleItemUseTypeActivity.class, HeaderAndFooterUseActivity.class, PullToRefreshUseActivity.class, SectionUseActivity.class, EmptyViewUseActivity.class, ItemDragAndSwipeUseActivity.class, ItemClickActivity.class, ExpandableUseActivity.class, DataBindingUseActivity.class,UpFetchUseActivity.class};
private static final String[] TITLE = {"Animation", "MultipleItem", "Header/Footer", "PullToRefresh", "Section", "EmptyView", "DragAndSwipe", "ItemClick", "ExpandableItem", "DataBinding", "UpFetchData"};
private static final int[] IMG = {R.mipmap.gv_animation, R.mipmap.gv_multipleltem, R.mipmap.gv_header_and_footer, R.mipmap.gv_pulltorefresh, R.mipmap.gv_section, R.mipmap.gv_empty, R.mipmap.gv_drag_and_swipe, R.mipmap.gv_item_click, R.mipmap.gv_expandable, R.mipmap.gv_databinding,R.drawable.gv_up_fetch};
private static final Class<?>[] ACTIVITY = {AnimationUseActivity.class, ChooseMultipleItemUseTypeActivity.class, HeaderAndFooterUseActivity.class, PullToRefreshUseActivity.class, SectionUseActivity.class, EmptyViewUseActivity.class, ItemDragAndSwipeUseActivity.class, ItemClickActivity.class, ExpandableUseActivity.class, DataBindingUseActivity.class,UpFetchUseActivity.class,SectionMultipleItemUseActivity.class};
private static final String[] TITLE = {"Animation", "MultipleItem", "Header/Footer", "PullToRefresh", "Section", "EmptyView", "DragAndSwipe", "ItemClick", "ExpandableItem", "DataBinding", "UpFetchData", "SectionMultipleItem"};
private static final int[] IMG = {R.mipmap.gv_animation, R.mipmap.gv_multipleltem, R.mipmap.gv_header_and_footer, R.mipmap.gv_pulltorefresh, R.mipmap.gv_section, R.mipmap.gv_empty, R.mipmap.gv_drag_and_swipe, R.mipmap.gv_item_click, R.mipmap.gv_expandable, R.mipmap.gv_databinding,R.drawable.gv_up_fetch, R.mipmap.gv_multipleltem};
private ArrayList<HomeItem> mDataList;
private RecyclerView mRecyclerView;

Expand Down
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);
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.chad.baserecyclerviewadapterhelper.entity.MultipleItem;
import com.chad.baserecyclerviewadapterhelper.entity.MySection;
import com.chad.baserecyclerviewadapterhelper.entity.NormalMultipleEntity;
import com.chad.baserecyclerviewadapterhelper.entity.SectionMultipleItem;
import com.chad.baserecyclerviewadapterhelper.entity.Status;
import com.chad.baserecyclerviewadapterhelper.entity.Video;

Expand Down Expand Up @@ -73,6 +74,34 @@ public static List<MySection> getSampleData() {
return list;
}

public static List<SectionMultipleItem> getSectionMultiData() {
List<SectionMultipleItem> list = new ArrayList<>();
Video video = new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD);

// add section data
list.add(new SectionMultipleItem(true, "Section 1", true));
// add multiple type item data ---start---
list.add(new SectionMultipleItem(SectionMultipleItem.TEXT, new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, "video_id_0")));
list.add(new SectionMultipleItem(SectionMultipleItem.TEXT, new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, "video_id_1")));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, "video_id_2")));
// ---end---

list.add(new SectionMultipleItem(true, "Section 2", false));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
list.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
list.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
list.add(new SectionMultipleItem(true, "Section 3", false));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
list.add(new SectionMultipleItem(true, "Section 4", false));
list.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
list.add(new SectionMultipleItem(true, "Section 5", false));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
list.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
return list;
}

public static List<String> getStrData() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Expand Down
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;
}
}
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);
}
}
}


Loading

0 comments on commit 243ec7f

Please sign in to comment.