首页 > 代码库 > DrawerLayout实现侧滑(简洁,高效)

DrawerLayout实现侧滑(简洁,高效)

效果图:

技术分享


主布局文件my_order_new_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:background="@color/white"
                style="@style/BaseStyle">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        style="@style/BaseStyle">

        <!--订单列表布局-->
        <ListView
            android:id="@+id/lv_order"
            android:divider="@color/background_gray_standard"
            android:dividerHeight="@dimen/margin_standard"
            style="@style/BaseStyle.FullWrap"/>

        <!--右滑布局-->
        <include
            android:id="@+id/right_drawer"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:background="@color/primary"
            android:choiceMode="singleChoice"
            layout="@layout/drawer_order_select"/>

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

</RelativeLayout>

侧滑部分的布局文件drawer_order_select.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/BaseStyle"
    android:background="@color/background_line_gray_light">

    <LinearLayout
        android:id="@+id/ll_select_bottom"
        style="@style/BaseStyle.FullWrap"
        android:orientation="horizontal"
        android:background="@color/background_gray"
        android:layout_alignParentBottom="true"
        android:padding="@dimen/margin_standard"
        android:gravity="right">

        <Button
            android:id="@+id/bt_select_clear"
            style="@style/BaseStyle.View"
            android:gravity="center"
            android:padding="@dimen/padding_medium"
            android:layout_height="@dimen/btn_height_large"
            android:textColor="@color/text_subtitle_standard"
            android:layout_marginRight="@dimen/margin_standard"
            android:background="@drawable/shape_rounded_corner_gray_line"
            android:text="@string/order_select_clear"/>

        <Button
            android:id="@+id/bt_select_confim"
            style="@style/BaseStyle.View"
            android:gravity="center"
            android:layout_height="@dimen/btn_height_large"
            android:padding="@dimen/padding_xmedium"
            android:textColor="@color/white"
            android:background="@drawable/rectangle_solid_cinnamomum"
            android:text="@string/order_select_done"/>
    </LinearLayout>

</RelativeLayout>

主页面代码:

public class MyBuyOrderActivity extends Activity{
    //侧滑布局
    private DrawerLayout mDrawerLayout;
    private RelativeLayout mRightLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_order_new_list);
       
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mRightLayout = (RelativeLayout)findViewById(R.id.right_drawer);

        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);//关闭手势滑动
    }

    @Override //菜单:筛选
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.select_order, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.select_order) {
            mDrawerLayout.openDrawer(mRightLayout);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override//关闭侧滑页面
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mDrawerLayout.isDrawerOpen(mRightLayout)) {
                mDrawerLayout.closeDrawer(mRightLayout);
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return true;
    }

}




DrawerLayout实现侧滑(简洁,高效)