手机排行榜网站,教育平台网站开发,wordpress 淘宝优惠券,微网站与微信的关系前言#xff1a;使用NestedScrollView嵌套RecyclerView。 解决步骤一#xff1a;固定高度
NestedScrollView嵌套RecyclerView时#xff0c;RecyclerView的高度是无限大#xff0c;所以要将RecyclerView设置固定高度。在代码中固定的#xff0c;灵活度更高。 binding.nest…前言使用NestedScrollView嵌套RecyclerView。 解决步骤一固定高度
NestedScrollView嵌套RecyclerView时RecyclerView的高度是无限大所以要将RecyclerView设置固定高度。在代码中固定的灵活度更高。 binding.nestedScrollV.post(new Runnable() {Overridepublic void run() {binding.selectList.getLayoutParams().height binding.nestedScrollV.getHeight(); // 使用NestedScrollView的高度binding.productList.getLayoutParams().height binding.nestedScrollV.getHeight(); binding.selectList.setLayoutParams(binding.selectList.getLayoutParams()); binding.productList.setLayoutParams(binding.productList.getLayoutParams());});
解决步骤二重写NestedScrollView的 measureChildWithMargins() 函数
public class MNestedScrollViewBox extends NestedScrollView {public MNestedScrollViewBox(NonNull Context context, Nullable AttributeSet attrs) {super(context, attrs);}// 使用NestedScrollView嵌套RecyclerView会导致RecyclerView复用机制失效RecyclerView会将所有数据一次性全部加载。// 解决方法重写measureChildWithMargins让NestedScrollView测量RecyclerView时 不使用MeasureSpec.UNSPECIFIED模式即可。Overrideprotected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {child.measure(parentWidthMeasureSpec, parentHeightMeasureSpec);}}
使用
?xml version1.0 encodingutf-8?
com.xx.xx.ui.widget.MNestedScrollViewBox xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:backgroundcolor/color_F5F6F7android:fillViewporttrueandroid:overScrollModenevertools:context.ui.fragment.shopdetail.OrderFoodFragmentLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroidx.recyclerview.widget.RecyclerViewandroid:idid/select_listandroid:layout_width84dpandroid:layout_heightmatch_parentandroid:overScrollModenevertools:listitemlayout/widget_select_item_ho /androidx.recyclerview.widget.RecyclerViewandroid:idid/product_listandroid:layout_width0dpandroid:layout_heightmatch_parentandroid:layout_weight1android:backgroundcolor/whiteandroid:overScrollModeneverandroid:paddingStart8dptools:ignoreRtlSymmetrytools:listitemlayout/widget_product_item_ho //LinearLayout/com.xx.xx.ui.widget.MNestedScrollViewBox