Viewpager Tabs Fragment amp; Normal Fragments(Viewpager 标签片段 amp;正常片段)
问题描述
我为我糟糕的代码和糟糕的知识提前道歉.我最近才开始使用 Android 和 Java 进行编码
我的问题是我有导航抽屉和标签.虽然如果我从导航抽屉中选择加载到片段中的东西,它不会显示.仅显示选项卡.
我的计划是一个导航抽屉选项卡里面应该有 3 个选项卡,其余的只是没有选项卡的普通页面.
MainActivity.java
Activity.xml
:
活动中的代码:
公共类 MainActivity 扩展 AppCompatActivity实现 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(这个,抽屉,工具栏,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) {//处理导航视图项目点击这里.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
.
所有与带有标签的片段相关的逻辑,都封装在FragmentWithTabs
中:
公共类 FragmentWithTabs 扩展 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
这篇关于Viewpager 标签片段 &正常片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Viewpager 标签片段 &正常片段
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01