diff --git a/.circleci/config.yml b/.circleci/config.yml index c81552d1a..7b1c9af8c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ jobs: build: working_directory: ~/code docker: - - image: circleci/android:api-26-alpha + - image: circleci/android:api-28-alpha environment: JVM_OPTS: -Xmx3200m steps: diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 000000000..69d168fb1 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,7 @@ +Thank you for contributing to BaseRecyclerViewAdapterHelper. Before pressing the "Create Pull Request" button, please consider the following points: + + - [1] Please give a description about what and why you are contributing, even if it's trivial. + + - [2] Please include the issue list number(s) or other PR numbers in the description if you are contributing in response to those. + + - [3] Please include a reasonable set of demo tests if you contribute new code or change an existing one. please make sure you have demo for working correctly. diff --git a/.travis.yml b/.travis.yml index abf269e44..4d47b7172 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: android +dist: trusty jdk: oraclejdk8 sudo: false @@ -6,15 +7,15 @@ android: components: - tools - platform-tools - - build-tools-27.0.2 - - android-27 + - build-tools-28.0.3 + - android-28 - extra-android-m2repository - extra-android-support before_install: - chmod +x gradlew - mkdir "$ANDROID_HOME/licenses" || true # Hack to accept Android licenses - - yes | sdkmanager "platforms;android-27" + - yes | sdkmanager "platforms;android-28" script: - ./gradlew assembleRelease diff --git a/README-cn.md b/README-cn.md index a391ee1e3..a7cd80ce5 100644 --- a/README-cn.md +++ b/README-cn.md @@ -163,7 +163,7 @@ Activity Toast.makeText(RecyclerClickItemActivity.this, "" + Integer.toString(position), Toast.LENGTH_SHORT).show(); } }); -``` +``` # 如何使用它添加动画? @@ -413,6 +413,85 @@ protected K createBaseViewHolder(View view) { ``` +# DiffUtil +先继承`BaseQuickDiffCallback`并实现: +```java +public class DiffDemoCallback extends BaseQuickDiffCallback { + + public DiffDemoCallback(@Nullable List newList) { + super(newList); + } + + /** + * 判断是否是同一个item + * + * @param oldItem New data + * @param newItem old Data + * @return + */ + @Override + protected boolean areItemsTheSame(DiffUtilDemoEntity oldItem, DiffUtilDemoEntity newItem) { +…… + } + + /** + * 当是同一个item时,再判断内容是否发生改变 + * + * @param oldItem New data + * @param newItem old Data + * @return + */ + @Override + protected boolean areContentsTheSame(DiffUtilDemoEntity oldItem, DiffUtilDemoEntity newItem) { +…… + } + + /** + * 可选实现 + * 如果需要精确修改某一个view中的内容,请实现此方法。 + * 如果不实现此方法,或者返回null,将会直接刷新整个item。 + * + * @param oldItem Old data + * @param newItem New data + * @return Payload info. if return null, the entire item will be refreshed. + */ + @Override + protected Object getChangePayload(DiffUtilDemoEntity oldItem, DiffUtilDemoEntity newItem) { +…… + } +} +``` + +`BaseQuickAdapter`中实现`convertPayloads()`方法,用于局部更新: +```java + /** + * + * 当有 payload info 时,只会执行此方法 + * + * @param helper A fully initialized helper. + * @param item The item that needs to be displayed. + * @param payloads payload info. + */ + @Override + protected void convertPayloads(BaseViewHolder helper, DiffUtilDemoEntity item, @NonNull List payloads) { + for (Object p : payloads) { +…… + } + } +``` + +更新整个数据集时,使用如下方法: +```java +DiffDemoCallback callback = new DiffDemoCallback(getNewList()); +mAdapter.setNewDiffData(callback); +``` + +更新单个item时,使用如下方法: +```java +mAdapter.notifyItemChanged(0, "payload info"); +``` + + >**持续更新!,所以推荐Star项目** # 感谢 diff --git a/README.md b/README.md index de456df0b..93e9c0120 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ kotlin demo :[BRVAH_kotlin](https://github.com/AllenCoder/BRVAH_kotlin) -keepclassmembers class **$** extends com.chad.library.adapter.base.BaseViewHolder { (...); } +-keepattributes InnerClasses ``` # Extension library diff --git a/app/build.gradle b/app/build.gradle index 8b564492b..1863684d6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,7 +5,7 @@ android { defaultConfig { applicationId "com.chad.baserecyclerviewadapterhelper" - minSdkVersion 14 + minSdkVersion 16 targetSdkVersion 28 versionCode 5 versionName "2.1" @@ -24,9 +24,8 @@ android { } dependencies { - implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs') implementation project(path: ':library') - implementation project(':material-spinner-1.0.5') implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.appcompat:appcompat:1.0.2' diff --git a/material-spinner-1.0.5/material-spinner-1.0.5.aar b/app/libs/material-spinner-1.0.5.aar similarity index 100% rename from material-spinner-1.0.5/material-spinner-1.0.5.aar rename to app/libs/material-spinner-1.0.5.aar diff --git a/app/src/androidTest/java/com/chad/baserecyclerviewadapterhelper/ApplicationTest.java b/app/src/androidTest/java/com/chad/baserecyclerviewadapterhelper/ApplicationTest.java index c03cfebe5..20e425cd2 100644 --- a/app/src/androidTest/java/com/chad/baserecyclerviewadapterhelper/ApplicationTest.java +++ b/app/src/androidTest/java/com/chad/baserecyclerviewadapterhelper/ApplicationTest.java @@ -1,16 +1,8 @@ package com.chad.baserecyclerviewadapterhelper; -import android.app.Application; -import android.test.ApplicationTestCase; - /** * Testing Fundamentals */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } - public void test() { - - } +public class ApplicationTest { + } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9a9b1a294..0e54457fc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,6 +36,7 @@ android:launchMode="singleTask"> + \ No newline at end of file diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/AnimationUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/AnimationUseActivity.java index a8e83267d..479762fa2 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/AnimationUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/AnimationUseActivity.java @@ -24,8 +24,6 @@ public class AnimationUseActivity extends Activity { private RecyclerView mRecyclerView; private AnimationAdapter mAnimationAdapter; - private ImageView mImgBtn; - private int mFirstPageItemCount = 3; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) { private void initView() { - mImgBtn = (ImageView) findViewById(R.id.img_back); + ImageView mImgBtn = (ImageView) findViewById(R.id.img_back); mImgBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { @@ -53,6 +51,7 @@ public void onClick(final View v) { private void initAdapter() { mAnimationAdapter = new AnimationAdapter(); mAnimationAdapter.openLoadAnimation(); + int mFirstPageItemCount = 3; mAnimationAdapter.setNotDoAnimationCount(mFirstPageItemCount); mAnimationAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { @Override @@ -73,6 +72,8 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).show(); // you have set clickspan .so there should not solve any click event ,just empty break; + default: + break; } } @@ -112,7 +113,8 @@ public void onItemSelected(MaterialSpinner view, int position, long id, String i mRecyclerView.setAdapter(mAnimationAdapter); } }); - mAnimationAdapter.isFirstOnly(false);//init firstOnly state + //init firstOnly state + mAnimationAdapter.isFirstOnly(false); SwitchButton switchButton = (SwitchButton) findViewById(R.id.switch_button); switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/DiffUtilActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/DiffUtilActivity.java new file mode 100644 index 000000000..7e6ed1ed6 --- /dev/null +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/DiffUtilActivity.java @@ -0,0 +1,135 @@ +package com.chad.baserecyclerviewadapterhelper; + +import android.os.Bundle; + +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import androidx.recyclerview.widget.RecyclerView; + +import com.chad.baserecyclerviewadapterhelper.adapter.diffUtil.DiffDemoCallback; +import com.chad.baserecyclerviewadapterhelper.adapter.diffUtil.DiffUtilAdapter; +import com.chad.baserecyclerviewadapterhelper.base.BaseActivity; +import com.chad.baserecyclerviewadapterhelper.data.DataServer; +import com.chad.baserecyclerviewadapterhelper.entity.DiffUtilDemoEntity; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by limuyang + * Date: 2019/7/14 + */ +public class DiffUtilActivity extends BaseActivity { + private RecyclerView mRecyclerView; + private Button itemChangeBtn; + private Button notifyChangeBtn; + + private DiffUtilAdapter mAdapter; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_diffutil); + setBackBtn(); + setTitle("DiffUtil Use"); + + findView(); + initRv(); + initClick(); + } + + private void findView() { + mRecyclerView = findViewById(R.id.diff_rv); + itemChangeBtn = findViewById(R.id.item_change_btn); + notifyChangeBtn = findViewById(R.id.notify_change_btn); + } + + private void initRv() { + mAdapter = new DiffUtilAdapter(DataServer.getDiffUtilDemoEntities()); + mAdapter.bindToRecyclerView(mRecyclerView); + + View view = getLayoutInflater().inflate(R.layout.head_view, mRecyclerView, false); + view.findViewById(R.id.iv).setVisibility(View.GONE); + mAdapter.addHeaderView(view); + } + + private void initClick() { + itemChangeBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + DiffDemoCallback callback = new DiffDemoCallback(getNewList()); + mAdapter.setNewDiffData(callback); + } + }); + + notifyChangeBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // change item 0 + mAdapter.getData().set(0, new DiffUtilDemoEntity( + 1, + "😊😊Item " + 0, + "Item " + 0 + " content have change (notifyItemChanged)", + "06-12")); + mAdapter.notifyItemChanged(0 + mAdapter.getHeaderLayoutCount(), DiffUtilAdapter.ITEM_0_PAYLOAD); + } + }); + } + + + /** + * get new data + * + * @return + */ + private List getNewList() { + List list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + /* + Simulate deletion of data No. 1 and No. 3 + 模拟删除1号和3号数据 + */ + if (i == 1 || i == 3) continue; + + /* + Simulate modification title of data No. 0 + 模拟修改0号数据的title + */ + if (i == 0) { + list.add(new DiffUtilDemoEntity( + i, + "😊Item " + i, + "This item " + i + " content", + "06-12") + ); + continue; + } + + /* + Simulate modification content of data No. 4 + 模拟修改4号数据的content发生变化 + */ + if (i == 4) { + list.add(new DiffUtilDemoEntity( + i, + "Item " + i, + "Oh~~~~~~, Item " + i + " content have change", + "06-12") + ); + continue; + } + + list.add(new DiffUtilDemoEntity( + i, + "Item " + i, + "This item " + i + " content", + "06-12") + ); + } + return list; + } +} diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/EmptyViewUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/EmptyViewUseActivity.java index 57163bdf5..6bbf066e4 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/EmptyViewUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/EmptyViewUseActivity.java @@ -16,7 +16,8 @@ public class EmptyViewUseActivity extends BaseActivity implements View.OnClickLi private QuickAdapter mQuickAdapter; private View notDataView; private View errorView; - + private boolean mError = true; + private boolean mNoData = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -59,8 +60,7 @@ public void onClick(View v) { onRefresh(); } - private boolean mError = true; - private boolean mNoData = true; + private void onRefresh() { mQuickAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) mRecyclerView.getParent()); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java index 3bacfdeb6..87f1b6d14 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ExpandableUseActivity.java @@ -18,9 +18,9 @@ * https://github.com/CymChad/BaseRecyclerViewAdapterHelper */ public class ExpandableUseActivity extends BaseActivity { - RecyclerView mRecyclerView; - ExpandableItemAdapter adapter; - ArrayList list; + private RecyclerView mRecyclerView; + private ExpandableItemAdapter adapter; + private ArrayList list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -67,7 +67,7 @@ private ArrayList generateData() { } res.add(lv0); } - res.add(new Level0Item("This is " + lv0Count + "th item in Level 0", "subtitle of " + lv0Count)); + return res; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/HomeActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/HomeActivity.java index e675ff593..6837a11e6 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/HomeActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/HomeActivity.java @@ -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,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 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, DiffUtilActivity.class}; + private static final String[] TITLE = {"Animation", "MultipleItem", "Header/Footer", "PullToRefresh", "Section", "EmptyView", "DragAndSwipe", "ItemClick", "ExpandableItem", "DataBinding", "UpFetchData", "SectionMultipleItem", "DiffUtil"}; + 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, R.mipmap.gv_databinding}; private ArrayList mDataList; private RecyclerView mRecyclerView; diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemClickActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemClickActivity.java index f00fe7f8a..d03d0fb1c 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemClickActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemClickActivity.java @@ -16,11 +16,13 @@ import java.util.ArrayList; import java.util.List; +/** + * @author Allen + */ public class ItemClickActivity extends BaseActivity { private RecyclerView mRecyclerView; private ItemClickAdapter adapter; - private static final int PAGE_SIZE = 10; private static String TAG = "ItemClickActivity"; @Override @@ -29,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) { setBackBtn(); setTitle("ItemClickActivity Activity"); setContentView(R.layout.activity_item_click); - mRecyclerView = (RecyclerView) findViewById(R.id.list); + mRecyclerView = findViewById(R.id.list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); initAdapter(); adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java index 3fc1e45f0..4690996da 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java @@ -28,11 +28,6 @@ */ public class ItemDragAndSwipeUseActivity extends BaseActivity { private static final String TAG = ItemDragAndSwipeUseActivity.class.getSimpleName(); - private RecyclerView mRecyclerView; - private List mData; - private ItemDragAdapter mAdapter; - private ItemTouchHelper mItemTouchHelper; - private ItemDragAndSwipeCallback mItemDragAndSwipeCallback; @Override protected void onCreate(Bundle savedInstanceState) { @@ -40,9 +35,9 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_item_touch_use); setBackBtn(); setTitle("ItemDrag And Swipe"); - mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); + RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - mData = generateData(50); + List mData = generateData(50); OnItemDragListener listener = new OnItemDragListener() { @Override public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) { @@ -94,9 +89,9 @@ public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, } }; - mAdapter = new ItemDragAdapter(mData); - mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter); - mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback); + ItemDragAdapter mAdapter = new ItemDragAdapter(mData); + ItemDragAndSwipeCallback mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter); + ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback); mItemTouchHelper.attachToRecyclerView(mRecyclerView); //mItemDragAndSwipeCallback.setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/MultipleItemUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/MultipleItemUseActivity.java index a6c89d339..b95786627 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/MultipleItemUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/MultipleItemUseActivity.java @@ -16,9 +16,9 @@ /** * https://github.com/CymChad/BaseRecyclerViewAdapterHelper * modify by AllenCoder + * @author ChayChan */ public class MultipleItemUseActivity extends BaseActivity { - private RecyclerView mRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { @@ -26,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_multiple_item_use); setTitle("MultipleItem Use"); setBackBtn(); - mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); + RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); final List data = DataServer.getMultipleItemData(); final MultipleItemQuickAdapter multipleItemAdapter = new MultipleItemQuickAdapter(this, data); final GridLayoutManager manager = new GridLayoutManager(this, 4); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionMultipleItemUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionMultipleItemUseActivity.java index 2155ea7b7..0ef7c1f78 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionMultipleItemUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionMultipleItemUseActivity.java @@ -20,8 +20,6 @@ * 2.create adapter which extend BaseSectionMultiItemQuickAdapter */ public class SectionMultipleItemUseActivity extends BaseActivity { - private RecyclerView mRecyclerView; - private List mData; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,11 +27,11 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_section_uer); setBackBtn(); setTitle("SectionMultiple Use"); - mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); + RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // 1. create entityList which item data extend SectionMultiEntity - mData = DataServer.getSectionMultiData(); + List mData = DataServer.getSectionMultiData(); // create adapter which extend BaseSectionMultiItemQuickAdapter provide your headerResId SectionMultipleItemAdapter sectionAdapter = new SectionMultipleItemAdapter(R.layout.def_section_head, mData); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionUseActivity.java index 0fb59b520..d9a2250fc 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/SectionUseActivity.java @@ -29,8 +29,8 @@ protected void onCreate(Bundle savedInstanceState) { setBackBtn(); setTitle("Section Use"); mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); - mRecyclerView.setLayoutManager(new GridLayoutManager(this,2)); - mRecyclerView.addItemDecoration(new GridSectionAverageGapItemDecoration(50,20,20,20)); + mRecyclerView.setLayoutManager(new GridLayoutManager(this,3)); + mRecyclerView.addItemDecoration(new GridSectionAverageGapItemDecoration(10,10,20,15)); mData = DataServer.getSampleData(); SectionAdapter sectionAdapter = new SectionAdapter(R.layout.item_section_content, R.layout.def_section_head, mData); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/UpFetchUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/UpFetchUseActivity.java index 92fe89263..6d4f87754 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/UpFetchUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/UpFetchUseActivity.java @@ -20,8 +20,8 @@ */ public class UpFetchUseActivity extends BaseActivity { - RecyclerView mRecyclerView; - UpFetchAdapter mAdapter; + private RecyclerView mRecyclerView; + private UpFetchAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/AnimationAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/AnimationAdapter.java index 99fd12b72..33d2a5813 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/AnimationAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/AnimationAdapter.java @@ -31,8 +31,7 @@ public AnimationAdapter() { @Override protected void convert(BaseViewHolder helper, Status item) { helper.addOnClickListener(R.id.img).addOnClickListener(R.id.tweetName); - switch (helper.getLayoutPosition() % - 3) { + switch (helper.getLayoutPosition() % 3) { case 0: helper.setImageResource(R.id.img, R.mipmap.animation_img1); break; @@ -42,6 +41,8 @@ protected void convert(BaseViewHolder helper, Status item) { case 2: helper.setImageResource(R.id.img, R.mipmap.animation_img3); break; + default: + break; } helper.setText(R.id.tweetName, "Hoteis in Rio de Janeiro"); String msg = "\"He was one of Australia's most of distinguished artistes, renowned for his portraits\""; @@ -52,7 +53,7 @@ protected void convert(BaseViewHolder helper, Status item) { ((TextView) helper.getView(R.id.tweetText)).setLongClickable(false); } - ClickableSpan clickableSpan = new ClickableSpan() { + private ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { ToastUtils.showShortToast("事件触发了 landscapes and nedes"); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/DataBindingUseAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/DataBindingUseAdapter.java index f24c3060e..43de46c1a 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/DataBindingUseAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/DataBindingUseAdapter.java @@ -34,14 +34,15 @@ protected void convert(MovieViewHolder helper, Movie item) { binding.setVariable(BR.movie, item); binding.setVariable(BR.presenter, mPresenter); binding.executePendingBindings(); - switch (helper.getLayoutPosition() % - 2) { + switch (helper.getLayoutPosition() % 2) { case 0: helper.setImageResource(R.id.iv, R.mipmap.m_img1); break; case 1: helper.setImageResource(R.id.iv, R.mipmap.m_img2); break; + default: + break; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java index d8094d19b..515682889 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ExpandableItemAdapter.java @@ -2,6 +2,7 @@ import android.util.Log; import android.view.View; + import com.chad.baserecyclerviewadapterhelper.R; import com.chad.baserecyclerviewadapterhelper.entity.Level0Item; import com.chad.baserecyclerviewadapterhelper.entity.Level1Item; @@ -10,6 +11,7 @@ import com.chad.library.adapter.base.BaseViewHolder; import com.chad.library.adapter.base.entity.IExpandable; import com.chad.library.adapter.base.entity.MultiItemEntity; + import java.util.List; /** @@ -40,8 +42,7 @@ public ExpandableItemAdapter(List data) { protected void convert(final BaseViewHolder holder, final MultiItemEntity item) { switch (holder.getItemViewType()) { case TYPE_LEVEL_0: - switch (holder.getLayoutPosition() % - 3) { + switch (holder.getLayoutPosition() % 3) { case 0: holder.setImageResource(R.id.iv_head, R.mipmap.head_img0); break; @@ -51,6 +52,8 @@ protected void convert(final BaseViewHolder holder, final MultiItemEntity item) case 2: holder.setImageResource(R.id.iv_head, R.mipmap.head_img2); break; + default: + break; } final Level0Item lv0 = (Level0Item) item; holder.setText(R.id.title, lv0.title) @@ -127,6 +130,8 @@ public void onClick(View view) { } }); break; + default: + break; } } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/HeaderAndFooterAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/HeaderAndFooterAdapter.java index 3c49c38ab..7d9287dc2 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/HeaderAndFooterAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/HeaderAndFooterAdapter.java @@ -17,16 +17,18 @@ public HeaderAndFooterAdapter(int dataSize) { @Override protected void convert(BaseViewHolder helper, Status item) { - switch (helper.getLayoutPosition()% - 3){ + switch (helper.getLayoutPosition() % + 3) { case 0: - helper.setImageResource(R.id.iv,R.mipmap.animation_img1); + helper.setImageResource(R.id.iv, R.mipmap.animation_img1); break; case 1: - helper.setImageResource(R.id.iv,R.mipmap.animation_img2); + helper.setImageResource(R.id.iv, R.mipmap.animation_img2); break; case 2: - helper.setImageResource(R.id.iv,R.mipmap.animation_img3); + helper.setImageResource(R.id.iv, R.mipmap.animation_img3); + break; + default: break; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemClickAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemClickAdapter.java index f156db6e9..f28b096b3 100755 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemClickAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemClickAdapter.java @@ -19,7 +19,7 @@ * */ public class ItemClickAdapter extends BaseMultiItemQuickAdapter implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener { - NestAdapter nestAdapter; + private NestAdapter nestAdapter; public ItemClickAdapter(List data) { super(data); @@ -51,9 +51,10 @@ protected void convert(final BaseViewHolder helper, final ClickEntity item) { .addOnClickListener(R.id.iv_num_reduce).addOnClickListener(R.id.iv_num_add); break; case ClickEntity.NEST_CLICK_ITEM_CHILD_VIEW: - helper.setNestView(R.id.item_click); // u can set nestview id + // u can set nestview id + helper.setNestView(R.id.item_click); final RecyclerView recyclerView = helper.getView(R.id.nest_list); - recyclerView.setLayoutManager(new LinearLayoutManager(helper.itemView.getContext(), RecyclerView.VERTICAL, false)); + recyclerView.setLayoutManager(new LinearLayoutManager(helper.itemView.getContext(), LinearLayoutManager.VERTICAL, false)); recyclerView.setHasFixedSize(true); nestAdapter = new NestAdapter(); @@ -61,6 +62,8 @@ protected void convert(final BaseViewHolder helper, final ClickEntity item) { nestAdapter.setOnItemChildClickListener(this); recyclerView.setAdapter(nestAdapter); break; + default: + break; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemDragAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemDragAdapter.java index 613bdb463..15c3465b8 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemDragAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/ItemDragAdapter.java @@ -16,8 +16,7 @@ public ItemDragAdapter(List data) { @Override protected void convert(BaseViewHolder helper, String item) { - switch (helper.getLayoutPosition() % - 3) { + switch (helper.getLayoutPosition() % 3) { case 0: helper.setImageResource(R.id.iv_head, R.mipmap.head_img0); break; @@ -27,6 +26,8 @@ protected void convert(BaseViewHolder helper, String item) { case 2: helper.setImageResource(R.id.iv_head, R.mipmap.head_img2); break; + default: + break; } helper.setText(R.id.tv, item); } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/MultipleItemQuickAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/MultipleItemQuickAdapter.java index fdf54f7df..a77ec063b 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/MultipleItemQuickAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/MultipleItemQuickAdapter.java @@ -29,17 +29,20 @@ protected void convert(BaseViewHolder helper, MultipleItem item) { helper.setText(R.id.tv, item.getContent()); break; case MultipleItem.IMG_TEXT: - switch (helper.getLayoutPosition() % - 2) { + 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; + default: + break; } break; + default: + break; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/NestAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/NestAdapter.java index 4d452902c..9b0e74f7a 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/NestAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/NestAdapter.java @@ -25,31 +25,32 @@ */ public class NestAdapter extends BaseQuickAdapter { public NestAdapter() { - super( R.layout.layout_nest_item, DataServer.getSampleData(20)); + super(R.layout.layout_nest_item, DataServer.getSampleData(20)); } @Override protected void convert(BaseViewHolder helper, Status item) { helper.addOnClickListener(R.id.tweetText); - switch (helper.getLayoutPosition()% - 3){ + switch (helper.getLayoutPosition() % 3) { case 0: - helper.setImageResource(R.id.img,R.mipmap.animation_img1); + helper.setImageResource(R.id.img, R.mipmap.animation_img1); break; case 1: - helper.setImageResource(R.id.img,R.mipmap.animation_img2); + helper.setImageResource(R.id.img, R.mipmap.animation_img2); break; case 2: - helper.setImageResource(R.id.img,R.mipmap.animation_img3); + helper.setImageResource(R.id.img, R.mipmap.animation_img3); + break; + default: break; } - helper.setText(R.id.tweetName,"Hoteis in Rio de Janeiro"); - String msg="\"He was one of Australia's most of distinguished artistes, renowned for his portraits\""; - ( (TextView)helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create()); - ( (TextView)helper.getView(R.id.tweetText)).setMovementMethod(LinkMovementMethod.getInstance()); + helper.setText(R.id.tweetName, "Hoteis in Rio de Janeiro"); + String msg = "\"He was one of Australia's most of distinguished artistes, renowned for his portraits\""; + ((TextView) helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create()); + ((TextView) helper.getView(R.id.tweetText)).setMovementMethod(LinkMovementMethod.getInstance()); } - ClickableSpan clickableSpan = new ClickableSpan() { + private ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { ToastUtils.showShortToast("事件触发了 landscapes and nedes"); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/PullToRefreshAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/PullToRefreshAdapter.java index ffd564d79..27e59dc7a 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/PullToRefreshAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/PullToRefreshAdapter.java @@ -24,27 +24,28 @@ */ public class PullToRefreshAdapter extends BaseQuickAdapter { public PullToRefreshAdapter() { - super( R.layout.layout_animation, null); + super(R.layout.layout_animation, null); } @Override protected void convert(BaseViewHolder helper, Status item) { - switch (helper.getLayoutPosition()% - 3){ + switch (helper.getLayoutPosition() % 3) { case 0: - helper.setImageResource(R.id.img,R.mipmap.animation_img1); + helper.setImageResource(R.id.img, R.mipmap.animation_img1); break; case 1: - helper.setImageResource(R.id.img,R.mipmap.animation_img2); + helper.setImageResource(R.id.img, R.mipmap.animation_img2); break; case 2: - helper.setImageResource(R.id.img,R.mipmap.animation_img3); + helper.setImageResource(R.id.img, R.mipmap.animation_img3); + break; + default: break; } - helper.setText(R.id.tweetName,"Hoteis in Rio de Janeiro"); - String msg="\"He was one of Australia's most of distinguished artistes, renowned for his portraits\""; - ( (TextView)helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create()); - ( (TextView)helper.getView(R.id.tweetText)).setMovementMethod(LinkMovementMethod.getInstance()); + helper.setText(R.id.tweetName, "Hoteis in Rio de Janeiro"); + String msg = "\"He was one of Australia's most of distinguished artistes, renowned for his portraits\""; + ((TextView) helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create()); + ((TextView) helper.getView(R.id.tweetText)).setMovementMethod(LinkMovementMethod.getInstance()); } ClickableSpan clickableSpan = new ClickableSpan() { diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/QuickAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/QuickAdapter.java index a5caa31cb..9bfc4528f 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/QuickAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/QuickAdapter.java @@ -17,8 +17,7 @@ public QuickAdapter(int dataSize) { @Override protected void convert(BaseViewHolder helper, Status item) { - switch (helper.getLayoutPosition() % - 3) { + switch (helper.getLayoutPosition() % 3) { case 0: helper.setImageResource(R.id.img, R.mipmap.animation_img1); break; @@ -28,6 +27,8 @@ protected void convert(BaseViewHolder helper, Status item) { case 2: helper.setImageResource(R.id.img, R.mipmap.animation_img3); break; + default: + break; } helper.setText(R.id.tweetName, "Hoteis in Rio de Janeiro"); helper.setText(R.id.tweetText, "O ever youthful,O ever weeping"); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionAdapter.java index 60d4d94ef..eee52c3c6 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionAdapter.java @@ -5,7 +5,6 @@ import com.chad.baserecyclerviewadapterhelper.entity.Video; import com.chad.library.adapter.base.BaseSectionQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; -import com.chad.library.adapter.base.listener.OnItemChildClickListener; import java.util.List; @@ -36,14 +35,15 @@ protected void convertHead(BaseViewHolder helper, final MySection item) { @Override protected void convert(BaseViewHolder helper, MySection item) { Video video = (Video) item.t; - switch (helper.getLayoutPosition() % - 2) { + switch (helper.getLayoutPosition() % 2) { case 0: helper.setImageResource(R.id.iv, R.mipmap.m_img1); break; case 1: helper.setImageResource(R.id.iv, R.mipmap.m_img2); break; + default: + break; } helper.setText(R.id.tv, video.getName()); diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionMultipleItemAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionMultipleItemAdapter.java index e47e4944a..d950829ab 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionMultipleItemAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/SectionMultipleItemAdapter.java @@ -50,7 +50,10 @@ protected void convert(BaseViewHolder helper, SectionMultipleItem item) { case 1: helper.setImageResource(R.id.iv, R.mipmap.animation_img2); break; + default: + break; } + default: break; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/diffUtil/DiffDemoCallback.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/diffUtil/DiffDemoCallback.java new file mode 100644 index 000000000..87c6a444a --- /dev/null +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/diffUtil/DiffDemoCallback.java @@ -0,0 +1,76 @@ +package com.chad.baserecyclerviewadapterhelper.adapter.diffUtil; + + + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.chad.baserecyclerviewadapterhelper.entity.DiffUtilDemoEntity; +import com.chad.library.adapter.base.diff.BaseQuickDiffCallback; + +import java.util.List; + +/** + * Create DiffCallback + */ +public class DiffDemoCallback extends BaseQuickDiffCallback { + + public DiffDemoCallback(@Nullable List newList) { + super(newList); + } + + /** + * Determine if it is the same item + *

+ * 判断是否是同一个item + * + * @param oldItem New data + * @param newItem old Data + * @return + */ + @Override + protected boolean areItemsTheSame(@NonNull DiffUtilDemoEntity oldItem, @NonNull DiffUtilDemoEntity newItem) { + return oldItem.getId() == newItem.getId(); + } + + /** + * When it is the same item, judge whether the content has changed. + *

+ * 当是同一个item时,再判断内容是否发生改变 + * + * @param oldItem New data + * @param newItem old Data + * @return + */ + @Override + protected boolean areContentsTheSame(@NonNull DiffUtilDemoEntity oldItem, @NonNull DiffUtilDemoEntity newItem) { + return oldItem.getTitle().equals(newItem.getTitle()) + && oldItem.getContent().equals(newItem.getContent()) + && oldItem.getDate().equals(newItem.getDate()); + } + + /** + * Optional implementation + * Implement this method if you need to precisely modify the content of a view. + * If this method is not implemented, or if null is returned, the entire item will be refreshed. + * + * 可选实现 + * 如果需要精确修改某一个view中的内容,请实现此方法。 + * 如果不实现此方法,或者返回null,将会直接刷新整个item。 + * + * @param oldItem Old data + * @param newItem New data + * @return Payload info. if return null, the entire item will be refreshed. + */ + @Override + protected Object getChangePayload(@NonNull DiffUtilDemoEntity oldItem, @NonNull DiffUtilDemoEntity newItem) { + if (!oldItem.getTitle().equals(newItem.getTitle())) { + // if only title change(如果标题变化了) + return DiffUtilAdapter.TITLE_PAYLOAD; + } else if (!oldItem.getContent().equals(newItem.getContent())) { + // if only content change(如果内容变化了) + return DiffUtilAdapter.CONTENT_PAYLOAD; + } + return null; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/diffUtil/DiffUtilAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/diffUtil/DiffUtilAdapter.java new file mode 100644 index 000000000..90798b36a --- /dev/null +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/adapter/diffUtil/DiffUtilAdapter.java @@ -0,0 +1,55 @@ +package com.chad.baserecyclerviewadapterhelper.adapter.diffUtil; + + +import androidx.annotation.NonNull; + +import com.chad.baserecyclerviewadapterhelper.R; +import com.chad.baserecyclerviewadapterhelper.entity.DiffUtilDemoEntity; +import com.chad.library.adapter.base.BaseQuickAdapter; +import com.chad.library.adapter.base.BaseViewHolder; + +import java.util.List; + +/** + * Create adapter + */ +public class DiffUtilAdapter extends BaseQuickAdapter { + public static final int TITLE_PAYLOAD = 899; + public static final int CONTENT_PAYLOAD = 900; + public static final int ITEM_0_PAYLOAD = 901; + + public DiffUtilAdapter(List list) { + super(R.layout.layout_animation, list); + } + + @Override + protected void convert(BaseViewHolder helper, DiffUtilDemoEntity item) { + helper.setText(R.id.tweetName, item.getTitle()) + .setText(R.id.tweetText, item.getContent()) + .setText(R.id.tweetDate, item.getDate()); + } + + /** + * This method will only be executed when there is payload info + * + * 当有 payload info 时,只会执行此方法 + * + * @param helper A fully initialized helper. + * @param item The item that needs to be displayed. + * @param payloads payload info. + */ + @Override + protected void convertPayloads(@NonNull BaseViewHolder helper, DiffUtilDemoEntity item, @NonNull List payloads) { + for (Object p : payloads) { + int payload = (int) p; + if (payload == TITLE_PAYLOAD) { + helper.setText(R.id.tweetName, item.getTitle()); + } else if (payload == CONTENT_PAYLOAD) { + helper.setText(R.id.tweetText, item.getContent()); + } else if (payload == ITEM_0_PAYLOAD) { + helper.setText(R.id.tweetName, item.getTitle()) + .setText(R.id.tweetText, item.getContent()); + } + } + } +} diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseActivity.java index 1e54dc9e4..932309bf3 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseActivity.java @@ -29,6 +29,7 @@ public class BaseActivity extends AppCompatActivity { private TextView title; private ImageView back; protected final String TAG = this.getClass().getSimpleName(); + private LinearLayout rootLayout; protected void setTitle(String msg) { if (title != null) { @@ -48,7 +49,7 @@ public void onClick(View v) { finish(); } }); - }else { + } else { Logger.t(TAG).e("back is null ,please check out"); } @@ -58,13 +59,12 @@ protected void setBackClickListener(View.OnClickListener l) { if (back != null) { back.setVisibility(View.VISIBLE); back.setOnClickListener(l); - }else { + } else { Logger.t(TAG).e("back is null ,please check out"); } } - private LinearLayout rootLayout; @Override protected void onCreate(Bundle savedInstanceState) { @@ -103,7 +103,9 @@ public void setContentView(int layoutId) { @Override public void setContentView(View view) { rootLayout = (LinearLayout) findViewById(R.id.root_layout); - if (rootLayout == null) return; + if (rootLayout == null) { + return; + } rootLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); initToolbar(); } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseBindingViewHolder.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseBindingViewHolder.java index 47e139c22..588bfca01 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseBindingViewHolder.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseBindingViewHolder.java @@ -6,23 +6,23 @@ import com.chad.library.adapter.base.BaseViewHolder; /** - * Created by tysheng * Date: 2017/5/11 14:42. * Email: tyshengsx@gmail.com + * @author tysheng */ -public class BaseBindingViewHolder extends BaseViewHolder { - private Binding mBinding; +public class BaseBindingViewHolder extends BaseViewHolder { + private B mB; public BaseBindingViewHolder(View view) { super(view); } - public Binding getBinding() { - return mBinding; + public B getBinding() { + return mB; } - public void setBinding(Binding binding) { - mBinding = binding; + public void setBinding(B b) { + mB = b; } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseDataBindingAdapter.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseDataBindingAdapter.java index ab0932f2b..0a04e7155 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseDataBindingAdapter.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/base/BaseDataBindingAdapter.java @@ -18,7 +18,7 @@ * Email: tyshengsx@gmail.com */ -public abstract class BaseDataBindingAdapter extends BaseQuickAdapter> { +public abstract class BaseDataBindingAdapter extends BaseQuickAdapter> { public BaseDataBindingAdapter(@LayoutRes int layoutResId, @Nullable List data) { @@ -34,29 +34,29 @@ public BaseDataBindingAdapter(@LayoutRes int layoutResId) { } @Override - protected BaseBindingViewHolder createBaseViewHolder(View view) { + protected BaseBindingViewHolder createBaseViewHolder(View view) { return new BaseBindingViewHolder<>(view); } @Override - protected BaseBindingViewHolder createBaseViewHolder(ViewGroup parent, int layoutResId) { - Binding binding = DataBindingUtil.inflate(mLayoutInflater, layoutResId, parent, false); + protected BaseBindingViewHolder createBaseViewHolder(ViewGroup parent, int layoutResId) { + B b = DataBindingUtil.inflate(mLayoutInflater, layoutResId, parent, false); View view; - if (binding == null) { + if (b == null) { view = getItemView(layoutResId, parent); } else { - view = binding.getRoot(); + view = b.getRoot(); } - BaseBindingViewHolder holder = new BaseBindingViewHolder<>(view); - holder.setBinding(binding); + BaseBindingViewHolder holder = new BaseBindingViewHolder<>(view); + holder.setBinding(b); return holder; } @Override - protected void convert(BaseBindingViewHolder helper, T item) { + protected void convert(BaseBindingViewHolder helper, T item) { convert(helper.getBinding(), item); helper.getBinding().executePendingBindings(); } - protected abstract void convert(Binding binding, T item); + protected abstract void convert(B b, T item); } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/data/DataServer.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/data/DataServer.java index 67ae1d228..89b0124ec 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/data/DataServer.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/data/DataServer.java @@ -1,6 +1,7 @@ package com.chad.baserecyclerviewadapterhelper.data; +import com.chad.baserecyclerviewadapterhelper.entity.DiffUtilDemoEntity; import com.chad.baserecyclerviewadapterhelper.entity.MultipleItem; import com.chad.baserecyclerviewadapterhelper.entity.MySection; import com.chad.baserecyclerviewadapterhelper.entity.NormalMultipleEntity; @@ -152,5 +153,16 @@ public static List getNormalMultipleEntities() { // return list; // } - + public static List getDiffUtilDemoEntities() { + List list = new ArrayList<>(); + for (int i = 0; i < 10; i++){ + list.add(new DiffUtilDemoEntity( + i, + "Item " + i, + "This item " + i + " content", + "06-12") + ); + } + return list; + } } diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionAverageGapItemDecoration.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionAverageGapItemDecoration.java index 241572c9c..ccacef72e 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionAverageGapItemDecoration.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionAverageGapItemDecoration.java @@ -2,29 +2,52 @@ import android.graphics.Rect; import android.os.Build; + +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.View; -import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.BaseSectionQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; import com.chad.library.adapter.base.entity.SectionEntity; -import androidx.recyclerview.widget.GridLayoutManager; -import androidx.recyclerview.widget.RecyclerView; +import java.util.ArrayList; +import java.util.List; /** * 应用于RecyclerView的GridLayoutManager,水平方向上固定间距大小,从而使条目宽度自适应。
* 配合Brvah的Section使用,不对Head生效,仅对每个Head的子Grid列表生效
- *Section Grid中Item的宽度应设为MATCH_PARAENT + * Section Grid中Item的宽度应设为MATCH_PARAENT * * @author : renpeng - * @org :Aurora Team * @since : 2018/9/29 */ public class GridSectionAverageGapItemDecoration extends RecyclerView.ItemDecoration { + private class Section { + public int startPos = 0; + public int endPos = 0; + + public int getCount() { + return endPos - startPos + 1; + } + + public boolean contains(int pos) { + return pos >= startPos && pos <= endPos; + } + + @Override + public String toString() { + return "Section{" + + "startPos=" + startPos + + ", endPos=" + endPos + + '}'; + } + } + private float gapHorizontalDp; private float gapVerticalDp; private float sectionEdgeHPaddingDp; @@ -34,17 +57,46 @@ public class GridSectionAverageGapItemDecoration extends RecyclerView.ItemDecora private int sectionEdgeHPaddingPx; private int eachItemHPaddingPx; //每个条目应该在水平方向上加的padding 总大小,即=paddingLeft+paddingRight private int sectionEdgeVPaddingPx; - private Rect preRect = new Rect(); - private boolean isPreItemHeader = false; - private int sectionStartItemPos = 0; - private int sectionEndItemPos = 0; - private int sectionItemCount = 0; + private List
mSectionList = new ArrayList<>(); + private BaseSectionQuickAdapter mAdapter; + private RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() { + @Override + public void onChanged() { + markSections(); + } + + @Override + public void onItemRangeChanged(int positionStart, int itemCount) { + markSections(); + } + + @Override + public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) { + markSections(); + } + + @Override + public void onItemRangeInserted(int positionStart, int itemCount) { + markSections(); + } + + @Override + public void onItemRangeRemoved(int positionStart, int itemCount) { + markSections(); + } + + @Override + public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { + markSections(); + } + }; + /** - * @param gapHorizontalDp 水平间距 - * @param gapVerticalDp 垂直间距 - * @param sectionEdgeHPaddingDp 左右两端的padding大小 - * @param sectionEdgeVPaddingDp 上下两端的padding大小 + * @param gapHorizontalDp item之间的水平间距 + * @param gapVerticalDp item之间的垂直间距 + * @param sectionEdgeHPaddingDp section左右两端的padding大小 + * @param sectionEdgeVPaddingDp section上下两端的padding大小 */ public GridSectionAverageGapItemDecoration(float gapHorizontalDp, float gapVerticalDp, float sectionEdgeHPaddingDp, float sectionEdgeVPaddingDp) { this.gapHorizontalDp = gapHorizontalDp; @@ -58,24 +110,21 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle if (parent.getLayoutManager() instanceof GridLayoutManager && parent.getAdapter() instanceof BaseSectionQuickAdapter) { GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager(); BaseSectionQuickAdapter adapter = (BaseSectionQuickAdapter) parent.getAdapter(); + if (mAdapter != adapter) { + setUpWithAdapter(adapter); + } int spanCount = layoutManager.getSpanCount(); int position = parent.getChildAdapterPosition(view); SectionEntity entity = adapter.getItem(position); if (entity != null && entity.isHeader) { //不处理header - isPreItemHeader = true; - outRect.set(0,0,0,0); + outRect.set(0, 0, 0, 0); // Log.w("GridAverageGapItem", "pos=" + position + "," + outRect.toShortString()); return; } - if (isPreItemHeader) { - sectionStartItemPos = position; - sectionEndItemPos = findSectionLastItemPos(position, adapter); - sectionItemCount = sectionEndItemPos - sectionStartItemPos + 1; -// Log.w("GridAverageGapItem", "new section=" + sectionStartItemPos + "-" + sectionEndItemPos); - } + Section section = findSectionLastItemPos(position); if (gapHSizePx < 0 || gapVSizePx < 0) { transformGapDefinition(parent, spanCount); @@ -84,7 +133,7 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle outRect.bottom = 0; //下面的visualPos为单个Section内的视觉Pos - int visualPos = position + 1 - sectionStartItemPos; + int visualPos = position + 1 - section.startPos; if (visualPos % spanCount == 1) { //第一列 outRect.left = sectionEdgeHPaddingPx; @@ -94,27 +143,65 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle outRect.left = eachItemHPaddingPx - sectionEdgeHPaddingPx; outRect.right = sectionEdgeHPaddingPx; } else { - outRect.left = gapHSizePx - preRect.right; + outRect.left = gapHSizePx - (eachItemHPaddingPx - sectionEdgeHPaddingPx); outRect.right = eachItemHPaddingPx - outRect.left; } if (visualPos - spanCount <= 0) { - //每个section的第一行 + //第一行 outRect.top = sectionEdgeVPaddingPx; } - //存在即是第一行,又是最后一行的情况,故不用elseif - if (isLastRow(visualPos, spanCount, sectionItemCount)) { - //每个section的最后一行 + + if (isLastRow(visualPos, spanCount, section.getCount())) { + //最后一行 outRect.bottom = sectionEdgeVPaddingPx; +// Log.w("GridAverageGapItem", "last row pos=" + position); } - preRect = new Rect(outRect); - isPreItemHeader = false; -// Log.w("GridAverageGapItem", "pos=" + position + "," + outRect.toShortString()); +// Log.w("GridAverageGapItem", "pos=" + position + ",vPos=" + visualPos + "," + outRect.toShortString()); } else { super.getItemOffsets(outRect, view, parent, state); } } + private void setUpWithAdapter(BaseSectionQuickAdapter adapter) { + if (mAdapter != null) { + mAdapter.unregisterAdapterDataObserver(mDataObserver); + } + mAdapter = adapter; + mAdapter.registerAdapterDataObserver(mDataObserver); + markSections(); + } + + private void markSections() { + if (mAdapter != null) { + BaseSectionQuickAdapter adapter = mAdapter; + mSectionList.clear(); + SectionEntity sectionEntity = null; + Section section = new Section(); + for (int i = 0, size = adapter.getItemCount(); i < size; i++) { + sectionEntity = adapter.getItem(i); + if (sectionEntity != null && sectionEntity.isHeader) { + //找到新Section起点 + if (section != null && i != 0) { + //已经有待添加的section + section.endPos = i - 1; + mSectionList.add(section); + } + section = new Section(); + section.startPos = i + 1; + } else { + section.endPos = i; + } + } + //处理末尾情况 + if (!mSectionList.contains(section)) { + mSectionList.add(section); + } + +// Log.w("GridAverageGapItem", "section list=" + mSectionList); + } + } + private void transformGapDefinition(RecyclerView parent, int spanCount) { DisplayMetrics displayMetrics = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { @@ -127,19 +214,13 @@ private void transformGapDefinition(RecyclerView parent, int spanCount) { eachItemHPaddingPx = (sectionEdgeHPaddingPx * 2 + gapHSizePx * (spanCount - 1)) / spanCount; } - private int findSectionLastItemPos(int curPos, BaseQuickAdapter adapter) { - int count = adapter.getItemCount(); - if (count == curPos + 1) { - return curPos; - } - SectionEntity sectionEntity = null; - for (int i = curPos + 1; i < count; i++) { - sectionEntity = adapter.getItem(i); - if (sectionEntity != null && sectionEntity.isHeader) { - return i - 1; + private Section findSectionLastItemPos(int curPos) { + for (Section section : mSectionList) { + if (section.contains(curPos)) { + return section; } } - return count - 1; + return null; } private boolean isLastRow(int visualPos, int spanCount, int sectionItemCount) { diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionMultiAvgGapItemDecoration.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionMultiAvgGapItemDecoration.java new file mode 100644 index 000000000..3d1252ee5 --- /dev/null +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/decoration/GridSectionMultiAvgGapItemDecoration.java @@ -0,0 +1,291 @@ +package com.chad.baserecyclerviewadapterhelper.decoration; + +import android.graphics.Rect; +import android.os.Build; + +import android.util.DisplayMetrics; +import android.util.Log; +import android.util.LongSparseArray; +import android.util.TypedValue; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.chad.library.adapter.base.BaseSectionMultiItemQuickAdapter; +import com.chad.library.adapter.base.BaseViewHolder; +import com.chad.library.adapter.base.entity.SectionMultiEntity; + +import java.util.ArrayList; +import java.util.List; + +/** + * 应用于RecyclerView的GridLayoutManager,水平方向上固定间距大小,从而使条目宽度自适应。
+ * 配合Brvah的SectionMulti使用,不对Head生效,仅对每个Head的子Grid列表生效
+ * 使用{@link #addSectionDecoration(int, SectionDecoration)}方法来增加对应的section定义,key值对应{@link SectionMultiEntity#getItemType()}
+ * 使用{@link #setLastSectionBottomMarginDp(float)}来定义最后一个section中最后一行的底部间距,设置后会覆盖最后一个section的底部间距
+ * Section Grid中Item的宽度应设为MATCH_PARAENT
+ * + * @author : renpeng + * @since : 2019/5/28 + */ +@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) +public class GridSectionMultiAvgGapItemDecoration extends RecyclerView.ItemDecoration { + + private class Section { + public int startPos = 0; + public int endPos = 0; + + public int getCount() { + return endPos - startPos + 1; + } + + public boolean contains(int pos) { + return pos >= startPos && pos <= endPos; + } + + @Override + public String toString() { + return "Section{" + + "startPos=" + startPos + + ", endPos=" + endPos + + '}'; + } + } + + public static class SectionDecoration { + private float gapHorizontalDp; + private float gapVerticalDp; + private float sectionEdgeHPaddingDp; + private float sectionTopMarginDp; + private float sectionBottomMarginDp; + private int gapHSizePx = -1; + private int gapVSizePx = -1; + private int sectionEdgeHPaddingPx; + private int eachItemHPaddingPx; //每个条目应该在水平方向上加的padding 总大小,即=paddingLeft+paddingRight + private int sectionTopMarginPx; + private int sectionBottomMarginPx; + + /** + * @param gapHorizontalDp item之间的水平间距 + * @param gapVerticalDp item之间的垂直间距 + * @param sectionEdgeHPaddingDp section左右两端的padding大小 + * @param sectionEdgeVMarginDp section上下两端的margin大小 + */ + public SectionDecoration(float gapHorizontalDp, float gapVerticalDp, float sectionEdgeHPaddingDp, float sectionEdgeVMarginDp) { + this(gapHorizontalDp,gapVerticalDp,sectionEdgeHPaddingDp,sectionEdgeVMarginDp,sectionEdgeVMarginDp); + } + + /** + * @param gapHorizontalDp item之间的水平间距 + * @param gapVerticalDp item之间的垂直间距 + * @param sectionEdgeHPaddingDp section左右两端的padding大小 + * @param sectionTopMarginDp section的top margin大小 + * @param sectionBottomMarginDp section的bottom margin大小 + */ + public SectionDecoration(float gapHorizontalDp, float gapVerticalDp, float sectionEdgeHPaddingDp, float sectionTopMarginDp,float sectionBottomMarginDp) { + this.gapHorizontalDp = gapHorizontalDp; + this.gapVerticalDp = gapVerticalDp; + this.sectionEdgeHPaddingDp = sectionEdgeHPaddingDp; + this.sectionTopMarginDp = sectionTopMarginDp; + this.sectionBottomMarginDp = sectionBottomMarginDp; + } + + private void transformGapDefinition(RecyclerView parent, int spanCount) { + if(gapHSizePx < 0){ + DisplayMetrics displayMetrics = new DisplayMetrics(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + parent.getDisplay().getMetrics(displayMetrics); + } + gapHSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, gapHorizontalDp, displayMetrics); + gapVSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, gapVerticalDp, displayMetrics); + sectionEdgeHPaddingPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sectionEdgeHPaddingDp, displayMetrics); + sectionTopMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sectionTopMarginDp, displayMetrics); + sectionBottomMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sectionBottomMarginDp, displayMetrics); + eachItemHPaddingPx = (sectionEdgeHPaddingPx * 2 + gapHSizePx * (spanCount - 1)) / spanCount; + } + } + + } + + + private List
mSectionList = new ArrayList<>(); + private LongSparseArray sectionDecorationMap = new LongSparseArray<>(); + private BaseSectionMultiItemQuickAdapter baseMultiItemQuickAdapter; + //最后一个Section的BottomMargin,在计算最后一个seciton的底部间距时,会替换掉相应sectionDecoration中的定义 + private float mLastSectionBottomMarginDp = -1; + private int mLastSectionBottomMarginPx = -1; + private RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() { + @Override + public void onChanged() { + markSectionsForMultiSectionAdapter(); + } + + @Override + public void onItemRangeChanged(int positionStart, int itemCount) { + markSectionsForMultiSectionAdapter(); + } + + @Override + public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) { + markSectionsForMultiSectionAdapter(); + } + + @Override + public void onItemRangeInserted(int positionStart, int itemCount) { + markSectionsForMultiSectionAdapter(); + } + + @Override + public void onItemRangeRemoved(int positionStart, int itemCount) { + markSectionsForMultiSectionAdapter(); + } + + @Override + public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { + markSectionsForMultiSectionAdapter(); + } + }; + + public float getLastSectionBottomMarginDp() { + return mLastSectionBottomMarginDp; + } + + public void setLastSectionBottomMarginDp(float lastSectionBottomMarginDp) { + if(lastSectionBottomMarginDp < 0){ + throw new IllegalArgumentException("mLastSectionBottomMarginDp must >=0!"); + } + this.mLastSectionBottomMarginDp = lastSectionBottomMarginDp; + } + + public void addSectionDecoration(int type, @NonNull SectionDecoration sectionDecoration){ + if(sectionDecorationMap.get(type) != null){ + throw new RuntimeException(String.format("SectionDecoration for %d already exist!",type)); + } + sectionDecorationMap.put(type,sectionDecoration); + } + + + @Override + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { + if (parent.getAdapter() instanceof BaseSectionMultiItemQuickAdapter) { + GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager(); + BaseSectionMultiItemQuickAdapter adapter = (BaseSectionMultiItemQuickAdapter) parent.getAdapter(); + if (baseMultiItemQuickAdapter != adapter) { + setUpWithMultiItemAdapter(adapter); + } + int spanCount = layoutManager.getSpanCount(); + int position = parent.getChildAdapterPosition(view); + SectionMultiEntity entity = adapter.getItem(position); + + SectionDecoration sectionDecoration = entity != null ? sectionDecorationMap.get(entity.getItemType()) : null; + if ((entity != null && entity.isHeader) || sectionDecoration == null) { + //不处理header + outRect.set(0, 0, 0, 0); +// Log.w("GridAverageGapItem", "pos=" + position + "," + outRect.toShortString()); + return; + } + if(mLastSectionBottomMarginDp >=0){ + mLastSectionBottomMarginPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mLastSectionBottomMarginDp, parent.getResources().getDisplayMetrics()); + } + sectionDecoration.transformGapDefinition(parent,spanCount); + Section section = findSectionByPosition(position); + + outRect.top = sectionDecoration.gapVSizePx; + outRect.bottom = 0; + + //下面的visualPos为单个Section内的视觉Pos + int visualPos = position + 1 - section.startPos; + if (visualPos % spanCount == 1) { + //第一列 + outRect.left = sectionDecoration.sectionEdgeHPaddingPx; + outRect.right = sectionDecoration.eachItemHPaddingPx - sectionDecoration.sectionEdgeHPaddingPx; + } else if (visualPos % spanCount == 0) { + //最后一列 + outRect.left = sectionDecoration.eachItemHPaddingPx - sectionDecoration.sectionEdgeHPaddingPx; + outRect.right = sectionDecoration.sectionEdgeHPaddingPx; + } else { + outRect.left = sectionDecoration.gapHSizePx - (sectionDecoration.eachItemHPaddingPx - sectionDecoration.sectionEdgeHPaddingPx); + outRect.right = sectionDecoration.eachItemHPaddingPx - outRect.left; + } + + if (visualPos - spanCount <= 0) { + //第一行 + outRect.top = sectionDecoration.sectionTopMarginPx; + } + + if (isLastRow(visualPos, spanCount, section.getCount())) { + //最后一行 + if(mLastSectionBottomMarginPx >= 0 && mSectionList.indexOf(section) == mSectionList.size()-1){ + //最后一个section + outRect.bottom = mLastSectionBottomMarginPx; + }else{ + outRect.bottom = sectionDecoration.sectionBottomMarginPx; + } +// Log.w("GridAverageGapItem", "last row " + position); + } +// Log.w("GridAverageGapItem", "pos=" + position + ",vPos=" + visualPos + "," + outRect.toShortString()); + } else { + super.getItemOffsets(outRect, view, parent, state); + } + + } + + private void setUpWithMultiItemAdapter(BaseSectionMultiItemQuickAdapter adapter) { + if (baseMultiItemQuickAdapter != null) { + baseMultiItemQuickAdapter.unregisterAdapterDataObserver(mDataObserver); + } + baseMultiItemQuickAdapter = adapter; + baseMultiItemQuickAdapter.registerAdapterDataObserver(mDataObserver); + markSectionsForMultiSectionAdapter(); + } + + private void markSectionsForMultiSectionAdapter() { + if (baseMultiItemQuickAdapter != null) { + BaseSectionMultiItemQuickAdapter adapter = baseMultiItemQuickAdapter; + mSectionList.clear(); + SectionMultiEntity sectionEntity = null; + Section section = new Section(); + for (int i = 0, size = adapter.getItemCount(); i < size; i++) { + sectionEntity = adapter.getItem(i); + if (sectionEntity != null && sectionEntity.isHeader) { + //找到新Section起点 + if (section != null && i != 0 && section.startPos != i) { + //已经有待添加的section + section.endPos = i - 1; + mSectionList.add(section); + } + section = new Section(); + section.startPos = i + 1; + } else { + section.endPos = i; + } + } + //处理末尾情况 + if (!mSectionList.contains(section)) { + mSectionList.add(section); + } + + Log.w("GridAverageGapItem", "section list=" + mSectionList); + } + } + + + private Section findSectionByPosition(int curPos) { + for (Section section : mSectionList) { + if (section.contains(curPos)) { + return section; + } + } + return null; + } + + private boolean isLastRow(int visualPos, int spanCount, int sectionItemCount) { + int lastRowCount = sectionItemCount % spanCount; + lastRowCount = lastRowCount == 0 ? spanCount : lastRowCount; + return visualPos > sectionItemCount - lastRowCount; + } +} diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/entity/DiffUtilDemoEntity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/entity/DiffUtilDemoEntity.java new file mode 100644 index 000000000..3a4bf9064 --- /dev/null +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/entity/DiffUtilDemoEntity.java @@ -0,0 +1,48 @@ +package com.chad.baserecyclerviewadapterhelper.entity; + +public class DiffUtilDemoEntity { + + private int id; + private String title; + private String content; + private String date; + + public DiffUtilDemoEntity(int id, String title, String content, String date) { + this.id = id; + this.title = title; + this.content = content; + this.date = date; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } +} diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/util/ToastUtils.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/util/ToastUtils.java index 262299e81..2012f30db 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/util/ToastUtils.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/util/ToastUtils.java @@ -5,16 +5,17 @@ import androidx.annotation.StringRes; import android.widget.Toast; +/** + * @author Allen + */ public class ToastUtils { - private ToastUtils() { - throw new UnsupportedOperationException("u can't instantiate me..."); - } - private static Toast sToast; private static Handler sHandler = new Handler(Looper.getMainLooper()); private static boolean isJumpWhenMore; - + private ToastUtils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } /** * 吐司初始化 * diff --git a/app/src/main/res/layout/activity_diffutil.xml b/app/src/main/res/layout/activity_diffutil.xml new file mode 100644 index 000000000..28e837a7a --- /dev/null +++ b/app/src/main/res/layout/activity_diffutil.xml @@ -0,0 +1,47 @@ + + + + + + + +