DrawerLayout侧滑列表

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#0ff"
        android:text="Hello World!" />
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/dl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--主布局必须在  左右两侧子布局的上面  设置-->
        <LinearLayout
            android:id="@+id/ll_main"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="我是主布局内容"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_left"
            android:orientation="vertical"
            android:layout_gravity="left"
            android:background="#fff"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="我是左侧布局"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/ll_right"
            android:orientation="vertical"
            android:layout_gravity="right"
            android:background="#fff"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="我是右侧布局"
                />
        </LinearLayout>

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



2.主类

public class MainActivity extends AppCompatActivity {

    DrawerLayout dl ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dl = (DrawerLayout) findViewById(R.id.dl);

        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置左右侧拉时  使用的是left 和 right   被替代
                //start  end
                //打开侧拉视图
                dl.openDrawer(Gravity.LEFT);

//                dl.closeDrawer(Gravity.LEFT);
            }
        });



        dl.addDrawerListener(new DrawerLayout.DrawerListener() {
            /**
             * 当DrawerLayout被拖动的时候  会被调用
             * @param drawerView   指当前的DrawerLayout对象
             * @param slideOffset  拖动的距离值   0-1
             */
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.i("==========", "onDrawerSlide: " + slideOffset);
            }

            /**
             * 当DrawerLayout控件  完全被打开时 调用
             * @param drawerView
             */
            @Override
            public void onDrawerOpened(View drawerView) {
                Log.i("==========", "onDrawerOpened: " + "Opened");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.i("==========", "onDrawerClosed: " + "Closed");
            }

            /**
             * 1、固定状态
             * 2、空闲状态
             * 3、被拖拽状态
             * @param newState
             */
            @Override
            public void onDrawerStateChanged(int newState) {
                switch (newState){
                    case DrawerLayout.STATE_DRAGGING:
                        Log.e("==========", "onDrawerStateChanged: STATE_DRAGGING" + newState);
                        break;
                    case DrawerLayout.STATE_IDLE:
                        Log.e("==========", "onDrawerStateChanged: STATE_IDLE" + newState);
                        break;
                    case DrawerLayout.STATE_SETTLING:
                        Log.e("==========", "onDrawerStateChanged: STATE_SETTLING" + newState);
                        break;
                }

            }
        });
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值