Android Tabview更改标签文本

时间:2018-03-20 12:27:14

标签: java android android-viewpager

嗨,大家好今天我在一个应用程序中弄乱了机器人tabview我目前正在努力,我希望有2个部分,一个名为New Releases,另一个名为A-Z。所以我看了一个教程视频,因为我以前从未尝试过使用这个视图。在教程中,他们使用CharSequence getPageTitle(int position)来获取页面的标题。但是我怎么能改变它来代替New Releeases和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"
    />

ActivityMain类

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

}

1 个答案:

答案 0 :(得分:1)

好吧我已经完成了这个不知道这是否是正确的方法,但确实有效。

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

}