Change pagetitle in Android Viewpager

I have a ViewPager in which I use the getPageTitle method to get the title of the current page.

Here is the adapter code:

  @Override public Fragment getItem(int i) { details = productData.get(i); Fragment fragment = new ProductViewFragment(); Bundle args = new Bundle(); args.putInt(ProductViewFragment.ARG_SECTION_NUMBER, i + 1); fragment.setArguments(args); return fragment; } @Override public int getCount() { return productData.size(); } public CharSequence getPageTitle(int position) { return (position+1)+" of "+myData.size(); } 

Now I would like to update the page names of the previous and subsequent fragments. I want to name them as "previous" and "next". And it should also be dynamically updated in the following pages.

I can get the current page header number. For example, when I view the 5th fragment, the current fragment will display the title correctly. And in both corners of the ViewPager, it shows the previous page title number on the left and the next page title number on the right. Now I want the page titles to be “previous” on the left and “next” to the right, like the Gmail application, and how it shows the number of email messages in ViewPager.

The main thing I want to know is how can I access / change the page header data of the next / previous fragments from the current fragment, as in the Gmail application?

+7
source share
1 answer

I based this on the samples that come with ViewPagerIndicator .

Basically you listen when the page changes and is communicated to the adapter to display a different page title depending on the current position.

If you have these samples and you just replace these two files, try the Titles-> Default sample and it works fine for me ...

Changed code for TestFragmentAdapter as follows:

 package com.viewpagerindicator.sample; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.viewpagerindicator.IconPagerAdapter; class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter { protected static final String[] CONTENT = new String[] { "This", "Is", "A", "Test", }; protected static final int[] ICONS = new int[] { R.drawable.perm_group_calendar, R.drawable.perm_group_camera, R.drawable.perm_group_device_alarms, R.drawable.perm_group_location }; private int mCount = CONTENT.length; // CHANGE STARTS HERE private int current_position=0; public void set_current_position(int i) { current_position = i; } // CHANGE ENDS HERE public TestFragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return TestFragment.newInstance(CONTENT[position % CONTENT.length]); } @Override public int getCount() { return mCount; } @Override public CharSequence getPageTitle(int position) { // CHANGE STARTS HERE if (position == current_position-1) { return "Previous"; } else if (position == current_position+1) { return "Next"; } // CHANGE ENDS HERE return TestFragmentAdapter.CONTENT[position % CONTENT.length]; } @Override public int getIconResId(int index) { return ICONS[index % ICONS.length]; } public void setCount(int count) { if (count > 0 && count <= 10) { mCount = count; notifyDataSetChanged(); } } } 

the code for SampleTitlesDefault looks like this (added OnPageChangeListener)

 package com.viewpagerindicator.sample; import android.os.Bundle; import android.support.v4.view.ViewPager; import com.viewpagerindicator.TitlePageIndicator; // CHANGE ADDED implements.... HERE public class SampleTitlesDefault extends BaseSampleActivity implements ViewPager.OnPageChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_titles); mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); mIndicator = (TitlePageIndicator)findViewById(R.id.indicator); mIndicator.setViewPager(mPager); // CHANGE STARTS HERE mIndicator.setOnPageChangeListener(this); // CHANGE ENDS HERE } // CHANGE STARTS HERE @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { mPager = (ViewPager)findViewById(R.id.pager); ((TestFragmentAdapter)mPager.getAdapter()).set_current_position(i); } @Override public void onPageScrollStateChanged(int i) { } // CHANGE ENDS HERE } 
+6
source

All Articles