触摸 ListView 区域时,ScrollView 内带有 ListViews 的 RelativeLayout 不滚动

RelativeLayout with ListViews inside ScrollView not scrolling when touching ListView area

我的观点有问题,如下所示:

My view before scrolling

My view after scrolling

如您所见,它应该是一个带有不可滚动 ListView 的可滚动视图。

它由 DrawerLayout 组成,其中我有 CoordinatorLayout(AppBarLayout 和我的 FrameLayout 包含我的片段)和 NavigationView。

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:background="@color/colorBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_vision"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_vision"
    app:menu="@menu/activity_vision_drawer" />

app_bar_vision.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorBar"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"/>

现在是主要部分。我的片段布局由 ScrollView 组成,里面有 RelativeLayout,我有两个 ListView,高度是根据分配的元素以编程方式设置的。

fragment_preferences.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<include layout="@layout/content_preferences" />

</ScrollView>

content_preferences.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
tools:context="dron.mkapiczynski.pl.dronvision.fragment.PreferencesFragment"
tools:showIn="@layout/fragment_preferences">>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Preferencje wizualizacji"
    android:id="@+id/preferencesTitle"
    android:layout_gravity="center" />

<TextView
    android:id="@+id/trackedListTitle"
    android:text="Drony śledzone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/preferencesTitle"
    android:layout_marginTop="20dp"/>

<ListView
    android:id="@+id/trackedDroneList"
    android:layout_below="@+id/trackedListTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_gravity="center_horizontal"
    android:layout_centerInParent="true"
    tools:showIn="@layout/fragment_preferences"/>

<TextView
    android:id="@+id/visualizedListTitle"
    android:text="Drony do wizualizacji obszaru przeszukanego"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/trackedDroneList"
    android:layout_marginTop="20dp"/>

<ListView
    android:id="@+id/visualizedDroneList"
    android:layout_below="@+id/visualizedListTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_gravity="center_horizontal"
    android:layout_centerInParent="true"
    android:nestedScrollingEnabled="true"
    tools:showIn="@layout/fragment_preferences"/>

一般来说,滚动是有效的,但只有当我在 ListView 之外的区域触摸屏幕时,例如在我的 TextView 区域。当我尝试使用 ListView 在区域中滚动时,没有任何反应。

如有任何建议,我将不胜感激。我已经尝试实现我自己的 nonScrollingListView 扩展 ListView 的想法,这可能会启用父滚动,但这无济于事。 我也读过有关使用 LinearLayout 而不是 ListView 的信息,但也许还有一种方法可以将 ListView 与 ScrollView 一起使用?

通常不鼓励将可滚动视图用作滚动视图或任何其他可滚动内容的子视图。 但是,但是,如果应用程序有需要,如果开发人员希望顺利处理滚动事件,下面是单独处理可滚动内容的方法,

对于父滚动视图,可以将滚动事件处理为

m_parentScrollView.setOnTouchListener(new View.OnTouchListener() 
{
       public boolean onTouch(View p_v, MotionEvent p_event) 
        {
            m_childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
           //  We will have to follow above for all scrollable contents
           return false;
        }
});

对于子滚动视图,在你的Listview中,我们必须处理如下所示的事件:

m_childScrollView.setOnTouchListener(new View.OnTouchListener() 
{
      public boolean onTouch(View p_v, MotionEvent p_event)
       {
          // this will disallow the touch request for parent scroll on touch of child view
           p_v.getParent().requestDisallowInterceptTouchEvent(true);
           return false;
       }
});

您可以从这里获得更多详细信息: handle-scrollable-views-or-controls-in-another-scrollview-in-android

此外,作为旁注,我在使用 Scrollview 和 RelativeLayout 时也遇到了一些类似的问题,this 用户也遇到了与您相同的问题。

希望对你有帮助:)