Java 我想做一个可滚动的TabWidget
我正在为Java 我想做一个可滚动的TabWidget,java,android,android-tabhost,android-framelayout,tabwidget,Java,Android,Android Tabhost,Android Framelayout,Tabwidget,我正在为TabHost内容添加大量布局 <TabHost android:id="@+id/tab_host" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout an
TabHost
内容添加大量布局
<TabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_mask"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_mask"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_mask"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_mask"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_mask"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_glass"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_hair"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/fragment_mask"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
但是,并非每个
选项卡都能正确地看到。所以,我想让它可以滚动。我可以通过TabLayout
来完成。但是,我想从MainActivity
访问那些布局。因此,我不能使用TabLayout
和ViewPager
。因此,我必须使TabWidget
可滚动。怎么做?或者,有类似的库吗?您可以使用tablayout和viewpager来实现这一点
Layout.xml文件
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="@dimen/_32sdp"
android:layout_margin="@dimen/_8sdp"
android:layout_marginLeft="8sp"
android:background="@drawable/tab_layout_background"
app:tabBackground="@drawable/tab_layout_selector"
app:tabIndicatorHeight="0dp"
app:tabMode="auto"
app:tabPaddingEnd="16dp"
app:tabPaddingStart="16dp"
app:tabRippleColor="@null"
app:tabSelectedTextColor="@color/white"
app:tabTextAppearance="@style/TabTextAppearance" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabLayout"
android:layout_marginLeft="@dimen/_8sdp"
android:layout_marginRight="@dimen/_8sdp"/>
</RelativeLayout>
还有适配器
public class HomeAdapter extends FragmentPagerAdapter {
private int totalTabs;
public HomeAdapter(MainActivity context, FragmentManager fm, int totalTabs) {
super( fm );
this.totalTabs = totalTabs;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new SalesListFragment();
case 1:
return new BuyerListFragment();
default:
return null;
}
}
// this counts total number of tabs
@Override
public int getCount() {
return totalTabs;
}
}
希望它会很丰满。我推荐一些你可能没有注意到的东西。我说过我必须使用我的main活动中的片段
,但是,当我使用ViewPager
时,我必须使用他们片段
类中的片段。我无法从MainActivity
访问这些片段。这就是为什么我必须使用TabHost
或TabWidget
和TabContent
。TabHost和TabWidget在API级别30都不推荐使用。在我的示例中,我使用了两个片段saleslistfragment和BuyerListFragment,使用viewpager。我在Adapter(HomeAdapter)中声明了它们。我想你不知道。我也就此提出了一个问题。访问和也访问。正如他所说的,TabHost
未被弃用TabActivity
已被弃用。关于这个话题,我也问了很多问题,,请遵循Android API文档。它说API 30中不推荐使用tabHost
public class HomeAdapter extends FragmentPagerAdapter {
private int totalTabs;
public HomeAdapter(MainActivity context, FragmentManager fm, int totalTabs) {
super( fm );
this.totalTabs = totalTabs;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new SalesListFragment();
case 1:
return new BuyerListFragment();
default:
return null;
}
}
// this counts total number of tabs
@Override
public int getCount() {
return totalTabs;
}