如何使用TabLayout同时显示两个片段?

时间:2018-01-20 09:21:53

标签: java android android-fragments

我使用Android Tab Example两个标签,查看寻呼机和片段(图片上的结构):enter image description here
为获取片段,我使用the solution from this post.
当我的设备旋转时,我想同时显示两个片段。
 我stackoverflow.com/questions/17970021有类似的问题,但我不知道如何将解决方案应用到我的任务中,因为我有TabLayout
如果你知道,你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

在activity_main.xml

<FrameLayout xmlns: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"
    >

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

</FrameLayout>

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

public class MainActivity extends AppCompatActivity {

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

        replaceFragment(0);

  }

       public void replaceFragment(int position) {
        Fragment fragment = null;
        switch (position) {

            case 0:
                fragment = new TabOneFragment();
                break;
            case 1:
                fragment = new TabTwoFragment();
                break;
            default:
                break;
        }

        if (null != fragment) {

            FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_content, fragment);
            transaction.addToBackStack(null);
            transaction.commit();

        }
    }

}
TabOneFragment.java中的

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;

public class TabOneFragment extends Fragment {
    private View inflatedView = null;

    public TabOneFragment() {

    }

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

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

      return this.inflatedView;
  }
}
TabTwoFragment.java中的

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;

public class TabTwoFragment extends Fragment {
    private View inflatedView = null;

    public TabTwoFragment() {

    }

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

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

      return this.inflatedView;
  }
}