Android-----DrawerLayout的使用

1, 谷歌建議跟佈局使用DrawerLayout, DrawerLayout是V4包裏的控件;


2  給DrawerLayout中的控件設置一下兩種屬性,屬性設置給誰,側拉出來的就是誰;可以設置給圖片, 按鈕 ListView等等;

	android:layout_gravity="left"
	android:choiceMode="singleChoice"

---------------------------------
佈局 
	
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/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.wzq.drawerlayoutdemo2.MainActivity">

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

    <!--
    和drawerLayout配合需要配這兩個屬性
    android:layout_gravity="left"
        android:choiceMode="singleChoice"
        -->
    <ListView
        android:id="@+id/lv"
        android:background="@color/colorPrimary"
        android:layout_width="300dp"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:layout_height="match_parent"/>


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

------------------------------------

代碼
public class MainActivity extends AppCompatActivity {

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

    @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.frameLayout);
        lv = (ListView) findViewById(R.id.lv);

    }

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


    private void setListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MFragment mFragment = new MFragment();
                Bundle bundle = new Bundle();
                bundle.putString("name", lists.get(i));
                mFragment.setArguments(bundle);
                getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,mFragment).commit();
                //關閉drawer
                drawerLayout.closeDrawer(lv);
            }
        });
    }
}





public class MFragment  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 = (String) bundle.get("name");
        textView.setText(name);
        return textView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    //靜態方法,把自己返回;並且可以把傳過來的參數封裝到自己裏面;
    public static MFragment getInstace(String s) {
        MFragment mFragment = new MFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name",s);
        mFragment.setArguments(bundle);
        return mFragment;
    }
}


發佈了43 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章