1

从第一个选项卡切换到第二个选项卡后出现主要问题。在两个选项卡中应该是不同的 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>

感谢您的阅读,任何帮助将不胜感激。

4

2 回答 2

1
  /** Detaches the androidfragment if exists */

                if(secondFragment !=null)
                    ft.detach(secondFragment );

                /** Detaches the applefragment if exists */
                if(firstFragment !=null)
                    ft.detach(firstFragment );

使用这个分离片段 看这个

于 2013-03-19T09:23:22.670 回答
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;
        }
    }
于 2013-03-19T08:21:18.487 回答