久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Viewpager 標(biāo)簽片段 &正常片段

Viewpager Tabs Fragment amp; Normal Fragments(Viewpager 標(biāo)簽片段 amp;正常片段)
本文介紹了Viewpager 標(biāo)簽片段 &正常片段的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我為我糟糕的代碼和糟糕的知識(shí)提前道歉.我最近才開(kāi)始使用 Android 和 Java 進(jìn)行編碼

我的問(wèn)題是我有導(dǎo)航抽屜和標(biāo)簽.雖然如果我從導(dǎo)航抽屜中選擇加載到片段中的東西,它不會(huì)顯示.僅顯示選項(xiàng)卡.

我的計(jì)劃是一個(gè)導(dǎo)航抽屜選項(xiàng)卡里面應(yīng)該有 3 個(gè)選項(xiàng)卡,其余的只是沒(méi)有選項(xiàng)卡的普通頁(yè)面.

MainActivity.java

Activity.xml:

活動(dòng)中的代碼:

公共類(lèi) MainActivity 擴(kuò)展 AppCompatActivity實(shí)現(xiàn) NavigationView.OnNavigationItemSelectedListener {@覆蓋protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);工具欄工具欄 = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(工具欄);DrawerLayout 抽屜 = (DrawerLayout) findViewById(R.id.drawer_layout);ActionBarDrawerToggle 切換 = 新的 ActionBarDrawerToggle(這個(gè),抽屜,工具欄,R.string.navigation_drawer_open,R.string.navigation_drawer_close);抽屜.setDrawerListener(toggle);切換.syncState();NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);navigationView.setNavigationItemSelectedListener(this);getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();}@覆蓋公共無(wú)效 onBackPressed() {DrawerLayout 抽屜 = (DrawerLayout) findViewById(R.id.drawer_layout);if (drawer.isDrawerOpen(GravityCompat.START)) {抽屜.closeDrawer(GravityCompat.START);} 別的 {super.onBackPressed();}}@SuppressWarnings("StatementWithEmptyBody")@覆蓋public boolean onNavigationItemSelected(MenuItem item) {//處理導(dǎo)航視圖項(xiàng)目點(diǎn)擊這里.int id = item.getItemId();如果(id == R.id.fragment_1){getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();} else if (id == R.id.fragment_2) {getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithoutTabs()).commit();} else if (id == R.id.fragment_3) {getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new Fragment()).commit();}DrawerLayout 抽屜 = (DrawerLayout) findViewById(R.id.drawer_layout);抽屜.closeDrawer(GravityCompat.START);返回真;}}

也就是說(shuō),如您所見(jiàn),我將 FrameLayout 中的片段替換為 id fragmentContainer.

所有與帶有標(biāo)簽的片段相關(guān)的邏輯,都封裝在FragmentWithTabs中:

公共類(lèi) FragmentWithTabs 擴(kuò)展 Fragment {@Nullable@覆蓋public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 容器, @Nullable Bundle savedInstanceState) {查看 rootView = inflater.inflate(R.layout.fragment_with_tabs, container, false);ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.viewPager);TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.tabLayout);viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {@覆蓋公共片段getItem(int位置){return new SubFragment(position == 0? Color.BLUE : position == 1? Color.WHITE : Color.RED);}@覆蓋公共 CharSequence getPageTitle(int position) {返回位置+"";}@覆蓋公共 int getCount() {返回 3;}});tabLayout.setupWithViewPager(viewPager);返回根視圖;}}

希望對(duì)你有幫助

I apologize in beforehand for my bad code and bad knowledge. I have just recently started coding in Android and Java

My problem is that I have Navigation Drawer and Tabs. Although if I choose something from the navigation drawer which loads up in a Fragment it doesn't show. Only the tabs are showing.

My plan is that one navigation drawer tab shall have 3 tabs inside and the rest just normal pages without tabs.

MainActivity.java

http://pastebin.com/TV7aWy9c

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#000000" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="se.themeister.hello.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:background="#009900"
            android:layout_below="@id/tab_layout"/>

    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

I don't know if that's enough or if more information or code is needed

解決方案

The problem with your code is that your ViewPager with TabLayout should be part of the Fragment, not part of the Activity. And NavigationDrawer should just replace one fragment by another in the viewport (i.e. in the container). Now, in your code, you're trying to somehow insert your new Fragment into the ViewPager.

Here's a very basic app with Navigation Drawer, one Fragment with Tabs and rest fragments without tabs, which you can use as an example:

Activity.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />    
</android.support.v4.widget.DrawerLayout>

The code in the activity:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.fragment_1) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
        } else if (id == R.id.fragment_2) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithoutTabs()).commit();
        } else if (id == R.id.fragment_3) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new Fragment()).commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

I.e., as you see, I'm replacing fragments in the FrameLayout with id fragmentContainer.

All logic related to the fragment with tabs, incapsulated in FragmentWithTabs:

public class FragmentWithTabs extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_with_tabs, container, false);
        ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.viewPager);
        TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.tabLayout);

        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return new SubFragment(position == 0? Color.BLUE : position == 1? Color.WHITE : Color.RED);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return position+"";
            }

            @Override
            public int getCount() {
                return 3;
            }
        });

        tabLayout.setupWithViewPager(viewPager);
        return rootView;
    }
}

I hope, it helps

這篇關(guān)于Viewpager 標(biāo)簽片段 &amp;正常片段的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周?chē)h(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产精品亚洲片在线播放 | 久久久久香蕉视频 | 亚洲国产精品成人综合久久久 | 在线看日韩av | 精品久久久久久久久久久 | 一级a性色生活片久久毛片波多野 | 日韩精品在线观看一区二区三区 | 国产丝袜一区二区三区免费视频 | 色视频网站 | 亚洲精品一区二区二区 | 国产一区二区 | 精品国产第一区二区三区 | 国产高清精品在线 | 欧美色专区 | 国内精品久久久久久影视8 最新黄色在线观看 | 大学生a级毛片免费视频 | 男女网站免费 | 欧美日韩久久精品 | 男人天堂网av | 黑人一级黄色大片 | 欧美一区二区在线观看 | 欧美久久一区 | 午夜日韩视频 | 99热首页 | 在线视频一区二区三区 | 99久久精品国产毛片 | 国产精品久久久久久久久久 | 日韩二| 亚洲一区二区三区久久久 | 黄免费观看 | 天堂资源最新在线 | 午夜理伦三级理论三级在线观看 | 日韩欧美中文字幕在线观看 | 欧美激情精品久久久久久 | 成人精品国产免费网站 | 成人在线 | 99国内精品久久久久久久 | 精品1区 | 青青久在线视频 | 国产一区二区日韩 | 天堂在线91 |