DrawerLayout

//Activity中的代码

package fmh.bwie.com.drawerlayout;


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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private ListView lv;
    private ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件的方法
        initView();
        //加载数据
        initData();
        //设置监听事件
        setListener();
    }

    private void initView() {
        //获取控件
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        frameLayout = (FrameLayout) findViewById(R.id.framlayout);
        lv = (ListView) findViewById(R.id.lv);
    }


    private void initData() {
        //创建集合
        list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add("DrawerLayout"+i);
        }
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1,list);
        lv.setAdapter(stringArrayAdapter);
    }

    private void setListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Fragment1 fragment1 = new Fragment1();
                Bundle bundle = new Bundle();
                bundle.putString("name",list.get(i));
                fragment1.setArguments(bundle);
                getSupportFragmentManager().beginTransaction().replace(R.id.framlayout,fragment1).commit();
                drawerLayout.closeDrawer(lv);
            }
        });
    }

}



//Fragment中的代码

package fmh.bwie.com.drawerlayout;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * author:Created by WangZhiQiang on 2017/9/7.
 */

public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        Bundle bundle = getArguments();
        String name = bundle.getString("name");
        textView.setText(name);
        return textView;
    }
}

//布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="fmh.bwie.com.drawerlayout.MainActivity">

    <FrameLayout
        android:id="@+id/framlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <ListView
        android:id="@+id/lv"
        android:background="@color/colorPrimaryDark"
        android:layout_width="300dp"
        android:choiceMode="singleChoice"
        android:layout_gravity="left"
        android:layout_height="match_parent"></ListView>

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值