禁用GridView Scroll并在Android中启用Scroll视图



我想在我的Android活动中放置一个座位布局。我想让我的座位布局在网格视图中完全可见,而不是滚动网格视图。我想我的滚动视图应该滚动,禁用网格视图滚动后,滚动视图也不起作用。有人能帮我吗?

<ScrollView
android:layout_below="@+id/top"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_above="@+id/rl"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
android:id="@+id/wheel"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_marginTop="20dp"
android:layout_marginEnd="100dp"
android:src="@drawable/mywheel" />
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/wheel"
android:layout_marginStart="90dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="90dp"
android:numColumns="5" />

下面的代码用于禁用我的网格视图中的滚动

gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return event.getAction() == MotionEvent.ACTION_MOVE;
}
});

遗憾的是,使用"经典的";View就像ScrollViewGridView一样——几天前安卓根本没有为嵌套滚动做好准备。。。理论上,你可以尝试将GridView迁移到一些自定义的NestedGridView,但我建议用GridLayoutManagerGridView转换为RecyclerView-当前版本的RecyclerView在高度上支持wrap_content,并且更灵活,你将不允许轻松滚动,因为这个View保持整个ScrollView容器可滚动

下面的代码段不起作用,因为当出现ACTION_MOVE时,您将返回true。该CCD_ 15值意味着";是的,我已经处理好了这个动作,不要再发了GridView什么也不做,但返回true,因此ACTION_MOVE永远不会被ScrollView调度和获取/处理

@Override
public boolean onTouch(View v, MotionEvent event) {
return event.getAction() == MotionEvent.ACTION_MOVE;
}

编辑:也有可能调度触摸(方法onInterceptTouchEventdispatchTouchEvent和类似方法(-在您的情况下,共享触摸(第二个滚动中的一个滚动(既困难又棘手,但如果您有非常复杂的GridView适配器,则可能值得一试

编辑评论:

GridViewXML声明替换为以下内容:

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/wheel"
android:layout_marginStart="90dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="90dp" />

删除android:numColumns="5"行,这将由GridLayoutManager在代码中声明

int numColumns = 5;
recyclerView.setLayoutManager(new GridLayoutManager(this, numColumns));

您需要编辑您的适配器,现在它应该是extends RecyclerView.Adapter<GridAdapter.GridViewHolder>。更多关于这个适配器在HERE

最新更新