NavigationDrawer菜单内的ScrollView

时间:2019-02-19 17:03:04

标签: android android-scrollview android-navigation-drawer

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

1 个答案:

答案 0 :(得分:1)

我正在为此使用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>

这是我的整个导航活动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: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

的更多答案