【问题标题】:ListView inside ScrollView with DrawerLayout带有 DrawerLayout 的 ScrollView 内的 ListView
【发布时间】:2016-09-28 07:17:54
【问题描述】:

我有一个带有 ScrollView 的布局,里面有一个 ListView。一切正常,但如果我打开 DrawerLayout,ScrollView 会滚动一点。看看这些截图。

这里我打开 DrawerLayout

然后我关闭 DrawerLayout。

如您所见,它滚动了一点。如何解决这个问题? 这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menuLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ProgressBar
            android:id="@+id/progressWheelBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/noDataTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/noData"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/actionBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/innerActionBarLayout"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@color/material_blue_500" >

                <ImageButton
                    android:id="@+id/menuButton"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:background="@null"
                    android:src="@drawable/ic_menu_white_24dp" />

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="16dp"
                    android:layout_toRightOf="@+id/menuButton"
                    android:textColor="@color/white"

                    android:textSize="18sp" />
            </RelativeLayout>
        </LinearLayout>

        <ScrollView
            android:id="@+id/mainScrollView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/actionBarLayout"
            android:fillViewport="true" 
            android:scrollbars="none"
            >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
                 <!-- Here goes the frame layout with a listview inside -->
                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="gone"
                    tools:context="ru.tenet.ttk.MainActivity" />
            </LinearLayout>
        </ScrollView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/actionBarLayout"
            android:background="@drawable/shadow_down" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="@color/white"
        android:orientation="vertical"
        android:clickable="true" >

        <ListView
            android:id="@+id/menuListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:listSelector="@drawable/list_selector"
            android:layout_marginTop="15dp"
            android:divider="@null" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 你真的需要ScrollView吗? ListView 会自行滚动。
  • 你可以使用NestedScrollView
  • 您可以将 ListView 放在滚动视图中,但您应该同时处理滚动,Bec 列表视图具有自己的滚动功能,滚动视图具有其滚动功能,当您尝试滚动时,滚动都会得到冲突所以你需要处理父子滚动事件。
  • 此链接可能会有所帮助:stackoverflow.com/questions/18367522/…
  • ListView和ScrollView的滚动是怎么同步的?哪个会优先?我猜是你在某处覆盖了 onIntercepterTouchEvent,是真的吗?

标签: android xml scrollview


【解决方案1】:

由于您尚未发布代码,因此很难给出明确的答案。一个原因可能是您的活动中有“windowActionBarOverlay=true”样式设置。由于该设置,活动的内容从页面的开头开始,位于 ActionBar 之后。

此外,正如上面的评论者所说,您真的需要在 ScrollView 中使用 ListView 吗? ListView 本身就是一个 ScrollView。

最好发布 Drawer 活动和内容活动的代码。

【讨论】:

    【解决方案2】:

    简单的解决方案是使用 headerView - ListView.addHeaderView(); 我认为您不需要使用 ScrollView 只需将容器添加为 headerView

    【讨论】:

      【解决方案3】:

      您最好的选择是使用RecyclerView 并定义一个 headerRow 来显示您的标题。

      【讨论】:

        【解决方案4】:

        为 DrawerLayout 分配一个 DrawerListener,并在 onDrawerClosed 事件中将 scrollView 滚动到顶部。

        mDrawer.setDrawerListener(new DrawerListener() {
                @Override
                public void onDrawerSlide(View view, float v) {
        
                }
        
                @Override
                public void onDrawerOpened(View view) {
        
                }
        
                @Override
                public void onDrawerClosed(View view) {
                    mScrollView.fullScroll(ScrollView.FOCUS_UP);
                }
        
                @Override
                public void onDrawerStateChanged(int i) {
        
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-07
          • 1970-01-01
          • 1970-01-01
          • 2017-09-02
          • 1970-01-01
          • 2013-09-19
          • 1970-01-01
          相关资源
          最近更新 更多