2

我的问题是我在 a 中有 2 个不同的列表ViewPager2,它们的大小不同。而且我不希望能够在小家伙下方滚动。

                          NestedScrollView (Activity)
                                 |
                                 |
                                 |
                            FrameLayout
                                 |
                                 |  
                                 |
                            LinearLayout (Fragment)
                                 |
                                 |
                                 |
                             ViewPager2
                                 |
                                 |
                                 |
                            RecyclerView
_______________________________________________
|                  ViewPager2                 |
_______________________________________________
|                      |                      |
|                      |                      |
|                      |                      |
|        Page 1        |        Page 2        |
|                      |                      |
|                      |                      |
|                      |_______________________
|                      |                      
|                      |                      
|                      |                      
|                      |      Space I don't want to be able to see / scroll   
|                      |                      
|                      |                      
|                      |                      
|                      | 
________________________                                            

我有这个UnScrollView可以禁用滚动的地方,但问题是当我设置scrollable为假时。它不进去onTouchEvent,但它仍然可以滚动。

class UnScrollView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyle: Int = 0) : NestedScrollView(context, attributeSet, defStyle) {

    var scrollable = true

    fun isScrollable() = scrollable

    override fun performClick() = super.performClick()

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        return scrollable && super.onTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent)
            = scrollable && super.onInterceptTouchEvent(ev)
}

这是检测我们何时可以滚动的代码

(requireActivity() as MainActivity).binding
            .scroll
            .setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
                println("####################################################################################")
                println("ScrollY : ${v?.cast<NestedScrollView>()?.scrollY}")
                println("ViewPager H: ${binding.viewPager.height}")

                binding.viewPager.children.first().cast<RecyclerView>()
                    ?.children
                    ?.mapNotNull { it.findViewById<RecyclerView>(R.id.list) }
                    ?.elementAt(binding.viewPager.currentItem)
                    ?.let {
                        val manager = (it.layoutManager as GridLayoutManager)
                        val tmp = (it.adapter?.itemCount ?: 0) / manager.spanCount
                        val minTmp = it.children.first().height.times(tmp)
                        val displayMetrics = DisplayMetrics()

                        requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)

                        println("Row: $tmp")
                        println("HeightRow: ${it.children.first().height}")
                        println("Height: ${it.children.first().height.times(tmp)}")
                        println("MinTMP: $minTmp")
                        println("ScrollY: ${v?.cast<NestedScrollView>()?.scrollY}")
                        println("Display HEIGHT: ${requireActivity().px2dp(displayMetrics.heightPixels.toFloat())}")
                        println("Display + ScrollY: ${v?.cast<NestedScrollView>()?.scrollY?.plus(requireActivity().px2dp(displayMetrics.heightPixels.toFloat()))}")

                        if ((v?.cast<NestedScrollView>()?.scrollY?.plus(requireActivity().px2dp(displayMetrics.heightPixels.toFloat())) ?: 0f) > minTmp) {
                            println("blocked")
                            v?.cast<UnScrollView>()?.scrollable = false
                        } else {
                            println("not blocked")
                            v?.cast<UnScrollView>()?.scrollable = true
                        }
                    }
            }

并且所有屏幕都必须滚动,我在上方有 aTabLayout和 a ,所以滚动需要在页面之外TextViewViewPager2ViewPager2

4

1 回答 1

0

请使用此视图寻呼机,它会在您切换标签时测量高度

public class WrapContentViewPager extends ViewPager {
private View mCurrentView;

public WrapContentViewPager(Context context) {
    super(context);
}

public WrapContentViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mCurrentView == null) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }
    int height = 0;
    mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int h = mCurrentView.getMeasuredHeight();
    if (h > height) height = h;
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void measureCurrentView(View currentView) {
    mCurrentView = currentView;
    requestLayout();
}

}

于 2020-01-21T11:15:31.640 回答