我有一个有ActionBarActivity
的应用程序,里面我有两个代表每个标签的片段......事情是我在fragment1上有一个列表,其中包含两个值“teste”和“teste2”
但是,当我更改为fragment2(tab2)并再次返回tab1时,它会重复列表中的值,如“teste”,“teste2”,“teste”,“teste2”,我该如何防止这种情况?
TabActionBarActivity:
public class TabActionBarActivity extends Activity {
String contextoId;
String BuId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_action_bar);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label1 = getResources().getString(R.string.label1);
Tab tab = actionBar.newTab();
tab.setText(label1);
TabListener<Tab1Fragment> tl = new TabListener<Tab1Fragment>(this, label1, Tab1Fragment.class);
tab.setTabListener(tl);
actionBar.addTab(tab);
String label2 = getResources().getString(R.string.label2);
tab = actionBar.newTab();
tab.setText(label2);
TabListener<Tab2Fragment> tl2 = new TabListener<Tab2Fragment>(this, label2, Tab2Fragment.class);
tab.setTabListener(tl2);
actionBar.addTab(tab);
BuId = getIntent().getExtras().getString("BUId");
contextoId = getIntent().getExtras().getString("CId");
Log.i("BUIdTabFrag", BuId);
}
private class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tab_action_bar, menu);
return true;
}
希望你能帮助我
“EDIT”
如果我这样做:
ft.replace(android.R.id.content, mFragment, mTag).commit();
它给了我一个错误:
log: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.icenterv2/com.example.admin.TabActionBarActivity}: java.lang.IllegalStateException: commit already called
就在这一行:actionBar.addTab(tab);
和
ft.replace(android.R.id.content, mFragment, mTag);
这种方式再次重复
答案 0 :(得分:0)
问题不在您的ListView
代码中,而是在您的MainActivity中的ActionBar
代码中。您没有正确附加,分离和替换fragments
。我有同样的问题。发布MainActivity的代码,我可以帮助您
您需要替换
ft.add(android.R.id.content, mFragment, mTag);
带
ft.replace(android.R.id.content, mFragment, mTag).commit();
因此onTabSelected(Tab tab, FragmentTransaction ft)
中的方法应该如下所示
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.replace(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
因为现在它正在将fragment
“添加”到容器中,为您提供重复项。