【问题标题】:RelativeLayout with ListViews inside ScrollView not scrolling when touching ListView area触摸 ListView 区域时,ScrollView 内的具有 ListViews 的 RelativeLayout 不滚动
【发布时间】:2016-01-15 13:39:39
【问题描述】:

我的视图有问题,如下所示:

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 一起使用?

【问题讨论】:

  • 当您嵌套可滚动视图时会发生这种情况...系统对要监听的哪个滚动事件感到困惑:ListView's ? ScrollView 的?

标签: android listview scroll scrollview android-relativelayout


【解决方案1】:

通常不鼓励使用可滚动视图作为滚动视图或任何其他可滚动内容的子视图。 但是,如果有应用程序的需要,如果开发者想要流畅地处理滚动事件,下面是单独处理可滚动内容的方法,

对于父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 用户也遇到了与您相同的问题。

希望对你有帮助:)

【讨论】:

  • 在我的情况下,我想要实现的只是滚动父视图(这里是 ScrollView),所以我唯一要做的就是通过 m_childScrollView.setOnTouchListener(new View.OnTouchListener() 禁用子滚动{ public boolean onTouch(View p_v, MotionEvent p_event) { // 这将禁止父滚动触摸子视图时的触摸请求 p_v.getParent().requestDisallowInterceptTouchEvent(false); return false; } });非常感谢;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
相关资源
最近更新 更多