【发布时间】:2021-03-21 12:28:29
【问题描述】:
我有一个问题。 我正在处理 1 个活动(mainActivity),我只是用导航视图切换片段。 我的一个片段有一个选项卡式布局。但不知何故,标签没有显示出来。
这是我的 fragment_home.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_constraintBottom_toTopOf="parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@color/beige">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_screen"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@drawable/tab_background"
app:tabIndicatorColor="@color/black"
app:tabSelectedTextColor="@color/black"
android:layout_below="@+id/toolbar">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipes_tab" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fav_tab" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tabLayout">
</androidx.viewpager.widget.ViewPager>
这是我的 HomeFragment.java
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
TabItem tab1 = (TabItem) view.findViewById(R.id.tab1);
TabItem tab2 = (TabItem) view.findViewById(R.id.tab2);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
PageAdapter pageAdapter = new PageAdapter(getFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0)
{
pageAdapter.notifyDataSetChanged();
}
else if(tab.getPosition() == 1)
{
pageAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
选项卡应该只打印出“Hello blank fragment”,如您在以下 .xml 和 .java 代码中所见:
fragment_tab1.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tab1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
tab1.java
public class tab1 extends Fragment {
public tab1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
}
提前致谢
【问题讨论】:
-
如果我理解正确,你想在片段内部而不是在主片段中显示 TabLayout?
-
完全正确,我在主要活动 onCreate() 方法中调用了这个片段。它可以工作,但不知何故标签无法正常工作。
-
好的!我有一个目标相同的项目。等一下! ;)
-
顺便说一下,我建议你总是以大写字母开头的类名。看看它是否有效,并记住在答案左侧的按钮中将答案设置为正确。如果它是您正在寻找的东西。
标签: java android android-fragments tabs