Z
Z
ZAndrew2015-08-28 18:55:24
Android
ZAndrew, 2015-08-28 18:55:24

How to change fragment A to fragment B with redrawing B, if it has already been created. And then B to A without redrawing?

Faced such a problem. There is a graphics layer for fragments A and B:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" >
    <LinearLayout android:id="@+id/list_manager_container_layout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_centerHorizontal="true" 
        android:orientation="horizontal">
   </LinearLayout>
</RelativeLayout>

First, fragment A is created. When you click on the button in fragment A, fragment B should appear in its place. Conversely, when you click on the button in fragment B, fragment A should appear in its place.
FragmentManager.replace () is not suitable, because to. fragment A does not need to be redrawn when "returning" from B, and fragment B, on the contrary, needs to be created / redrawn every time you switch to it. For these purposes, I created
a method in my Activity that switches fragments:
public void switchFragment(android.support.v4.app.Fragment switchtofrag,
                           android.support.v4.app.Fragment switchfromfrag,
         int switcharea, 
         boolean redraw) {
    android.support.v4.app.FragmentTransaction ft = mVCollectionPageAdapter.mFM.beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    if (switchtofrag.isAdded()) {
      
      if (redraw) {
      ft.detach(switchtofrag);
      ft.attach(switchtofrag);
      }
      
      ft.show(switchtofrag);
      ft.hide(switchfromfrag);
      
    } else {
    
      ft.hide(switchfromfrag);
      ft.add(switcharea, switchtofrag);
      ft.show(switchtofrag);

    }

    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    ft.commit();
  }

Accordingly, when switching from A to B, I use redraw=true, from B to A, redraw=false.
The problem is that when switching from A to B, if it already exists - switchtofrag.isAdded()=true, first the old, unredrawn version of B appears for a fraction of a second and then instantly redrawn. It looks very unsightly. Trials with rearrangement of ft.show, ft.hide did
not bring anything. How to get rid of such an effect? Perhaps there are other ways to solve such problems? I appeal to Android specialists.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bolshakov, 2015-08-28
@enq3

On a vskidka: when you click on the button in fragment B, call getActivity().getSupportFragmentManager().popBackStack();
I correctly understood that when you click on the button in fragment B, fragment A should appear in its place , in fact, you need to go back?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/fragment">
</FrameLayout>

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new MainActivityFragment()).commit();
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        btn = (Button) view.findViewById(R.id.button2);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new BlankFragment()).addToBackStack(null).commit();
            }
        });
        return view;
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        btn = (Button) view.findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });
        return view;
    }

Try it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question