天天看点

侧滑菜单drawerlayout的使用和一些小问题

侧滑菜单drawerlayout的使用和一些小问题

要在项目中加入drawerlayout侧滑,首先要定义好xml布局:

(注意:根布局要为DrawerLayout,第一个子布局要为主菜单(不是的话,会出现侧滑菜单控件无法点击,点击左菜单,关闭左菜单)左滑或者右滑菜单可以在根据

android:layout_gravity=""定义)      
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ditange.drawerlayout.MainActivity">

    <RelativeLayout
        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="主界面"
            android:textSize="20sp" />
        <ListView
            android:id="@+id/main_listview"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"></ListView>

    </RelativeLayout>

    <FrameLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff">

        <ListView
            android:id="@+id/left_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>      
然后在活动添加数据就可以显示了;贴部分代码      
final String[] str = new String[]{"item1", "item2", "item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, str);

mLeftListView.setAdapter(adapter);
mLeftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, "点击了侧滑菜单:" + str[position], Toast.LENGTH_SHORT).show();
    }
});

mMainListView.setAdapter(adapter);
mMainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, "点击了主菜单:" + str[position], Toast.LENGTH_SHORT).show();
    }
});      
我试了在写DrawerListener里面,把主布局设enable,clickable等都没有效果。      
后来把侧滑菜单的xml设置为       
android:clickable="true"      
问题解决了      

继续阅读