0

Ich habe zwei Fragmente.Ersetzen Sie ein Fragment durch ein anderes, gehen Sie zurück zum vorherigen Fragment auf der Symbolleiste zurück Schaltfläche drücken

Das erste Fragment hat eine Button Ansicht, die beim Klicken das zweite Fragment öffnen soll. Der Titel der Symbolleiste sollte zwischen den beiden Fragmenten wechseln (von "Erstes Fragment" zu "Zweites Fragment"). Das zweite Fragment sollte eine "Zurück-Schaltfläche" (Pfeilsymbol links) in der Symbolleiste enthalten, die es dem Benutzer ermöglicht, zum ersten Fragment zurückzukehren.

Wie implementiere ich das?

Ist es außerdem möglich, den Übergang zwischen den beiden Fragmenten so zu animieren, dass das zweite Fragment in einem neuen "Fenster" erscheint (something like this)?

Hier ist meine Aktivitätsklasse:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     if (getSupportActionBar() != null) { 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      getSupportActionBar().setDisplayShowHomeEnabled(true); 
     } 

     if (findViewById(R.id.fragment_container) != null) { 
      if (savedInstanceState != null) { 
       return; 
      } 

      FirstFragment firstFragment = new FirstFragment(); 

      getSupportFragmentManager().beginTransaction() 
        .add(R.id.fragment_container, firstFragment).commit(); 
     } 
    } 
} 

Hier ist die Aktivität Layout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" /> 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

Hier ist meine erste Fragment Klasse:

public class FirstFragment extends Fragment implements View.OnClickListener { 

    public FirstFragment() { 
     // Required empty public constructor 
    } 

    public static FirstFragment newInstance() { 
     FirstFragment fragment = new FirstFragment(); 

     Bundle args = new Bundle(); 
     fragment.setArguments(args); 

     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getArguments() != null) { 
      // 
     } 
    } 

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

     Button openSecondFragmentButton = (Button) view.findViewById(R.id.openSecondFragmentButton); 
     openSecondFragmentButton.setOnClickListener(this); 

     return view; 
    } 

    @Override 
    public void onClick(View v) { 
     Fragment fragment = null; 

     switch (v.getId()) { 
      case R.id.openSecondFragmentButton: 
       // ? 
       break; 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 
} 

Hier ist mein erstes Layout fragment :

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:isScrollContainer="true"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/openSecondFragmentButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Open Second Fragment" /> 

     </RelativeLayout> 

    </RelativeLayout> 

</ScrollView> 

Hier ist meine zweite Fragment Klasse:

public class SecondFragment extends Fragment { 

    public SecondFragment() { 
     // Required empty public constructor 
    } 

    public static SecondFragment newInstance() { 
     SecondFragment fragment = new SecondFragment(); 

     Bundle args = new Bundle(); 
     fragment.setArguments(args); 

     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getArguments() != null) { 
      // 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_second, container, false); 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 
} 

Und schließlich, hier mein zweites Fragment Layout ist:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <GridView 
     android:id="@+id/gridView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="10" /> 

</LinearLayout> 

Wie kann ich die Ergebnisse erzielen, die ich suche?

Antwort

0

Verwenden Sie addToBackStack auf die Transaktion, um das Verhalten "zurück" zu erhalten.

Verwenden Sie setCustomAnimations für die Transaktion, um Ihre Fragmentübergänge zu animieren.

 getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, firstFragment) 
       .addToBackStack(null) 
       .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_out_right, R.anim.slide_in_left) 
       .commit(); 

Um programmatisch auf die Schaltfläche Zurück drücken:

 getSupportFragmentManager().popBackStack(); 

Da Sie offenbar FragmentTransaction die Unterstützung Bibliothek verwenden, sollten die Animationen der alte R.anim Art von Animationen sein. (Die Nicht-Unterstützung FragmentTransaction verwendet die neuere R.animator Art.)

/res/anim/slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

    <translate 
     android:duration="@android:integer/config_mediumAnimTime" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     android:fromXDelta="-100%p" 
     android:toXDelta="0" /> 

</set> 

/res/anim/slide_in_right.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

    <translate 
     android:duration="@android:integer/config_mediumAnimTime" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     android:fromXDelta="100%p" 
     android:toXDelta="0" /> 

</set> 

/res/anim/slide_out_left.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

    <translate 
     android:duration="@android:integer/config_mediumAnimTime" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     android:fromXDelta="0" 
     android:toXDelta="-100%p" /> 

</set> 

/res/anim/slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

    <translate 
     android:duration="@android:integer/config_mediumAnimTime" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     android:fromXDelta="0" 
     android:toXDelta="100%p" /> 

</set> 

Fügen Sie diesen Code onCreate der Tätigkeit:

mToolbar = (Toolbar) view.findViewById(R.id.toolbar); 
    setSupportActionBar(mToolbar); 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

dann außer Kraft setzen onOptionsItemSelected().

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case android.R.id.home: 

      getSupportFragmentManager().popBackStack(); 
      return true; 
     } 

     return false; 
    } 
+0

Wo 'getSupportFragmentManager() popBackStack();' gehen? – user7705019

+0

Siehe aktualisierte Antwort. –

Verwandte Themen