NavigationDrawer 菜单中的 ScrollView

ScrollView inside a NavigationDrawer menu

我想在 Android Studio 的 NavigationDrawer 模板中实现 ScrollView。我需要做什么?如何使完整的导航菜单可滚动?我这里有很多选项,但我无法滚动查看所有选项 Demo Image

我正在使用它,包括 navigationView 中的布局,我可以轻松管理它。

像这样

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginEnd="-65dp"
        android:layout_marginRight="-65dp"
        android:fitsSystemWindows="true">

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

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

nav_header_main_navigation

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/app_gray"
    android:gravity="bottom">
  <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:id="@+id/rlTop"
                android:layout_width="match_parent"
                android:layout_height="88dp"
                android:layout_alignParentTop="true"
                android:background="@color/appYellow">

                <TextView
                    android:id="@+id/tvName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="24dp"
                    android:layout_marginEnd="30dp"
                    android:layout_marginLeft="24dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginStart="24dp"
                    android:ellipsize="end"
                    android:includeFontPadding="false"
                    android:maxEms="14"
                    android:maxLines="2"
                    android:textColor="@color/app_gray"
                    android:textSize="15sp" />

                <RelativeLayout
                    android:id="@+id/rlBack"
                    android:layout_width="30dp"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginBottom="20dp"
                    android:clickable="true">
                    <ImageView
                        android:id="@+id/ivBack"
                        android:layout_width="10dp"
                        android:layout_height="20dp"
                        android:adjustViewBounds="true"
                        android:clickable="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/back_icon_black" />
                </RelativeLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rlBottom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/rlTop"
                android:background="@color/app_gray">

                <RelativeLayout
                    android:id="@+id/rlProfile"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:layout_marginTop="10dp">

                    <ImageView
                        android:id="@+id/ivProfile"
                        android:layout_width="19dp"
                        android:layout_height="19dp"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="24dp"
                        android:layout_marginStart="24dp"
                        android:src="@drawable/icon_nav_profile" />

                    <TextView
                        android:id="@+id/tvProfile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_toEndOf="@id/ivProfile"
                        android:layout_toRightOf="@id/ivProfile"
                        android:includeFontPadding="false"
                        android:paddingBottom="1dp"
                        android:paddingTop="1dp"
                        android:text="@string/profile"
                        android:textAlignment="viewStart"
                        android:textColor="@color/white"
                        android:textDirection="locale"
                        android:textSize="15sp" />
                </RelativeLayout>
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

这是我的整个导航activityXML

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginEnd="-65dp"
        android:layout_marginRight="-65dp"
        android:fitsSystemWindows="true">

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

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

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

这是要避免的主要因素ViewPager

app_bar_main_navigation

<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:background="@color/app_gray"
    tools:context="com.barq.consumer.ui.home.MainNavigationActivity">

    <FrameLayout
        android:id="@+id/flMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>


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

现在MainNavigationActivity.Java

public class MainNavigationActivity extends BaseActivity {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_navigation);
        ButterKnife.bind(this);
      //here is you can load your fragment without view pager
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.flMain, HomeFragment.newInstance(), HomeFragment.TAG)
                .commit();


}

您可以手动处理其他点击以加载框架布局和活动中的其他片段以及抽屉打开和关闭您也可以手动处理使用抽屉对象。希望你能看懂。

这是一个让您轻松避免菜单并在滚动视图中使用普通布局的示例。 以下是关于上述示例的更多答案 ULR