在 Android 标签视图中更改标签文本



今天,我在目前正在开发的应用程序中弄乱了Android的选项卡视图。我想有 2 个部分,一个称为新版本,另一个称为 A-Z。所以我看了一个教程视频,因为我以前从未尝试过使用这个视图。在教程中,他们使用 CharSequence getPageTitle(int position( 来获取页面的标题。但是我怎样才能将其更改为新版本和 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;
}
}
好吧,

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

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

}

最新更新