Today I'm messing with Android's tabview in an app I'm currently working on. I want to have 2 sections one called New Releases and the other Called A-Z. So I've watched a tutorial video because I've never tried to use this view before. In the tutorial they use CharSequence getPageTitle(int position) to get the title of the page. But how can I change this to instead say New Releases and A-Z?
Here is how I've set my tabs up at the moment:
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>
ActivityMain class
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));
}
}
Models class
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;
}
}
Adapter class
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;
}
}
OK, I've done this, and I don't know if this the proper way to do it, but it does work.
public CharSequence getPageTitle(int position){
if (position==0) {
return "New Releases";
}
return "A-Z";
}