从第一个标签切换到第二个标签后出现主要问题。在两个选项卡中应该是不同的ListView,但是显示此issue我的活动代码是:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabFirst = actionBar.newTab().setText("First");
ActionBar.Tab tabSecond = actionBar.newTab().setText("Second");
tabFirst.setTabListener(new tabListener());
tabSecond.setTabListener(new tabListener());
actionBar.addTab(tabFirst);
actionBar.addTab(tabSecond);
}
protected class tabListener implements ActionBar.TabListener {
ParkFragment firstFragment;
ParkFragment secondFragment;
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()){
case 0:
if (firstFragment == null){
firstFragment = new ParkFragment("Here is first url");
ft.add(R.id.parkfragment, firstFragment,"FIRST");
}
else{
ft.attach(firstFragment);
}
break;
case 1:
if (secondFragment == null){
secondFragment = new ParkFragment("here is the second one");
ft.add(R.id.parkfragment, secondFragment, "SECOND");
}
else{
ft.attach(secondFragment);
}
break;
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
public class ParkFragment extends ListFragment {
private ArrayList<Cinemas> cinema;
private CinemasAdapter cinemaAdapter;
private View v;
private String url;
public ParkFragment (String cinema){
url = cinema;
}
public void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cinema = new Handler().handle(url);
cinemaAdapter = new CinemasAdapter(MainActivity.this, R.layout.movie_data_row, cinema);
setListAdapter(cinemaAdapter);
}
}
这是容器xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/parkfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
感谢您的阅读,我们将不胜感激。
答案 0 :(得分:1)
/** Detaches the androidfragment if exists */
if(secondFragment !=null)
ft.detach(secondFragment );
/** Detaches the applefragment if exists */
if(firstFragment !=null)
ft.detach(firstFragment );
使用这一个分离片段see this
答案 1 :(得分:0)
您需要添加/附加隐藏另一个片段的片段。
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()){
case 0:
if (firstFragment == null){
firstFragment = new ParkFragment("Here is first url");
ft.add(R.id.parkfragment, firstFragment,"FIRST");
}
else{
ft.attach(firstFragment);
}
if (secondFragment != null)
ft.hide(secondFragment);
break;
case 1:
if (secondFragment == null){
secondFragment = new ParkFragment("here is the second one");
ft.add(R.id.parkfragment, secondFragment, "SECOND");
}
else{
ft.attach(secondFragment);
}
if (firstFragment != null)
ft.hide(firstFragment);
break;
}
}