选项卡布局显示空白片段视图,并在选项卡更改后消失

时间:2018-01-16 09:38:14

标签: android android-fragments tabs android-tablayout

我根据我的要求使用没有viewpager的Tablayout,并希望使用多个标签。这里我的应用程序第一次正常工作或者在点击最后几个片段之后使其变为空白我可以看到视图,如果我将其最小化以最大化或解锁设备或关闭应用程序并重新启动是选项。提前谢谢。

<RelativeLayout 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">

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/header"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:visibility="gone"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="scrollable"
        app:tabGravity="fill" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/header"
    android:orientation="vertical">

    <include layout="@layout/content_main" />


</LinearLayout>

和我的代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
            setContentView(R.layout.app_bar_main);
            activityComponent().inject(this);
            instance = this;
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            toolbar.setTitle("" + EPreference.getInstance().getStringValue(EPreference.EVENTNAME));
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setIcon(R.mipmap.ic_launcher);
            tabViewGroup = (ViewGroup) findViewById(R.id.content_main);

            tabLayout = (TabLayout) findViewById(R.id.tab_layout);
            tabLayout.setVisibility(View.VISIBLE);
            mMainPresenter.attachView(this);
            if (!EApplication.get().isLaoggedin) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setCancelable(false);
                progressDialog.setMessage("Loading...");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDialog.setProgress(0);
                progressDialog.show();
                mMainPresenter.syncAllDataAtOnce();

            }


            bindWidgetsWithAnEvent();
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            try {
                if (tabLayout.getTabCount() > 0)
                    tabLayout.removeAllTabs();
            } catch (Exception e) {
                e.printStackTrace();
            }

            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_1)), true);
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_3)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_2)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_4)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_5)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_6)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_7)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_9)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_8)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_10)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_11)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_12)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_13)));
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.demo_tab_14)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindWidgetsWithAnEvent() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setCurrentTabFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }



    private void setCurrentTabFragment(int tabPosition) {
        String type = "";
        String sender_id = "";
        String wallStr = "false", guestStr = "false", chatStr = "false", OneToOne = "true";
        try {
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                type = extras.getString("type");
                if (type.equalsIgnoreCase("Wall")) {
                    wallStr = "true";
                } else if (type.equalsIgnoreCase("Guest")) {
                    guestStr = "true";
                } else if (type.equalsIgnoreCase("Chat")) {
                    sender_id = extras.getString("other");
                    OneToOne = extras.getString("OneToOne");
                    chatStr = sender_id;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Bundle bundle = new Bundle();
        switch (tabPosition) {
            case 0:
                WallFragment wallFragment = WallFragment.newInstance();
                bundle.putString("page_source", wallStr);
                wallFragment.setArguments(bundle);
                replaceFragment(wallFragment);
                break;

            case 1:
                EventFragment eventFragment = EventFragment.newInstance();
                replaceFragment(eventFragment);
                break;

            case 2:
                GuestFragment guestFragment = GuestFragment.newInstance();
                bundle.putString("page_source", guestStr);
                guestFragment.setArguments(bundle);
                replaceFragment(guestFragment);
                break;
            case 3:
                GallleryGridFragment gallleryGridFragment = GallleryGridFragment.newInstance();
                replaceFragment(gallleryGridFragment);
                break;
            case 4:
                ChatFragment chatFragment = ChatFragment.newInstance();
                bundle.putString("page_source", chatStr);
                bundle.putString("OneToOne", OneToOne);
                chatFragment.setArguments(bundle);
                replaceFragment(chatFragment);
                break;
            case 5:
                GiftFragment giftFragment = GiftFragment.newInstance();
                replaceFragment(giftFragment);
                break;
            case 6:
                FeedbackFragment feedbackFragment = FeedbackFragment.newInstance();
                replaceFragment(feedbackFragment);
                break;
            case 7:
                ExpenseFragment expenseFragment = ExpenseFragment.newInstance();
                replaceFragment(expenseFragment);
                break;
            case 8:
                HotelRoomFragment hotelRoomFragment = HotelRoomFragment.newInstance();
                replaceFragment(hotelRoomFragment);
                break;
            case 9:
                replaceFragment(VendorFragment.newInstance());
                break;
            case 10:
                replaceFragment(ParticularFragment.newInstance());
                break;
            case 11:
                replaceFragment(ProfileFragment.newInstance());
                break;
            case 12:
                replaceFragment(BroadcastFragment.newInstance());
                break;
            case 13:
                replaceFragment(NearByFragment.newInstance());
                break;

        }
    }

    public void replaceFragment(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_main, fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();
    }

1 个答案:

答案 0 :(得分:0)

试试这个 活动类

 TabLayout tabLayout;
int values;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {

        setContentView(R.layout.activity_main);
         tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setVisibility(View.VISIBLE);

        bindWidgetsWithAnEvent();
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        try {
            if (tabLayout.getTabCount() > 0)
                tabLayout.removeAllTabs();
        } catch (Exception e) {
            e.printStackTrace();
        }
        values=1;
        openFragment(Event_Fragment.class);

        tabLayout.addTab(tabLayout.newTab().setText("1"), true);
        tabLayout.addTab(tabLayout.newTab().setText("2"));
        tabLayout.addTab(tabLayout.newTab().setText("3"));
        tabLayout.addTab(tabLayout.newTab().setText("4"));
        tabLayout.addTab(tabLayout.newTab().setText("5"));
        tabLayout.addTab(tabLayout.newTab().setText("6"));
        tabLayout.addTab(tabLayout.newTab().setText("7"));
        tabLayout.addTab(tabLayout.newTab().setText("8"));
        tabLayout.addTab(tabLayout.newTab().setText("9"));
        tabLayout.addTab(tabLayout.newTab().setText("10"));
        tabLayout.addTab(tabLayout.newTab().setText("11"));
        tabLayout.addTab(tabLayout.newTab().setText("12"));
        tabLayout.addTab(tabLayout.newTab().setText("13"));
        tabLayout.addTab(tabLayout.newTab().setText("14"));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void bindWidgetsWithAnEvent() {
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            setCurrentTabFragment(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}



private void setCurrentTabFragment(int tabPosition) {
    String type = "";
    String sender_id = "";
    String wallStr = "false", guestStr = "false", chatStr = "false", OneToOne = "true";
    try {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            type = extras.getString("type");
            if (type.equalsIgnoreCase("Wall")) {
                wallStr = "true";
            } else if (type.equalsIgnoreCase("Guest")) {
                guestStr = "true";
            } else if (type.equalsIgnoreCase("Chat")) {
                sender_id = extras.getString("other");
                OneToOne = extras.getString("OneToOne");
                chatStr = sender_id;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Bundle bundle = new Bundle();
    switch (tabPosition) {
        case 0:
            values=1;
            openFragment(Event_Fragment.class);
           /* Event_Fragment wallFragment = Event_Fragment.newInstance();
            bundle.putString("page_source", wallStr);
            wallFragment.setArguments(bundle);
            replaceFragment(wallFragment);*/
            break;

        case 1:
            values=2;
            openFragment(Event_Fragment.class);
            break;

        case 2:
            values=3;
            openFragment(Event_Fragment.class);
            break;
        case 3:
            values=4;
            openFragment(Event_Fragment.class);
            break;
        case 4:
            values=5;
            openFragment(Event_Fragment.class);
            break;
        case 5:
            values=6;
            openFragment(Event_Fragment.class);
            break;
        case 6:
            values=7;
            openFragment(Event_Fragment.class);
            break;
        case 7:
            values=8;
            openFragment(Event_Fragment.class);
            break;
        case 8:
            values=9;
            openFragment(Event_Fragment.class);
            break;
        case 9:
            values=10;
            openFragment(Event_Fragment.class);
            break;
        case 10:
            values=11;
            openFragment(Event_Fragment.class);
            break;
        case 11:
            values=12;
            openFragment(Event_Fragment.class);
            break;
        case 12:
            values=13;
            openFragment(Event_Fragment.class);
            break;
        case 13:
            values=14;
            openFragment(Event_Fragment.class);
            break;

    }
}
  public void openFragment(Class fragmentClass) {
    try {
        Fragment fragment = (Fragment) fragmentClass.newInstance();
        Bundle bundle = new Bundle();
        bundle.putInt("values", values);
        fragment.setArguments(bundle);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content, fragment, fragmentClass.getSimpleName()).commit();
      //  setTitle(menuItem.getTitle());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

XML

<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:orientation="vertical"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Tab event"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:visibility="gone"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="scrollable"
        app:tabGravity="fill" />

</LinearLayout>
<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

</FrameLayout>

片段XML

<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">
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Tab event"
    android:textAppearance="?android:attr/textAppearanceLarge" />
 </LinearLayout>