Android抽屜菜單DrawerLayout的實現案例

來源:互聯網
上載者:User

標籤:drawerlayo   抽屜菜單   

(1)項目布局檔案
activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <!-- The main content view -->    <FrameLayout        android:id="@+id/content_frame"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </FrameLayout>    <!-- The navigation view -->    <ListView        android:id="@+id/left_drawer"        android:layout_width="240dp"        android:layout_height="match_parent"        android:layout_gravity="start"        android:background="#ffffcc"        android:choiceMode="singleChoice"        android:divider="@android:color/transparent"        android:dividerHeight="0dp" >    </ListView></android.support.v4.widget.DrawerLayout>

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="25sp" /></LinearLayout>

(2)主要類檔案

package com.xuliugen.drawerlayout;import java.util.ArrayList;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.content.Intent;import android.content.res.Configuration;import android.net.Uri;import android.os.Bundle;import android.support.v4.app.ActionBarDrawerToggle;import android.support.v4.widget.DrawerLayout;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity implements OnItemClickListener {    private DrawerLayout mDrawerLayout; // 設定的是左側的抽屜菜單    private ListView mDrawerList;    private ArrayList<String> menuLists;    private ArrayAdapter<String> adapter;    private ActionBarDrawerToggle mDrawerToggle;// actionBar開啟關閉的    private String mTitle;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mTitle = (String) getTitle();        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);        mDrawerList = (ListView) findViewById(R.id.left_drawer);        menuLists = new ArrayList<String>();        for (int i = 0; i < 5; i++) {            menuLists.add("item" + i);        }        // 初始化適配器        adapter = new ArrayAdapter<String>(this,                android.R.layout.simple_list_item_1, menuLists);        // 為左側的抽屜設定了資料        mDrawerList.setAdapter(adapter);        // 左側滑動菜單的監聽事件        mDrawerList.setOnItemClickListener(this);        // 設定抽屜被開啟關閉的對象        mDrawerToggle = new ActionBarDrawerToggle(this, //                mDrawerLayout,//                R.drawable.ic_drawer,//                R.string.drawer_open,//                R.string.drawer_close) {            // 被開啟的時候            @Override            public void onDrawerOpened(View drawerView) {                super.onDrawerOpened(drawerView);                getActionBar().setTitle("請選擇"); // 設定actionBar的文字                invalidateOptionsMenu(); // Call onPrepareOptionsMenu()            }            // 被關閉的時候            @Override            public void onDrawerClosed(View drawerView) {                super.onDrawerClosed(drawerView);                getActionBar().setTitle(mTitle);                invalidateOptionsMenu();// 重新繪製actionBar上邊的功能表項目            }        };        // 設定滑動菜單的 開啟關閉事件        mDrawerLayout.setDrawerListener(mDrawerToggle);        // 開啟ActionBar上APP ICON的功能:點擊開啟和點擊關閉7        getActionBar().setDisplayHomeAsUpEnabled(true);        getActionBar().setHomeButtonEnabled(true);    }    @Override    public boolean onPrepareOptionsMenu(Menu menu) {        boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);        menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);        return super.onPrepareOptionsMenu(menu);    }    /**     * 功能表項目的設定     */    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    /**     * 設定actionBar上邊表徵圖的點擊事件     */    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // 將ActionBar上的表徵圖與Drawer結合起來        if (mDrawerToggle.onOptionsItemSelected(item)) {            return true;        }        switch (item.getItemId()) {        case R.id.action_websearch:            Intent intent = new Intent();            intent.setAction("android.intent.action.VIEW");            Uri uri = Uri.parse("http://blog.csdn.net/xlgen157387");            intent.setData(uri);            startActivity(intent);            break;        }        return super.onOptionsItemSelected(item);    }    /**     * 根據官方文檔提示的資訊     *      * 將mDrawerToggle.syncState();放入到onPostCreate中     */    @Override    protected void onPostCreate(Bundle savedInstanceState) {        super.onPostCreate(savedInstanceState);        // 需要將ActionDrawerToggle與DrawerLayout的狀態同步        // 將ActionBarDrawerToggle中的drawer表徵圖,設定為ActionBar中的Home-Button的Icon        mDrawerToggle.syncState();    }    /**     * 當螢幕發生選裝的時候也需要進行相應的設定     */    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        mDrawerToggle.onConfigurationChanged(newConfig);    }    /**     * 監聽事件的實現     *      *      * 當點擊功能表列中的item的時候切換相應的fragment介面     */    @Override    public void onItemClick(AdapterView<?> arg0, View arg1, int position,            long arg3) {        // 動態插入一個Fragment到FrameLayout當中        Fragment contentFragment = new ContentFragment();        Bundle bundle = new Bundle();        bundle.putString("text", menuLists.get(position));        contentFragment.setArguments(bundle);        // fragment建立好了之後需要交給fragmentManager來替換到相應的視圖中        FragmentManager fm = getFragmentManager();        fm.beginTransaction().replace(R.id.content_frame, contentFragment)                .commit();        mDrawerLayout.closeDrawer(mDrawerList);    }}

ContentFragment.java

package com.xuliugen.drawerlayout;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/** * 用於填充介面的fragment *  * @author xuliugen *  */public class ContentFragment extends Fragment {    private TextView textView;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_content, container,false);        textView = (TextView) view.findViewById(R.id.textView);        // 獲得傳入的參數        String text = getArguments().getString("text");         textView.setText(text);        return view;    }}

(3)項目示範效果

(4)項目原始碼和Google參考文檔下載:http://yunpan.cn/cZZ7RVRY96yWe (提取碼:2981)

Android抽屜菜單DrawerLayout的實現案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.