Skip to content

Commit

Permalink
V1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Mar 7, 2018
1 parent 13c1459 commit 49012ca
Show file tree
Hide file tree
Showing 20 changed files with 186 additions and 120 deletions.
@@ -1,6 +1,5 @@
package com.sunfusheng;

import android.annotation.SuppressLint;
import android.content.Context;

import java.util.HashMap;
Expand All @@ -12,7 +11,6 @@
*/
abstract public class ExpandableGroupRecyclerViewAdapter<T> extends GroupRecyclerViewAdapter<T> {

@SuppressLint("UseSparseArrays")
private Map<Integer, List<T>> cache = new HashMap<>();

public ExpandableGroupRecyclerViewAdapter(Context context) {
Expand Down
Expand Up @@ -39,7 +39,7 @@ public static <T> boolean checkGroupData(List<T> group, int minCountPerGroup) {

public static <T> boolean checkGroupsData(T[][] groups, int minCountPerGroup) {
if (!isEmpty(groups)) {
return checkGroupsData(convertGroupsData(groups), minCountPerGroup);
return checkGroupsData(convertGroupsData(groups, minCountPerGroup), minCountPerGroup);
}
return false;
}
Expand All @@ -52,24 +52,24 @@ public static <T> boolean checkGroupsData(List<List<T>> groups, int minCountPerG
Iterator<List<T>> iterator = groups.iterator();
while (iterator.hasNext()) {
List<T> group = iterator.next();
if (isEmpty(group) || minCountPerGroup > group.size()) {
if (isEmpty(group) || group.size() < minCountPerGroup) {
iterator.remove();
Log.w(TAG, "Data illegal, already removed group = " + group);
}
}
return !isEmpty(groups);
}

public static <T> List<List<T>> convertGroupsData(T[][] groups) {
public static <T> List<List<T>> convertGroupsData(T[][] groups, int minCountPerGroup) {
List<List<T>> lists = new ArrayList<>();
if (isEmpty(groups)) {
return lists;
}

for (T[] group : groups) {
List<T> list = new ArrayList<>();
list.addAll(Arrays.asList(group));
lists.add(list);
if (!isEmpty(groups)) {
for (T[] group : groups) {
if (!isEmpty(group) && group.length >= minCountPerGroup) {
List<T> list = new ArrayList<>();
list.addAll(Arrays.asList(group));
lists.add(list);
}
}
}
return lists;
}
Expand Down
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

Expand Down Expand Up @@ -33,12 +32,10 @@ public GroupRecyclerViewAdapter(Context context) {
}

public GroupRecyclerViewAdapter(Context context, T[][] groups) {
GroupAdapterUtils.checkGroupsData(groups, minCountPerGroup());
init(context, GroupAdapterUtils.convertGroupsData(groups));
init(context, GroupAdapterUtils.convertGroupsData(groups, minCountPerGroup()));
}

public GroupRecyclerViewAdapter(Context context, List<List<T>> groups) {
GroupAdapterUtils.checkGroupsData(groups, minCountPerGroup());
init(context, groups);
}

Expand All @@ -49,13 +46,13 @@ private void init(Context context, List<List<T>> groups) {
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void setItems(T[][] groups) {
public void setGroups(T[][] groups) {
GroupAdapterUtils.checkGroupsData(groups, minCountPerGroup());
this.groups = GroupAdapterUtils.convertGroupsData(groups);
this.groups = GroupAdapterUtils.convertGroupsData(groups, minCountPerGroup());
notifyDataSetChanged();
}

public void setItems(List<List<T>> groups) {
public void setGroups(List<List<T>> groups) {
GroupAdapterUtils.checkGroupsData(groups, minCountPerGroup());
this.groups = groups;
notifyDataSetChanged();
Expand Down Expand Up @@ -110,7 +107,6 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int groupPosition = getGroupPosition(position);
int childPosition = getGroupChildPosition(groupPosition, position);
T item = getItem(groupPosition, childPosition);
Log.d(TAG, "position: " + position + " groupPosition: " + groupPosition + " childPosition: " + childPosition);

if (null != onItemClickListener) {
holder.itemView.setOnClickListener(view -> {
Expand Down Expand Up @@ -279,40 +275,68 @@ public int countGroupsItemsRange(int start, int count) {
return GroupAdapterUtils.countGroupsItemsRange(groups, start, count);
}

/****************************************************************
* insert operation
***************************************************************/

public boolean insertGroup(int groupPosition, T[] group) {
return insertGroup(groupPosition, group, true);
}

public boolean insertGroup(int groupPosition, T[] group, boolean withAnim) {
if (checkGroupPositionForInsert(groupPosition) && GroupAdapterUtils.checkGroupData(group, minCountPerGroup())) {
List<T> list = Arrays.asList(group);
return insertGroup(groupPosition, new ArrayList<>(list));
return insertGroup(groupPosition, new ArrayList<>(list), withAnim);
}
return false;
}

public boolean insertGroup(int groupPosition, List<T> group) {
return insertGroup(groupPosition, group, true);
}

public boolean insertGroup(int groupPosition, List<T> group, boolean withAnim) {
if (checkGroupPositionForInsert(groupPosition) && GroupAdapterUtils.checkGroupData(group, minCountPerGroup())) {
this.groups.add(groupPosition, group);
int positionStart = countGroupsItemsRange(0, groupPosition);
notifyItemRangeInserted(positionStart, group.size());
notifyItemRangeChanged(positionStart + group.size(), getItemCount() - positionStart - group.size());
if (withAnim) {
int positionStart = countGroupsItemsRange(0, groupPosition);
notifyItemRangeInserted(positionStart, group.size());
notifyItemRangeChanged(positionStart + group.size(), getItemCount() - positionStart - group.size());
} else {
notifyDataSetChanged();
}
return true;
}
return false;
}

public boolean insertGroups(int groupPosition, T[][] groups) {
return insertGroups(groupPosition, groups, true);
}

public boolean insertGroups(int groupPosition, T[][] groups, boolean withAnim) {
if (checkGroupPositionForInsert(groupPosition) && GroupAdapterUtils.checkGroupsData(groups, minCountPerGroup())) {
List<List<T>> lists = GroupAdapterUtils.convertGroupsData(groups);
return insertGroups(groupPosition, lists);
List<List<T>> lists = GroupAdapterUtils.convertGroupsData(groups, minCountPerGroup());
return insertGroups(groupPosition, lists, withAnim);
}
return false;
}

public boolean insertGroups(int groupPosition, List<List<T>> groups) {
return insertGroups(groupPosition, groups, true);
}

public boolean insertGroups(int groupPosition, List<List<T>> groups, boolean withAnim) {
if (checkGroupPositionForInsert(groupPosition) && GroupAdapterUtils.checkGroupsData(groups, minCountPerGroup())) {
this.groups.addAll(groupPosition, groups);
int groupItemCount = GroupAdapterUtils.countGroupsItemsRange(groups, 0, groups.size());
int positionStart = countGroupsItemsRange(0, groupPosition);
notifyItemRangeInserted(positionStart, groupItemCount);
notifyItemRangeChanged(positionStart + groupItemCount, getItemCount() - positionStart - groupItemCount);
if (withAnim) {
int groupItemCount = GroupAdapterUtils.countGroupsItemsRange(groups, 0, groups.size());
int positionStart = countGroupsItemsRange(0, groupPosition);
notifyItemRangeInserted(positionStart, groupItemCount);
notifyItemRangeChanged(positionStart + groupItemCount, getItemCount() - positionStart - groupItemCount);
} else {
notifyDataSetChanged();
}
return true;
}
return false;
Expand Down Expand Up @@ -378,18 +402,35 @@ public boolean insertItems(int groupPosition, int childPosition, List<T> items,
return false;
}

/****************************************************************
* remove operation
***************************************************************/

public boolean removeGroup(int groupPosition) {
return removeGroup(groupPosition, true);
}

public boolean removeGroup(int groupPosition, boolean withAnim) {
if (checkGroupPosition(groupPosition)) {
int positionStart = countGroupsItemsRange(0, groupPosition);
int itemCount = countGroupItems(groupPosition);
this.groups.remove(groupPosition);
notifyItemRangeRemoved(positionStart, countGroupItems(groupPosition));
notifyItemRangeChanged(positionStart, getItemCount() - positionStart);
if (withAnim) {
notifyItemRangeRemoved(positionStart, itemCount);
notifyItemRangeChanged(positionStart, getItemCount() - positionStart);
} else {
notifyDataSetChanged();
}
return true;
}
return false;
}

public boolean removeGroups(int groupPosition, int count) {
return removeGroups(groupPosition, count, true);
}

public boolean removeGroups(int groupPosition, int count, boolean withAnim) {
if (checkGroupPosition(groupPosition) && count > 0) {
int groupsCount = count;
if (groupPosition + count > groupsCount()) {
Expand All @@ -398,11 +439,17 @@ public boolean removeGroups(int groupPosition, int count) {

int positionStart = countGroupsItemsRange(0, groupPosition);
int itemCount = countGroupsItemsRange(groupPosition, groupsCount);

for (int i = 0; i < groupsCount; i++) {
this.groups.remove(groupPosition);
}
notifyItemRangeRemoved(positionStart, itemCount);
notifyItemRangeChanged(positionStart, getItemCount() - positionStart);

if (withAnim) {
notifyItemRangeRemoved(positionStart, itemCount);
notifyItemRangeChanged(positionStart, getItemCount() - positionStart);
} else {
notifyDataSetChanged();
}
return true;
}
return false;
Expand Down Expand Up @@ -452,6 +499,10 @@ public boolean removeItems(int groupPosition, int childPosition, int count, bool
return false;
}

/****************************************************************
* update operation
***************************************************************/

public boolean updateGroup(int groupPosition, T[] group) {
if (checkGroupPosition(groupPosition) && GroupAdapterUtils.checkGroupData(group, minCountPerGroup())) {
List<T> list = Arrays.asList(group);
Expand All @@ -473,7 +524,7 @@ public boolean updateGroup(int groupPosition, List<T> group) {

public boolean updateGroups(int groupPosition, T[][] groups) {
if (!GroupAdapterUtils.isEmpty(groups)) {
return updateGroups(groupPosition, GroupAdapterUtils.convertGroupsData(groups));
return updateGroups(groupPosition, GroupAdapterUtils.convertGroupsData(groups, minCountPerGroup()));
}
return false;
}
Expand Down Expand Up @@ -590,6 +641,10 @@ public boolean showFooter() {
return false;
}

/****************************************************************
* abstract method
***************************************************************/

abstract public int getHeaderLayoutId(int viewType);

abstract public int getChildLayoutId(int viewType);
Expand All @@ -606,6 +661,10 @@ public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

/****************************************************************
* interface
***************************************************************/

public interface OnItemClickListener {
void onItemClick(GroupRecyclerViewAdapter adapter, GroupViewHolder holder, int groupPosition, int childPosition);
}
Expand Down
Expand Up @@ -8,7 +8,7 @@
import android.widget.TextView;

import com.sunfusheng.adapter.sample.adapter.ExpandCollapseGroupAdapter;
import com.sunfusheng.adapter.sample.util.GroupData;
import com.sunfusheng.adapter.sample.util.DataSource;

/**
* @author sunfusheng on 2018/2/9.
Expand Down Expand Up @@ -36,7 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {

RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ExpandCollapseGroupAdapter expandableAdapter = new ExpandCollapseGroupAdapter(this, GroupData.items);
ExpandCollapseGroupAdapter expandableAdapter = new ExpandCollapseGroupAdapter(this, DataSource.items);
recyclerView.setAdapter(expandableAdapter);

expandableAdapter.setOnItemClickListener((adapter, holder, groupPosition, childPosition) -> {
Expand Down
Expand Up @@ -5,7 +5,7 @@
import android.support.v7.widget.RecyclerView;

import com.sunfusheng.adapter.sample.adapter.FooterGroupAdapter;
import com.sunfusheng.adapter.sample.util.GroupData;
import com.sunfusheng.adapter.sample.util.DataSource;
import com.sunfusheng.adapter.sample.util.Utils;

/**
Expand All @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {

RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FooterGroupAdapter footerAdapter = new FooterGroupAdapter(this, GroupData.items);
FooterGroupAdapter footerAdapter = new FooterGroupAdapter(this, DataSource.items);
recyclerView.setAdapter(footerAdapter);

footerAdapter.setOnItemClickListener((adapter, holder, groupPosition, childPosition) -> {
Expand Down
Expand Up @@ -5,7 +5,7 @@
import android.support.v7.widget.RecyclerView;

import com.sunfusheng.adapter.sample.adapter.HeaderGroupAdapter;
import com.sunfusheng.adapter.sample.util.GroupData;
import com.sunfusheng.adapter.sample.util.DataSource;
import com.sunfusheng.adapter.sample.util.Utils;

/**
Expand All @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {

RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
HeaderGroupAdapter headerAdapter = new HeaderGroupAdapter(this, GroupData.items);
HeaderGroupAdapter headerAdapter = new HeaderGroupAdapter(this, DataSource.items);
recyclerView.setAdapter(headerAdapter);

headerAdapter.setOnItemClickListener((adapter, holder, groupPosition, childPosition) -> {
Expand Down
Expand Up @@ -5,7 +5,7 @@
import android.support.v7.widget.RecyclerView;

import com.sunfusheng.adapter.sample.adapter.HeaderFooterGroupAdapter;
import com.sunfusheng.adapter.sample.util.GroupData;
import com.sunfusheng.adapter.sample.util.DataSource;
import com.sunfusheng.adapter.sample.util.Utils;

/**
Expand All @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {

RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
HeaderFooterGroupAdapter headerFooterAdapter = new HeaderFooterGroupAdapter(this, GroupData.items);
HeaderFooterGroupAdapter headerFooterAdapter = new HeaderFooterGroupAdapter(this, DataSource.items);
recyclerView.setAdapter(headerFooterAdapter);

headerFooterAdapter.setOnItemClickListener((adapter, holder, groupPosition, childPosition) -> {
Expand Down

0 comments on commit 49012ca

Please sign in to comment.