How To Display Two Fragments At The Same Time Using Tablayout?
I use Android Tab Example with two tabs, view pager and fragments (structure on the image): For get fragments i use the solution from this post. When my device is rotation i want
Solution 1:
In activity_main.xml
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"
><FrameLayoutandroid:id="@+id/main_content"android:layout_width="match_parent"android:layout_height="match_parent"
></FrameLayout></FrameLayout>
In MainActivity.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.annotation.NonNull;
publicclassMainActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(0);
}
publicvoidreplaceFragment(int position) {
Fragmentfragment=null;
switch (position) {
case0:
fragment = newTabOneFragment();
break;
case1:
fragment = newTabTwoFragment();
break;
default:
break;
}
if (null != fragment) {
FragmentManagerfragmentManager= MainActivity.this.getSupportFragmentManager();
FragmentTransactiontransaction= fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
in TabOneFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
publicclassTabOneFragmentextendsFragment {
privateViewinflatedView=null;
publicTabOneFragment() {
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflatedView = inflater.inflate(R.layout.fragment_tab_one, container, false);
returnthis.inflatedView;
}
}
in TabTwoFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
publicclassTabTwoFragmentextendsFragment {
privateViewinflatedView=null;
publicTabTwoFragment() {
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflatedView = inflater.inflate(R.layout.fragment_tab_two, container, false);
returnthis.inflatedView;
}
}
Post a Comment for "How To Display Two Fragments At The Same Time Using Tablayout?"