Skip to content

Commit

Permalink
feature: Added removeSectionTitleIfOnlyOneSection control for QMUISti…
Browse files Browse the repository at this point in the history
…ckSectionAdapter.
  • Loading branch information
cgspine committed Jun 8, 2020
1 parent 25b3377 commit 1ae3c33
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public abstract class QMUIDefaultStickySectionAdapter<
T extends QMUISection.Model<T>> extends
QMUIStickySectionAdapter<H, T, QMUIStickySectionAdapter.ViewHolder> {

public QMUIDefaultStickySectionAdapter() {
}

public QMUIDefaultStickySectionAdapter(boolean removeSectionTitleIfOnlyOneSection) {
super(removeSectionTitleIfOnlyOneSection);
}

@NonNull
@Override
protected ViewHolder onCreateSectionLoadingViewHolder(@NonNull ViewGroup viewGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class QMUISectionDiffCallback<H extends QMUISection.Model<H>, T extends Q

private SparseIntArray mNewSectionIndex = new SparseIntArray();
private SparseIntArray mNewItemIndex = new SparseIntArray();
private boolean mRemoveSectionTitleIfOnlyOnceSection;

public QMUISectionDiffCallback(
@Nullable List<QMUISection<H, T>> oldList,
Expand All @@ -50,9 +51,12 @@ public QMUISectionDiffCallback(
if (newList != null) {
mNewList.addAll(newList);
}
}

generateIndex(mOldList, mOldSectionIndex, mOldItemIndex);
generateIndex(mNewList, mNewSectionIndex, mNewItemIndex);
void generateIndex(boolean removeSectionTitleIfOnlyOnceSection){
mRemoveSectionTitleIfOnlyOnceSection = removeSectionTitleIfOnlyOnceSection;
generateIndex(mOldList, mOldSectionIndex, mOldItemIndex, removeSectionTitleIfOnlyOnceSection);
generateIndex(mNewList, mNewSectionIndex, mNewItemIndex, removeSectionTitleIfOnlyOnceSection);
}

public void cloneNewIndexTo(@NonNull SparseIntArray sectionIndex, @NonNull SparseIntArray itemIndex) {
Expand All @@ -67,7 +71,8 @@ public void cloneNewIndexTo(@NonNull SparseIntArray sectionIndex, @NonNull Spars
}

private void generateIndex(List<QMUISection<H, T>> list,
SparseIntArray sectionIndex, SparseIntArray itemIndex) {
SparseIntArray sectionIndex, SparseIntArray itemIndex,
boolean removeSectionTitleIfOnlyOnceSection) {
sectionIndex.clear();
itemIndex.clear();
IndexGenerationInfo generationInfo = new IndexGenerationInfo(sectionIndex, itemIndex);
Expand All @@ -80,7 +85,9 @@ private void generateIndex(List<QMUISection<H, T>> list,
if (section.isLocked()) {
continue;
}
generationInfo.appendIndex(i, ITEM_INDEX_SECTION_HEADER);
if(!removeSectionTitleIfOnlyOnceSection || list.size() > 1){
generationInfo.appendIndex(i, ITEM_INDEX_SECTION_HEADER);
}
if (section.isFold()) {
continue;
}
Expand Down Expand Up @@ -225,6 +232,16 @@ public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return areCustomContentsTheSame(null, oldItemIndex, null, newItemIndex);
}

if(mRemoveSectionTitleIfOnlyOnceSection){
// may be the indentation is changed.
if(mOldList.size() == 1 && mNewList.size() != 1){
return false;
}
if(mOldList.size() != 1 && mNewList.size() == 1){
return false;
}
}

QMUISection<H, T> oldModel = mOldList.get(oldSectionIndex);
QMUISection<H, T> newModel = mNewList.get(newSectionIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public abstract class QMUIStickySectionAdapter<

private Callback<H, T> mCallback;
private ViewCallback mViewCallback;
private final boolean mRemoveSectionTitleIfOnlyOneSection;


public QMUIStickySectionAdapter() {
this(false);
}

public QMUIStickySectionAdapter(boolean removeSectionTitleIfOnlyOneSection) {
mRemoveSectionTitleIfOnlyOneSection = removeSectionTitleIfOnlyOneSection;
}

/**
* see {@link #setData(List, boolean, boolean)}
Expand Down Expand Up @@ -142,6 +152,7 @@ public final void setDataWithoutDiff(@Nullable List<QMUISection<H, T>> data, boo
}
// only used to generate index info
QMUISectionDiffCallback callback = createDiffCallback(mBackupData, mCurrentData);
callback.generateIndex(mRemoveSectionTitleIfOnlyOneSection);
callback.cloneNewIndexTo(mSectionIndex, mItemIndex);
notifyDataSetChanged();
mBackupData.clear();
Expand All @@ -152,6 +163,7 @@ public final void setDataWithoutDiff(@Nullable List<QMUISection<H, T>> data, boo

private void diff(boolean newDataSet, boolean onlyMutateState) {
QMUISectionDiffCallback callback = createDiffCallback(mBackupData, mCurrentData);
callback.generateIndex(mRemoveSectionTitleIfOnlyOneSection);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, false);
callback.cloneNewIndexTo(mSectionIndex, mItemIndex);
diffResult.dispatchUpdatesTo(this);
Expand All @@ -175,13 +187,15 @@ private void diff(boolean newDataSet, boolean onlyMutateState) {
*/
public void refreshCustomData() {
QMUISectionDiffCallback callback = createDiffCallback(mBackupData, mCurrentData);
callback.generateIndex(mRemoveSectionTitleIfOnlyOneSection);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, false);
callback.cloneNewIndexTo(mSectionIndex, mItemIndex);
diffResult.dispatchUpdatesTo(this);
}

protected QMUISectionDiffCallback<H, T> createDiffCallback(
List<QMUISection<H, T>> lastData, List<QMUISection<H, T>> currentData) {
List<QMUISection<H, T>> lastData,
List<QMUISection<H, T>> currentData) {
return new QMUISectionDiffCallback<>(lastData, currentData);
}

Expand All @@ -194,6 +208,10 @@ void setViewCallback(ViewCallback viewCallback) {
}


public int getSectionCount() {
return mCurrentData.size();
}

public int getItemIndex(int position) {
if (position < 0 || position >= mItemIndex.size()) {
return QMUISection.ITEM_INDEX_UNKNOWN;
Expand Down Expand Up @@ -570,6 +588,10 @@ public int getRelativeStickyPosition(int position) {
return position;
}

public boolean isRemoveSectionTitleIfOnlyOneSection() {
return mRemoveSectionTitleIfOnlyOneSection;
}

@Override
public final int getItemCount() {
return mItemIndex.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.section.QMUIDefaultStickySectionAdapter;
import com.qmuiteam.qmui.widget.section.QMUISection;
import com.qmuiteam.qmuidemo.R;
Expand All @@ -37,6 +38,13 @@

public class QDGridSectionAdapter extends QMUIDefaultStickySectionAdapter<SectionHeader, SectionItem> {

public QDGridSectionAdapter() {
}

public QDGridSectionAdapter(boolean removeSectionTitleIfOnlyOneSection) {
super(removeSectionTitleIfOnlyOneSection);
}

@NonNull
@Override
protected ViewHolder onCreateSectionHeaderViewHolder(@NonNull ViewGroup viewGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@

public class QDListSectionAdapter extends QDGridSectionAdapter {

public QDListSectionAdapter() {
}

public QDListSectionAdapter(boolean removeSectionTitleIfOnlyOneSection) {
super(removeSectionTitleIfOnlyOneSection);
}

@NonNull
@Override
protected ViewHolder onCreateSectionItemViewHolder(@NonNull ViewGroup viewGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class QDListSectionLayoutFragment extends QDBaseSectionLayoutFragment {

@Override
protected QMUIStickySectionAdapter<SectionHeader, SectionItem, QMUIStickySectionAdapter.ViewHolder> createAdapter() {
return new QDListSectionAdapter();
return new QDListSectionAdapter(true);
}

@Override
Expand Down

0 comments on commit 1ae3c33

Please sign in to comment.