Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android DrawerLayout 抽屜

Android DrawerLayout 抽屜

編輯:關於Android編程

Android DrawerLayout 抽屜

DrawerLayout 在supportV4 Lib中,類似開源slidemenu一樣,DrawerLayout父類為ViewGroup,自定義組件基本都是擴展這個類。
android.support.v4.widget.DrawerLayout

下面是個簡單的用法演示。點左上角的按鈕 打開抽屜菜單。

首頁

這裡寫圖片描述

點擊對應的ITEM 切換對應的內容,內容顯示使用Fragment,這裡沒用到ActionBar來做切換<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> <framelayout android:id="@+id/fragment_container" android:layout_height="match_parent" android:layout_width="match_parent"> </framelayout>

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class DrawerLayoutActivity extends FragmentActivity {

    private DrawerLayout mDrawerLayout;
    private ListView drawerContent;//抽屜內容


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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerContent = (ListView) findViewById(R.id.drawer_content);
        mDrawerLayout.setDrawerListener(new DemoDrawerListener());
        addHeader(drawerContent);//ListView Header頭
        drawerContent.setAdapter(new MyAdapter());
        drawerContent.setOnItemClickListener(new DrawerItemClickListener());
        findViewById(R.id.slide).setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                mDrawerLayout.openDrawer(drawerContent);//打開抽屜內容
            }
        });

        //顯示默認的HOME
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();

    }

    private void addHeader(ListView mDrawer2) {
        View view = LayoutInflater.from(this).inflate(R.layout.list_header,null);
        mDrawer2.addHeaderView(view);
    }

    static String titles[] = { "主頁", "公司", "附近", "設置" };

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int p) {
            return titles[p];
        }

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

        @Override
        public View getView(int p, View contentView, ViewGroup arg2) {
            String title = titles[p];
            View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_list, null);
            TextView textView = (TextView) v.findViewById(R.id.text);
            textView.setText(title);
            return v;
        }

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position,long id) {
            showFragmentById(position);
            mDrawerLayout.closeDrawer(drawerContent);
        }
    }

    //切換到對應的Fragment
    public void showFragmentById(int position) {
        Fragment f = null;
        switch (position) {
        case 1:
            f = new HomeFragment();
            break;
        case 2:
            f = new CompanyFragment();
            break;
        case 3:
            f = new NearFragment();
            break;
        case 4:
            f = new SettingFragment();
            break;
        default:
            break;
        }
        if (f == null) {
            return;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();
    }

    //回調函數,打開或關閉時可以做一些事情
    private class DemoDrawerListener implements DrawerLayout.DrawerListener {
        @Override
        public void onDrawerOpened(View drawerView) { //打開時調用
        }

        @Override
        public void onDrawerClosed(View drawerView) { //關閉時調用
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {//滑動過程中
        }

        @Override
        public void onDrawerStateChanged(int newState) {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

}

這裡只寫HomeFragment代碼,其它幾個內容都是一樣的。

public class HomeFragment extends Fragment {

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fg, container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView textView = (TextView) view.findViewById(R.id.text);
        textView.setText("Home");
    }

}

home_fg.xml




     

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved