如何在 XML 中添加双 ScrollView?

How can i add double ScrollView in XML?

我目前正在尝试在 XML 中添加双 ScrollView。 我在布局中使用了两个不同的视图 - 列表和类别。

当用户单击类别时 - 视频列表将变为可见,而类别将变为不可见。 目前,类别按钮是可滚动的,当我单击类别按钮时,视频列表会变得可见。但是,问题是视频列表不滚动。

这是我的布局代码:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/tools"
        android:orientation="vertical"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--this is parent layout where I call ListView-->
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/parent01"
            android:descendantFocusability="beforeDescendants"
            android:fitsSystemWindows="true"
            android:focusableInTouchMode="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical"
            android:weightSum="1">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <ListView
                    android:id="@+id/listview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="5dp"
                    android:divider="@android:color/transparent"
                    android:dividerHeight="1dp"></ListView>

            <ProgressBar
                    android:id="@+id/nextProgress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_gravity="center"
                    android:visibility="gone"
                    style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Small"/>
        </RelativeLayout>
    </LinearLayout>

    <!--This is the categories layout-->
    <ScrollView
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:id="@+id/ll_buttons"
            android:layout_marginTop="60dp"
            android:layout_height="wrap_content">

        <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="200dp">

            <Button
                    android:layout_weight="0.5"
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"/>

            <Button
                    android:layout_weight="0.5"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"
                    android:id="@+id/button2"/>
        </LinearLayout>

        <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="200dp">

            <Button
                    android:layout_weight="0.5"
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"/>

            <Button
                    android:layout_weight="0.5"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/rec_img"
                    android:id="@+id/button4"/>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

使用嵌套滚动视图

<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> 


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

嵌套滚动更好用NestedScrollView

NestedScrollView as the name suggests is used when there is a need for a scrolling view inside another scrolling view.

another thing use RecyclerView insted of Listview

RecyclerView vs. ListView

布局示例

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

 <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        // add here all your controlls


    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

并且不要忘记在 RecyclerView 中设置 setNestedScrollingEnabled() 属性 就像下面的代码

If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping the nested scroll.

mRecyclerView.setNestedScrollingEnabled(false);