【发布时间】:2013-11-28 16:05:22
【问题描述】:
我想根据某些条件在抽屉中加载不同的片段。说:
if(bit =0){
load fragment0();
}
else if(bit =1){
load fragment1();
}
每个片段(片段 0 和片段 1)都包含自己的项目列表和按钮/视图。 然后根据抽屉片段中的项目单击,我想在主布局中加载相应的片段。 以下是我正在使用的代码:
这是在抽屉中加载不同片段的主要活动
public class LaunchActivity extends FragmentActivity {
private DrawerLayout DrawerLayout;
//private ListView DrawerList;
private FrameLayout DrawerList;
private ActionBarDrawerToggle DrawerToggle;
@SuppressWarnings("unused")
private CharSequence DrawerTitle;
private CharSequence Title;
private String[] ListTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawerlist_test);
Title = DrawerTitle = getTitle();
ListTitles = getResources().getStringArray(R.array.list_array);
DrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//DrawerList = (ListView) findViewById(R.id.left_drawer);
DrawerList = (FrameLayout) findViewById(R.id.left_drawer);
// initialize drawer list
// Also set a custom shadow that overlays the main content when the drawer opens
DrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// change frames
int bit =0;
if(bit==1){
// add fragments to drawer list
Fragment newFragment;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
newFragment = new testfragment();
transaction.add(R.id.left_drawer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
else
{
// add fragments to drawer list
Fragment newFragment;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
newFragment = new testfragment1();
transaction.add(R.id.left_drawer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
// set up the drawer's list view with items and click listener
// DrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_itemlist, ListTitles));
// DrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
DrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
DrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(Title);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(Title); // same title for open/close drawer
//getActionBar().setTitle(DrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
DrawerLayout.setDrawerListener(DrawerToggle);
if (savedInstanceState == null) {
// selectItem(0);
}
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
DrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggle
DrawerToggle.onConfigurationChanged(newConfig);
}
// option menu - action bar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
// Called whenever we call invalidateOptionsMenu()
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = DrawerLayout.isDrawerOpen(DrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (DrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/*
// Nav Drawer List click
// The click listener for ListView in the navigation drawer
@SuppressWarnings("unused")
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
Fragment newFragment;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (position) {
case 0:
newFragment = new f1();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 1:
newFragment = new f2();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 2:
newFragment = new f3();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 3:
newFragment = new f4();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
}
//DrawerList.setItemChecked(position, true);
setTitle(ListTitles[position]);
DrawerLayout.closeDrawer(DrawerList);
}
@Override
public void setTitle(CharSequence title) {
Title = title;
getActionBar().setTitle(Title);
}
*/
}
fragment1 的源代码:
public class testfragment extends Fragment {
ListView DrawerList;
private String[] ListTitles;
private List<String> mDataSourceList = new ArrayList<String>();
private List<FragmentTransaction> mBackStackList = new ArrayList<FragmentTransaction>();
public static Fragment newInstance(Context context) {
testfragment f = new testfragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_testfragment, null);
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//add data to ListView
for(int i=0, count=20; i<count; i++){
mDataSourceList.add("record" + i);
}
ListView listView = (ListView) getActivity().findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, mDataSourceList));
}
public android.app.Fragment commit() {
// TODO Auto-generated method stub
return null;
}
}
片段 2 也有类似的代码,但列表中的项目不同。 根据位值,两个片段都完美地加载到抽屉中。现在当有人点击抽屉列表项时,我想在主布局中加载相应的片段。
我的问题是我应该在哪里以及如何为列表视图添加 onclicklistener 以便在抽屉关闭后在主要内容中生成适当的片段?在对应的fragment1/2或者主activity中。
我还有另一种方法可以仅在 man 活动中使用不同的列表来做同样的事情。 我能做的是——在主要活动中声明两个不同的列表视图及其 onclicklistener 切换案例。然后我可以在抽屉中使用 if-else 条件加载这些列表视图。
对于一个灵活、结构良好且设计良好的应用程序来说,有什么更好的方法。
这是我问题的另一部分:Load navigation drawer slider with Dynamic Fragments
【问题讨论】:
-
为什么会有一些注释代码
DrawerList.setOnItemClickListener(new DrawerItemClickListener());?? -
该代码使用列表视图填充抽屉,正如谷歌导航抽屉文档中所建议的那样。我不想这样做,所以我把它注释掉了。相反,我想添加自己的片段。
-
您的片段与列表无关。您可以有一个自定义列表视图。您可以拥有自己的自定义片段
-
是的,这就是我正在做的......在抽屉中添加我的自定义片段。当我单击抽屉中的某个项目时,我对如何替换主要内容视图中的片段感到困惑。
-
你的名单在哪里??它在 LaunchActivity 中评论。
标签: android android-fragments navigation-drawer