android编程Drawerlayout问题
如图,我用drawerlayout实现了抽屉布局。但是我要想点击抽屉菜单中的选项如Section1,主内容就显示一个activity。我点击Section2,主内容就显示...
如图,我用drawerlayout实现了抽屉布局。但是我要想点击抽屉菜单中的选项如Section1,主内容就显示一个activity。我点击Section2,主内容就显示另一个activity。我该怎么办?
展开
1个回答
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;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询