android编程Drawerlayout问题

如图,我用drawerlayout实现了抽屉布局。但是我要想点击抽屉菜单中的选项如Section1,主内容就显示一个activity。我点击Section2,主内容就显示... 如图,我用drawerlayout实现了抽屉布局。但是我要想点击抽屉菜单中的选项如Section1,主内容就显示一个activity。我点击Section2,主内容就显示另一个activity。我该怎么办? 展开
 我来答
匿名用户
2015-04-04
展开全部
package com.xiaolangcai.lasttime;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends FragmentActivity {

        
       private Button mDrawerButton;
          private TextView title;
          private DrawerLayout mDrawerLayout;
          private ListView mDrawerList;
          
          private 巧镇行String[] menus;
          private List<Fragment> fragments;
        @Override
     孝哗   protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                init();
      setAdapter();
      setListener();

                if (savedInstanceState == null) {
                        getFragmentManager().beginTransaction()
                                        .add(R.id.container, new PlaceholderFragment()).commit();
                        selectItem(0);
                }
        }

        private void setListener() {
                // TODO Auto-generated method stub
               mDrawerButton.setOnClickListener(new OnClickListener() {
             
                    @Override
                    public void onClick(View v) {
                      if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                            mDrawerLayout.closeDrawer(mDrawerList);
                      } 旅谈else {
                            mDrawerLayout.openDrawer(mDrawerList);
                      }
                    }
                });
                mDrawerList.setOnItemClickListener(new OnItemClickListener() {
          
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View v, int position,
                            long id) {
                      selectItem(position);
                    }
                });
        }

        private void setAdapter() {
                // TODO Auto-generated method stub
                mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menus));
        }

        private void init() {
                // TODO Auto-generated method stub
                mDrawerButton = (Button) findViewById(R.id.left);
      title = (TextView) findViewById(R.id.title);
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ListView) findViewById(R.id.drawer_list);

      menus = new String[] {
                "Home", "Mercury", "Venus", "Mars", "About", "Setting"
      };
      fragments = new ArrayList<Fragment>();
      fragments.add(Homefragment.newInstance("Home"));
      fragments.add(Mercuryfragment.newInstance("Mercury"));
      fragments.add(Venusfragment.newInstance("Venus"));
      fragments.add(Marsfragment.newInstance("Mars"));
      fragments.add(Aboutfragment.newInstance("About"));
      fragments.add(Settingfragment.newInstance("Setting")); 
        }

        private void selectItem(int i) {
                // TODO Auto-generated method stub
                Fragment fragment = fragments.get(i);
      FragmentManager manager = getFragmentManager();
      manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
      title.setText(menus);
      mDrawerList.setItemChecked(i, true);
      mDrawerLayout.closeDrawer(mDrawerList);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                        return true;
                }
                return super.onOptionsItemSelected(item);
        }

        /**
       * A placeholder fragment containing a simple view.
       */
        public static class PlaceholderFragment extends Fragment {

                public PlaceholderFragment() {
                }

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

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式