DrawerLayout侧滑

来源:互联网 发布:剑灵免费捏脸数据图 编辑:程序博客网 时间:2024/06/11 13:19

/*

*activity_main

*/

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mydraw"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/openDraw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="打开侧滑菜单"/>
    </LinearLayout>
    <ListView
        android:id="@+id/lv"
        android:layout_width="240sp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#f00"
        android:dividerHeight="1dp"
        android:entries="@array/books"
        android:background="#fff"
        />
</android.support.v4.widget.DrawerLayout>


/*

*MianActivity

*/



import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;


public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private DrawerLayout drawerLayout;
    private Button openDraw;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }


    private void init() {
        drawerLayout = (DrawerLayout) findViewById(R.id.mydraw);
        openDraw = (Button) findViewById(R.id.openDraw);
        lv = (ListView) findViewById(R.id.lv);
        //点击侧滑
        openDraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开侧滑
                drawerLayout.openDrawer(lv);
            }
        });
        //点击lv的条目,关闭侧滑,在主界面显示相关详细
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                drawerLayout.closeDrawer(lv);//关闭侧滑
                //改变主界面
                String str = parent.getItemAtPosition(position).toString();
                openDraw.setText(str);
            }
        });
    }

}


/*

*侧滑布局

*/

private String[] menuStr = {"搜索","消息","收藏","离线","活动"};
//侧滑private void initDataForListViewLeft(){    ArrayAdapter<String> lvLeftAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menuStr);    lvLeft.setAdapter(lvLeftAdapter);    //设置ListView的监听器    lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {            //关闭侧滑            drawerLayout.closeDrawer(lvLeft);        }    });}