DrawerLayout

来源:互联网 发布:淘宝u站刷粉丝软件 编辑:程序博客网 时间:2024/06/10 12:13
//XMl
<?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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.drawerlary.MainActivity"    android:id="@+id/dl">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/fl">    </FrameLayout>    <ListView        android:layout_gravity="start"        android:choiceMode="singleChoice"        android:background="#ff00ff"        android:id="@+id/lv"        android:layout_width="240dp"        android:layout_height="match_parent"></ListView></android.support.v4.widget.DrawerLayout>
package com.example.drawerlary;import android.os.Bundle;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.FrameLayout;import android.widget.ListView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    private List<String> list = new ArrayList<String>();    private ListView lv;    private DrawerLayout dl;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        for (int i = 0; i < 4; i++) {            list.add("item"+i);        }        lv = (ListView) findViewById(R.id.lv);        FrameLayout fl = (FrameLayout) findViewById(R.id.fl);        dl = (DrawerLayout) findViewById(R.id.dl);        lv.setAdapter(new MyAdapter());        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                ContentFragment contentFragment = new ContentFragment();                Bundle bundle = new Bundle();                bundle.putString("text",list.get(position));                contentFragment.setArguments(bundle);                int commit = getSupportFragmentManager().beginTransaction().replace(R.id.fl, contentFragment).commit();                dl.closeDrawer(lv);            }        });    }    class MyAdapter extends BaseAdapter {        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int position) {            return list.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            TextView textView = new TextView(MainActivity.this);            textView.setText(list.get(position));            return textView;        }    }}
package com.example.drawerlary;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/8. */public class ContentFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        Bundle arguments = getArguments();        String text = arguments.getString("text");        TextView textView = new TextView(getActivity());        textView.setText(text);        return textView;    }}