java 在Android Tabview中更改选项卡文本

wgxvkvu9  于 2022-10-30  发布在  Java
关注(0)|答案(1)|浏览(136)

今天,我在一个我正在开发的应用程序中弄乱了Android的tabview。我想有两个部分,一个叫New Releases,另一个叫A-Z。所以我看了一个教程视频,因为我以前从来没有尝试过使用这个视图。在教程中,他们使用CharSequence getPageTitle(int position)来获得页面的标题。但是我怎么才能把它改为New Releases和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"
    />
</android.support.constraint.ConstraintLayout>

活动主类

List<Model>  models = new ArrayList<>();
ViewPager viewpager;
PagerTabsIndicator tabs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initModel();

    viewpager = (ViewPager)findViewById(R.id.view_pager);
    tabs = (PagerTabsIndicator)findViewById(R.id.tabs_indicator);
    viewpager.setAdapter(new HarropPageAdapter(models,this));
    tabs.setViewPager(viewpager);
}

private void initModel() {
    for(int i = 0;i<2;i++)

    models.add(new Model("Page"+(i+i),0));
}
}

模型类

public class Model {
private String title;
private int id;
public Model(){

}
public Model (String title, int id){
    this.title = title;
    this.id = id;
}
public String getTitle(){
    return title;
}
public void setTitle(String title){
    this.title = title;
}
public int getId(){
    return id;
}
public void setId(int id){
    this.id = id;
}
}

适配器类

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;
}
}
oknrviil

oknrviil1#

好吧,我做了这个,不知道这是不是正确的方法,但它确实有效。

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

}

相关问题