LBJ33 LBJ33 - 2 months ago 16
Android Question

Changing a textview above viewpager depending on what page?

I have an activity that has 2 buttons (Previous, Next) and a text view in between the two. Below that is a viewpager.

I would like to change the textview to the title of whatever page they are on.

Here is the class where it all happens:

public class MyWorkout extends BaseActivity {

NonSwipeableViewPager vp;
TextView txtViewTitle;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("My Workout");

Intent intent = getIntent();
String workouttitle = intent.getStringExtra("workout");
switch(workouttitle) {
case "w29" : {
getSupportActionBar().setTitle("My Workout - Workout 29");
break;
}
case "w30" : {
getSupportActionBar().setTitle("My Workout - Workout 30");
break;
}
}

Button btnPrev = (Button) findViewById(R.id.btnPrevious);
Button btnNext = (Button) findViewById(R.id.btnNext);
vp = (NonSwipeableViewPager) findViewById(R.id.view_pager);
txtViewTitle = (TextView) findViewById(R.id.txtViewTitle);
this.addPages(vp);

//THIS IS WHAT I HAVE TRIED
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}

btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MoveNext();
}
});

btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MovePrevious();
}
});

}
public void MoveNext() {
//it doesn't matter if you're already in the last item
vp.setCurrentItem(vp.getCurrentItem() + 1);
}

public void MovePrevious() {
//it doesn't matter if you're already in the first item
vp.setCurrentItem(vp.getCurrentItem() - 1);
}

private void addPages(NonSwipeableViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);

Week1Mon week1MonFragment = new Week1Mon();
week1MonFragment.setArguments(args);
adapter.addPage(week1MonFragment);
Week1Wed week1WedFragment = new Week1Wed();
week1WedFragment.setArguments(args);
adapter.addPage(week1WedFragment);
Week1Fri week1Fri = new Week1Fri();
week1Fri.setArguments(args);
adapter.addPage(week1Fri);
Week2Mon week2MonFragment = new Week2Mon();
week2MonFragment.setArguments(args);
adapter.addPage(week2MonFragment);
Week2Wed week2WedFragment = new Week2Wed();
week2WedFragment.setArguments(args);
adapter.addPage(week2WedFragment);
Week2Fri week2Fri = new Week2Fri();
week2Fri.setArguments(args);
adapter.addPage(week2Fri);

pager.setAdapter(adapter);
}
}


The part I have tried:

if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}


This sets the textview to Week One, Monday. Unfortunately it doesn't change when the user hits the Next button. I'm also hoping theres a better way, as I don't want to "assume" that the current item is that day of the week, because every week is different. I also need to figure out how to set the current page when the app is opened, because it saves the position is shared pref and I have to load it in the class above, and somehow set the page to a current page (preferably by the title)

Here is MyFragPagerAdapter:

public class MyFragPagerAdapter extends FragmentPagerAdapter {

ArrayList<Fragment> pages = new ArrayList<>();

public MyFragPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return pages.get(position);
}

@Override
public int getCount() {
return pages.size();
}

public void addPage(Fragment f) {
pages.add(f);
}

@Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
}
}


Thanks for any help, let me know if you need to see more code!

Answer

You can use,

viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

public void onPageSelected(int position) {
    // Check if this is the page you want.
}});
Comments