简体   繁体   中英

FragmentTransaction remains old fragment

I'm new android programming. I want to make slider left or right fragment. eg when button clicked list fragment show and map fragment hide and vice versa.First i added 2 fragment to my framelayout and I use hide/show instead of replace.

However my fragmenttransaction sometimes work and sometimes not work. For example, When hit button map fragment entering(show) by sliding out left and list hides by sliding out right. But sometimes list remains my frame layout. In this situation when I pressed button normally reverse effect occurs but list fragment entering (show) my frame layout and it duplicated.

How can i can fix problem about remains listview.

My codes here

Listview fragment

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"/>

MainActivity.java

public void onClick(View view) {
            if (mapshere) {


                android.support.v4.app.FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();

                ft2.setCustomAnimations(R.anim.map_slide_in_right, R.anim.map_left_evade);

                    ft2.show(list);
                    ft2.hide(map);
                ft2.commit();

                btnList.setImageResource(R.drawable.icon_pin_menu1);

            } else {

                android.support.v4.app.FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();

                ft2.setCustomAnimations(R.anim.map_slide_in_left, R.anim.map_right_evade);

                ft2.show(map);
                ft2.hide(list);

                ft2.commit();


                btnList.setImageResource(R.drawable.icon1);

            }

My Listvie OnCreatView

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment_item_list, container, false);

    // Set the adapter
    mListView = (AbsListView) view.findViewById(android.R.id.list);

    ((AdapterView<ListAdapter>) mListView).setAdapter(getmAdapter());
    mListView.setOnItemClickListener(this);
    return view;


}

As I said before I have already add these fragments in OnCreate function. But I hide list fragment.

Try using

getSupportFragmentManager()
.beginTransaction()
// set your custom in/out animations 
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left,R.anim.slide_out_right)
// replace the content of the layout defined with R.id.content_frame by your fragment.
.replace(R.id.content_frame, fragment)
.commit();

This should solve your issues with multiple instances of fragments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM