0

我试图在滚动视图到达 scrollY 位置时停止滚动。然后,当子视图(recyclerview)到达顶部或过度滚动时,将其更改回可滚动。

我尝试了两种方法

  1. onTouchListener - 它不与 scrollY 位置交互
nestedScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
  1. setOnScrollChangeListener - 不知道如何将其设置为不可滚动
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY >400dp) {
     
}
}

编辑 1 个 XML

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

     <Linearlayout
         android:layout_width="match_parent"
         android:layout_height="match_parent">
         //include more contents card
         <androidx.recyclerview.widget.RecyclerView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="true"
            android:orientation="vertical"
            />
          </LinearLayout>
</androidx.core.widget.NestedScrollView>
4

1 回答 1

0

您的想法OnTouchListener是可行的,但 Android 期望的返回值令人困惑。false表示滚动已激活。true表示滚动已停用。

这将防止滚动:

scrollView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

来自View.java

/**
* Called when a touch event is dispatched to a view. This allows listeners to
* get a chance to respond before the target view.
*
* @param v The view the touch event has been dispatched to.
* @param event The MotionEvent object containing full information about
*        the event.
* @return True if the listener has consumed the event, false otherwise.
*/
boolean onTouch(View v, MotionEvent event);

编辑:

您可以同时使用您的两个侦听器并通过实例变量“连接”它们。像这样:

    boolean freezeScrolling = false; //This has to be an instance variable so both Listeners can access it.

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        ScrollView scrollView = findViewById(R.id.scroll_view);
        Button button = findViewById(R.id.button);

        scrollView.setOnScrollChangeListener(new ScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                    Log.d("TAG", String.valueOf(scrollY));
                    freezeScrolling = (scrollY>100); //freeScrolling is set to true if scrollY is above 100. Otherwise to false.
            }
        });

        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return freezeScrolling; //onTouch returns the value which has been set in onScrollChange.
            }
        });

        //unfreezing test:
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.scrollTo(0, 0); //freezeScrolling will be changed automatically.
            }
        });
于 2020-08-09T16:51:28.670 回答