Java Android Tabview更改选项卡文本

Java Android Tabview更改选项卡文本,java,android,android-viewpager,Java,Android,Android Viewpager,大家好,今天我在一个我正在开发的应用程序中使用androids tabview。我想有两个部分,一个叫做New Releases,另一个叫做A-Z。所以我看了一个教程视频,因为我以前从未尝试过使用这个视图。在本教程中,他们使用CharSequence getPageTitle(int位置)获取页面标题。但是我怎么能把它改成新的release和A-Z呢 这是我目前如何设置标签的 activity_main.xml <android.support.constraint.ConstraintL

大家好,今天我在一个我正在开发的应用程序中使用androids tabview。我想有两个部分,一个叫做New Releases,另一个叫做A-Z。所以我看了一个教程视频,因为我以前从未尝试过使用这个视图。在本教程中,他们使用CharSequence getPageTitle(int位置)获取页面标题。但是我怎么能把它改成新的release和A-Z呢

这是我目前如何设置标签的

activity_main.xml

<android.support.constraint.ConstraintLayout 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"
tools:context="studios.p9p.harrop99.tabproject.MainActivity">


<android.support.v4.view.ViewPager
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/view_pager"
    app:layout_constraintBottom_toTopOf="@+id/tabs_indicator"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="8dp" />

<com.hold1.pagertabsindicator.PagerTabsIndicator
    android:layout_width="0dp"
    android:layout_height="64dp"
    android:id="@+id/tabs_indicator"
    android:background="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteX="0dp"

    app:tab_indicator="top_bar"
    app:tab_indicator_scale_type="centerInside"
    app:tab_lock_expanded="true"
    />
适配器类

public class HarropPageAdapter extends PagerAdapter {

List<Model> models;
Context context;

 public HarropPageAdapter(List<Model> models,Context context){
 this.models = models;
 this.context = context;
 }
public CharSequence getPageTitle(int position){
;
return "Page "+ (position+1)  ;
}


@Override
public int getCount() {
    return models.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewGroup)container).removeView((View)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater =  LayoutInflater.from(context);
    View itemView =inflater.inflate(R.layout.layout_item,container, false);
    TextView textView = itemView.findViewById(R.id.titleapp);
    textView.setText(models.get(position).getTitle());
    container.addView(itemView);
    return itemView;
}
公共类HarroPageAdapter扩展了PagerAdapter{
列出模型;
语境;
公共HarropPageAdapter(列表模型、上下文){
这个。模型=模型;
this.context=上下文;
}
公共字符序列getPageTitle(int位置){
;
返回“页面”+(位置+1);
}
@凌驾
public int getCount(){
返回models.size();
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回视图==对象;
}
@凌驾
公共项(视图组容器、int位置、对象){
((视图组)容器)。移除视图((视图)对象);
}
@凌驾
公共对象实例化项(视图组容器,int位置){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图项视图=充气机。充气(右布局。布局项,容器,假);
TextView TextView=itemView.findViewById(R.id.titleapp);
textView.setText(models.get(position.getTitle());
container.addView(itemView);
返回项目视图;
}

}

好的,我做了这件事,不知道这是否是正确的方法,但它确实有效

public CharSequence getPageTitle(int position){
if (position==0) {
    return "New Releases";
}
return "A-Z";
}

public class HarropPageAdapter extends PagerAdapter {

List<Model> models;
Context context;

 public HarropPageAdapter(List<Model> models,Context context){
 this.models = models;
 this.context = context;
 }
public CharSequence getPageTitle(int position){
;
return "Page "+ (position+1)  ;
}


@Override
public int getCount() {
    return models.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewGroup)container).removeView((View)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater =  LayoutInflater.from(context);
    View itemView =inflater.inflate(R.layout.layout_item,container, false);
    TextView textView = itemView.findViewById(R.id.titleapp);
    textView.setText(models.get(position).getTitle());
    container.addView(itemView);
    return itemView;
}
public CharSequence getPageTitle(int position){
if (position==0) {
    return "New Releases";
}
return "A-Z";