我想根據某些條件在我的抽屜中加載不同的片段。你說:在Navigation-Drawer的抽屜中加載自定義片段並在主佈局中打開相應的片段
if(bit =0){
load fragment0();
}
else if(bit =1){
load fragment1();
}
而且每個片段(fragment0 &片段1)包含它自己的項和按鈕/視圖列表。 然後根據項目在抽屜碎片中點擊,我想在主佈局中加載相應的碎片。 下面是我正在使用的代碼:
這是在抽屜爲片段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);
}
*/
}
源代碼加載不同片段的主要活動:
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也將類似的代碼,但在列表中有不同的項目。 這兩個碎片都根據位值在抽屜中完美加載。現在當有人點擊抽屜列表項時,我想在主佈局中加載相應的片段。
我的問題是Where &我應該如何爲listview添加onclicklistener,以便在抽屜關閉後在主內容中生成適當的片段?在相應的fragment1/2或主要活動中。
我也有另一種方法來做同樣的事情,只使用man活動中的不同列表。 我能做的是 - 在主要活動中聲明兩個不同的列表視圖以及他們的onclicklistener開關案例。然後我可以使用抽屜中的if-else條件加載這些列表視圖。
對於靈活,結構良好,設計良好的應用程序來說,更好的方法是什麼?
這裏是我的問題的另一部分:Load navigation drawer slider with Dynamic Fragments
爲什麼你有一些註釋代碼'DrawerList.setOnItemClickListener(new DrawerItemClickListener());'? – Raghunandan
該代碼使用listview填充抽屜,如google的抽屜文檔中所建議的。我不想這樣做,所以我評論說。相反,我想添加我自己的片段。 –
你的片段與列表無關。你可以有一個自定義的列表視圖。你可以有你自己的自定義片段 – Raghunandan