Simple implementation of Android sliding menu

The so-called sliding menu is to hide some menu options instead of placing them on the home screen, and then the menu can be displayed by sliding. It would be very difficult to implement it all by yourself. Fortunately, Google provides a DrawerLayout control

Suppose we want to implement such a function, click a navigation button to display a sliding menu

<android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_layout">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swip_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<ScrollView
    android:id="@+id/weather_layout"
    android:scrollbars="none"
    android:overScrollMode="never"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/title"/>
        <include layout="@layout/now"/>
        <include layout="@layout/forecast"/>
        <include layout="@layout/aqi"/>
        <include layout="@layout/suggestion"/>
    </LinearLayout>
</ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/choose_area_fragment"
            android:name="com.example.haley.coolweather.ChooseAreaFragment"
            android:layout_gravity="start"
            />
    </android.support.v4.widget.DrawerLayout>

DrawerLayout is a layout that allows two direct child controls to be placed in the layout. The first subspace is the content displayed in the main screen, and the second subspace is the content displayed in the active menu. In this demo, my One sub-control is the main interface of the weather forecast, the other sub-control is a fragment used to display the province and city, and then set a click event in a button

        navButton.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

The effect is shown in the figure:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325659684&siteId=291194637