[英]Android Tab name is disappearing when swiping
I started a new project on Android Studio using Tabbed Activity.我使用选项卡式活动在 Android Studio 上开始了一个新项目。 I did not change any code but what I observed is, when you swipe between the tabs then the name of the current active tab is not showing.
我没有更改任何代码,但我观察到的是,当您在选项卡之间滑动时,当前活动选项卡的名称未显示。 I want the name to be shown up always.
我希望名称始终显示。 How can I do this?
我怎样才能做到这一点?
I post the default code which is delivered automatically by Android Studio.我发布了由 Android Studio 自动提供的默认代码。 I think the method
getPageTitle
is responsible for this.我认为
getPageTitle
方法对此负责。
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.customerservice.R;
/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3};
private final Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]);
}
@Override
public int getCount() {
// Show 2 total pages.
return 3;
}
}
Open a new project打开一个新项目
choose "Tabbed Activity"选择“标签式活动”
open "SectionsPagerAdapter" Java class:打开“SectionsPagerAdapter”Java class:
Click on "Tab 1" and "Tab 2"单击“选项卡 1”和“选项卡 2”
you can see that the names of Tabs are stored in String resources you can find it here:你可以看到 Tabs 的名称存储在 String 资源中,你可以在这里找到它:
then you can change it for example: Hello Tab 1 Hello Tab 2那么您可以更改它,例如:Hello Tab 1 Hello Tab 2
This is the result:这是结果:
Edited:编辑:
If you want to see all the tabs names at the same time, you can use "ViewPager2", read this documentation:如果您想同时查看所有选项卡名称,可以使用“ViewPager2”,阅读此文档:
Create swipe views with tabs using ViewPager2 使用 ViewPager2 创建带有选项卡的滑动视图
I hope this is helpful.我希望这是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.