从第一个选项卡切换到第二个选项卡后出现主要问题。在两个选项卡中应该是不同的 ListViews,但出现此问题我的活动代码是:
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>
感谢您的阅读,任何帮助将不胜感激。