diff --git a/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/QuickMultistageSelectView.kt b/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/QuickMultistageSelectView.kt index fdf5727d..a0e6bc8d 100644 --- a/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/QuickMultistageSelectView.kt +++ b/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/QuickMultistageSelectView.kt @@ -16,6 +16,7 @@ import com.wpf.app.quick.widgets.selectview.data.QuickChildSelectData import com.wpf.app.quick.widgets.selectview.data.QuickParentSelectData import com.wpf.app.quick.widgets.selectview.helper.ParentChildDataHelper import com.wpf.app.quick.widgets.selectview.helper.QuickStickyView +import com.wpf.app.quickutil.LogUtil import com.wpf.app.quickutil.recyclerview.StickyItemDecoration /** @@ -61,7 +62,7 @@ open class QuickMultistageSelectView @JvmOverloads constructor( } private fun addListener() { - selectViewList[selectViewList.size - 1].addItemDecoration(StickyItemDecoration(QuickStickyView())) +// selectViewList[selectViewList.size - 1].addItemDecoration(StickyItemDecoration(QuickStickyView())) selectViewList[selectViewList.size - 1].setOnSelectChangeListener(object : OnSelectOnChange { override fun onSelectChange() { @@ -69,6 +70,7 @@ open class QuickMultistageSelectView @JvmOverloads constructor( } }) selectViewList[selectViewList.size - 1].addOnScrollListener(object : OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { super.onScrollStateChanged(recyclerView, newState) val selectAdapter: QuickSelectAdapter = @@ -120,9 +122,14 @@ open class QuickMultistageSelectView @JvmOverloads constructor( selectViewList[0].getSelectAdapter().curClickData = dataList[0] selectViewList[0].getSelectAdapter().notifyItemChanged(0) if (childInOne) { - selectViewList[1].setNewData(dataList.flatMap { + val allChildList = dataList.flatMap { it.childList ?: arrayListOf() - }) + } + val hasSuspension = allChildList.find { it.isSuspension } != null + selectViewList[1].setNewData(allChildList) + if (hasSuspension) { + selectViewList[1].addItemDecoration(StickyItemDecoration(QuickStickyView())) + } } else { selectViewList[1].setNewData(dataList[0].childList) } diff --git a/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/data/QuickParentSelectData.kt b/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/data/QuickParentSelectData.kt index a766d8f3..cbe4d5ad 100644 --- a/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/data/QuickParentSelectData.kt +++ b/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/data/QuickParentSelectData.kt @@ -6,6 +6,7 @@ import com.wpf.app.quick.widgets.recyclerview.QuickSelectAdapter import com.wpf.app.quick.widgets.recyclerview.data.QuickBindData import com.wpf.app.quick.widgets.recyclerview.holder.QuickViewHolder import com.wpf.app.quickbind.interfaces.RunItemClickWithSelf +import com.wpf.app.quickutil.recyclerview.scrollToPositionAndOffset /** * Created by 王朋飞 on 2022/7/13. @@ -64,7 +65,7 @@ open class QuickParentSelectData( } } if (findPos >= 0) { - childAdapter.mRecyclerView?.scrollToPosition(findPos) + childAdapter.mRecyclerView?.scrollToPositionAndOffset(findPos) } } } diff --git a/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/helper/QuickStickyView.kt b/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/helper/QuickStickyView.kt index 0b469361..f031a2fd 100644 --- a/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/helper/QuickStickyView.kt +++ b/Quick/src/main/java/com/wpf/app/quick/widgets/selectview/helper/QuickStickyView.kt @@ -4,9 +4,11 @@ import android.view.View import androidx.recyclerview.widget.RecyclerView import com.wpf.app.quick.widgets.recyclerview.QuickAdapter import com.wpf.app.quick.widgets.recyclerview.data.QuickItemData +import com.wpf.app.quick.widgets.selectview.data.QuickChildSelectData +import com.wpf.app.quickutil.LogUtil import com.wpf.app.quickutil.recyclerview.StickyView -class QuickStickyView: StickyView { +class QuickStickyView : StickyView { override fun isStickyView(recyclerView: RecyclerView, view: View?): Boolean { val viewData = getViewData(recyclerView, view) @@ -16,6 +18,15 @@ class QuickStickyView: StickyView { return view?.tag == "true" } + override fun getLastStickyView(adapter: RecyclerView.Adapter?, curViewPos: Int): Int { + (0 until curViewPos).reversed().forEach { i -> + if (getViewData(adapter, i)?.isSuspension == true) { + return i + } + } + return -1 + } + override fun getStickViewType(recyclerView: RecyclerView, view: View?): Int { val viewData = getViewData(recyclerView, view) if (viewData is QuickItemData) { @@ -30,4 +41,9 @@ class QuickStickyView: StickyView { val quickAdapter = recyclerView.adapter as? QuickAdapter return quickAdapter?.mDataList?.getOrNull(viewPos) } + + private fun getViewData(adapter: RecyclerView.Adapter?, viewPos: Int): QuickItemData? { + val quickAdapter = adapter as? QuickAdapter + return quickAdapter?.mDataList?.getOrNull(viewPos) + } } \ No newline at end of file diff --git a/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/RecyclerViewEx.kt b/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/RecyclerViewEx.kt index aa0bc019..1db922f1 100644 --- a/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/RecyclerViewEx.kt +++ b/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/RecyclerViewEx.kt @@ -7,7 +7,6 @@ import androidx.recyclerview.widget.RecyclerView * Created by 王朋飞 on 2022/6/22. */ - fun RecyclerView.isScrollBottom(): Boolean { return !canScrollVertically(1) } @@ -16,7 +15,7 @@ import androidx.recyclerview.widget.RecyclerView return !canScrollVertically(-1) } - fun RecyclerView.scrollToPosition(position: Int) { + fun RecyclerView.scrollToPositionAndOffset(position: Int) { if (position != -1) { scrollToPosition(position) val mLayoutManager = layoutManager diff --git a/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyItemDecoration.kt b/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyItemDecoration.kt index 068d5246..9978017b 100644 --- a/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyItemDecoration.kt +++ b/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyItemDecoration.kt @@ -6,6 +6,7 @@ import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.wpf.app.quickutil.LogUtil class StickyItemDecoration( @@ -181,6 +182,14 @@ class StickyItemDecoration( if (!mStickyPositionList.contains(position)) { mStickyPositionList.add(position) } + //不能用上一次缓存的position,需要根据当前位置查询上一个可吸附的View + if (mStickyPositionList.size > 1) { + val lastPos = mStickyView.getLastStickyView(mAdapter, position) + if (lastPos != -1 && lastPos != position && !mStickyPositionList.contains(lastPos)) { + mStickyPositionList.add(mStickyPositionList.size - 2, lastPos) + mStickyPositionList.sort() + } + } } /** @@ -189,7 +198,7 @@ class StickyItemDecoration( * @return */ private fun getStickyViewPositionOfRecyclerView(m: Int): Int { - return mLayoutManager!!.findFirstVisibleItemPosition() + m + return (mLayoutManager?.findFirstVisibleItemPosition() ?: 0) + m } /** diff --git a/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyView.kt b/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyView.kt index d4e805de..b0d87302 100644 --- a/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyView.kt +++ b/QuickUtil/src/main/java/com/wpf/app/quickutil/recyclerview/StickyView.kt @@ -6,10 +6,15 @@ import androidx.recyclerview.widget.RecyclerView interface StickyView { /** * 是否是吸附view - * @param view + * @param curView * @return */ - fun isStickyView(recyclerView: RecyclerView, view: View?): Boolean + fun isStickyView(recyclerView: RecyclerView, curView: View?): Boolean + + /** + * 得到上一个吸附View + */ + fun getLastStickyView(adapter: RecyclerView.Adapter?, curViewPos: Int): Int /** * 得到吸附view的itemType diff --git a/gradle.properties b/gradle.properties index a726e454..5815e4f5 100755 --- a/gradle.properties +++ b/gradle.properties @@ -27,7 +27,7 @@ android.nonTransitiveRClass=true IS_REMOTE=true GROUP=com.wpf.app.quick -VERSION_NAME=0.4.4 +VERSION_NAME=0.4.5 POM_DESCRIPTION=Quick Android.