pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

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

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

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

MainActivity.java

Activity.xml:

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

公共類 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();}@覆蓋公共無效 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);返回真;}}

也就是說,如您所見,我將 FrameLayout 中的片段替換為 id fragmentContainer.

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

公共類 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);返回根視圖;}}

希望對你有幫助

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;正常片段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(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ù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 304不锈钢无缝管_不锈钢管厂家 - 隆达钢业集团有限公司 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 广州企亚 - 数码直喷、白墨印花、源头厂家、透气无手感方案服务商! | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 板式换热器_板式换热器价格_管式换热器厂家-青岛康景辉 | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 缝纫客| PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 手持式3d激光扫描仪-便携式三维立体扫描仪-北京福禄克斯 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 青岛美佳乐清洁工程有限公司|青岛油烟管道清洗|酒店|企事业单位|学校工厂厨房|青岛油烟管道清洗 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 专业音响设备_舞台音响设备_会议音响工程-首选深圳一禾科技 | 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 3D全息投影_地面互动投影_360度立体投影_水幕灯光秀 | 广东银虎 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 北京亦庄厂房出租_经开区产业园招商信息平台 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 铁盒_铁罐_马口铁盒_马口铁罐_铁盒生产厂家-广州博新制罐 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 污水提升器,污水提升泵,污水提升装置-德国泽德(zehnder)水泵系统有限公司 | 水成膜泡沫灭火剂_氟蛋白泡沫液_河南新乡骏华消防科技厂家 | 深圳办公室装修-写字楼装修设计-深圳标榜装饰公司| 翅片管散热器价格_钢制暖气片报价_钢制板式散热器厂家「河北冀春暖气片有限公司」 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 大型果蔬切片机-水果冬瓜削皮机-洗菜机切菜机-肇庆市凤翔餐饮设备有限公司 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 档案密集架,移动密集架,手摇式密集架,吉林档案密集架-厂家直销★价格公道★质量保证 | 5L旋转蒸发器-20L-50L旋转蒸发器-上海越众仪器设备有限公司 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 千斤顶,液压千斤顶-力良企业,专业的液压千斤顶制造商,shliliang.com | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 万师讲师网-优质讲师培训师供应商,讲师认证,找讲师来万师 |