Andrew Rahimov
Andrew Rahimov

Reputation: 803

Displaying issues of ListViews in TABs

The main problem appears after switching from first tab to the second one. In both tabs should be different ListViews, but appears this issue My activity code is:

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);

          }
        } 

And here is container xml code:

<?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>

Thank you for reading, any help will be appreciated.

Upvotes: 1

Views: 97

Answers (2)

QuokMoon
QuokMoon

Reputation: 4425

  /** Detaches the androidfragment if exists */

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

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

Use this one detach fragment see this

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You need along with adding/attaching a fragment hiding the other one.

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;
        }
    }

Upvotes: 0

Related Questions