gpt4 book ai didi

android - 如果已经添加了 fragment ......如何重新加载它

转载 作者:行者123 更新时间:2023-11-30 02:54:55 25 4
gpt4 key购买 nike

我在做什么::

  • 我正在从 actionbar 启动一个项目的 fragment onClick

发生了什么::

  • 我能够第一次启动 fragment 。

  • 现在,当我第二次单击操作栏菜单项时。我是得到日志中显示的错误

  • 应该发生的是我应该重新加载 fragment ,如果它已经在容器中

  • 如果 fragment 已经添加了如何重新加载

MainActivity.java

public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{

private DrawerLayout drawlayout=null;
private ListView listview=null;
private ActionBarDrawerToggle actbardrawertoggle=null;

FragmentTransaction ft1 ;
FragSearch fragSearch=new FragSearch();
mFragSortingHome fragment1=new mFragSortingHome();

private String[] myfriendname=null;
private int[] photo=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_activity_main);
//Code moved to a seperate function from onCreate with a call in it for clarity
drawerConfig(savedInstanceState);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actbardrawertoggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

if(item.getItemId()==android.R.id.home)
{
if(drawlayout.isDrawerOpen(listview))
{
drawlayout.closeDrawer(listview);
}
else {
drawlayout.openDrawer(listview);
}
}else if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
ft1.add(R.id.content_frame, fragSearch);
ft1.addToBackStack(null);
ft1.commit();
return true;
}
return super.onOptionsItemSelected(item);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.actionbar_sort_menu, menu);

return super.onCreateOptionsMenu(menu);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
selectItem(position);

}

private void drawerConfig(Bundle savedInstanceState) {
// TODO Auto-generated method stub
myfriendname = new String[] { "Home", "Login","List", "Photo", "Filter" };
photo = new int[] { R.drawable.ic_action_map, R.drawable.ic_action_accounts, R.drawable.ic_action_view_as_list, R.drawable.ic_action_photo, R.drawable.ic_action_photo};

drawlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
listview = (ListView) findViewById(R.id.listview_drawer);

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);

drawlayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawlayout.setBackgroundColor(Color.WHITE);

DrawerMenuListAdapter menuadapter=new DrawerMenuListAdapter(getApplicationContext(), myfriendname, photo);
listview.setAdapter(menuadapter);

actbardrawertoggle= new ActionBarDrawerToggle(this, drawlayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}

public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);

}

};
drawlayout.setDrawerListener(actbardrawertoggle);

listview.setOnItemClickListener(this);

if (savedInstanceState == null) {
selectItem(0);
}
}

private void selectItem(int position) {

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

// Locate Position
switch (position) {
case 0:
mFragSortingHome fragment1=new mFragSortingHome();
ft.replace(R.id.content_frame, fragment1);/*
Bundle b = new Bundle();
b.putString("name",myfriendname[position]);
b.putInt("photo",photo[position]);
fragment1.setArguments(b);*/
break;
case 1:
FilterBuffetFragment fragment2=new FilterBuffetFragment();
ft.replace(R.id.content_frame, fragment2);/*
Bundle b1 = new Bundle();
b1.putString("name",myfriendname[position]);
b1.putInt("photo",photo[position]);
fragment2.setArguments(b1);*/
break;
case 2:
mFragment3 fragment3=new mFragment3();
ft.replace(R.id.content_frame, fragment3);/*
Bundle b2 = new Bundle();
b2.putString("name",myfriendname[position]);
b2.putInt("photo",photo[position]);
fragment3.setArguments(b2);*/
break;
case 3:
mFragment4 fragment4=new mFragment4();
ft.replace(R.id.content_frame, fragment4);/*
Bundle b3 = new Bundle();
b3.putString("name",myfriendname[position]);
b3.putInt("photo",photo[position]);
fragment4.setArguments(b3);*/
break;
case 4:
FilterBuffetFragment fragment5=new FilterBuffetFragment();
ft.replace(R.id.content_frame, fragment5);
ft.addToBackStack(null);/*
Bundle b4 = new Bundle();
b4.putString("name",myfriendname[position]);
b4.putInt("photo",photo[position]);
fragment5.setArguments(b4);*/
break;

}
ft.commit();
listview.setItemChecked(position, true);
/*setTitle(myfriendname[position]);*/
drawlayout.closeDrawer(listview);
}

}

我正在加载操作栏项目的 fragment oncick 的代码是

}else if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
ft1.add(R.id.content_frame, fragSearch);
ft1.addToBackStack(null);
ft1.commit();
return true;

日志::

05-04 00:42:01.896: E/AndroidRuntime(2253): FATAL EXCEPTION: main
05-04 00:42:01.896: E/AndroidRuntime(2253): java.lang.IllegalStateException: Fragment already added: FragSearch{415a4748 #1 id=0x7f04003e}
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1159)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.os.Handler.handleCallback(Handler.java:605)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.os.Looper.loop(Looper.java:137)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-04 00:42:01.896: E/AndroidRuntime(2253): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 00:42:01.896: E/AndroidRuntime(2253): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 00:42:01.896: E/AndroidRuntime(2253): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-04 00:42:01.896: E/AndroidRuntime(2253): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-04 00:42:01.896: E/AndroidRuntime(2253): at dalvik.system.NativeStart.main(Native Method)

最佳答案

使用 FragmentManager.findFragmentByIdFragmentManager.findFragmentByTag 查找您已添加的 fragment 。如果这些方法返回 null,则 fragment 尚未添加,您可以像现在一样继续操作。当这些方法找到该 fragment 时,您就有了一个可以使用的引用:设计您的 fragment 类以具有 reload 方法或类似方法,您可以在这种情况下调用它。

关于android - 如果已经添加了 fragment ......如何重新加载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com