首页 > 代码库 > DrawerLayout

DrawerLayout

技术分享

1. main layout:

<RelativeLayout 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"
    tools:context=".MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Main -->
        <FrameLayout
            android:id="@+id/fl_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/btn_open"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Open" />

        </FrameLayout>

        <!-- Drawer -->

        <ListView
            android:id="@+id/lv_drawer"
            android:background="#EEEEEE"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start" />

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

</RelativeLayout>


2. list item layout

<?

xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_drawer_list_item" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>



3. java code:

package com.torv.lijian.drawerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    private DrawerLayout mDrawerLayout = null;

    private Button mBtnOpen;

    private ListView mLvDrawer;
    List<String> mListData;

    LayoutInflater inflater = null;

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

        inflater = LayoutInflater.from(MainActivity.this);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mBtnOpen = (Button) findViewById(R.id.btn_open);
        mBtnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });

        mLvDrawer = (ListView)findViewById(R.id.lv_drawer);

        mListData = http://www.mamicode.com/new ArrayList<>();"This is Item 1");
        mListData.add("This is Item 2");
        mListData.add("This is Item 3");
        mListData.add("This is Item 4");

        MyAdapter adapter = new MyAdapter();
        mLvDrawer.setAdapter(adapter);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mListData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = null;
            if(null == convertView){
                convertView = inflater.inflate(R.layout.drawer_list_item, null);
                tv = (TextView)convertView.findViewById(R.id.tv_drawer_list_item);
            }else{
                tv = (TextView)convertView.findViewById(R.id.tv_drawer_list_item);
            }

            tv.setText(mListData.get(position));
            return convertView;
        }
    }

}


DrawerLayout