Question

At the moment I have a custom PagerAdapter class in my android app that basically gives a new flash card for each swipe using cursors. At the beginning it gives a custom XML that would basically give instructions (swipe left for previous, right for next). After it has been swiped to the right it will then move onto the flash cards and cycle through them as the person swipes. What I want to do is two things:

1)At the end of the ViewPager (after the cursor has reached its last position and the user has seen the last card) I want to append a custom XML View just as I did at the beginning.

2)I would like to make the ViewPager stop at at this view and make it so further swipes to the right won't do anything.

At the moment 1) is very important and has been frustrating me to no end. 2) I can live without but would be handy to know for future references. I've written out my code below is hopefully some Android/ViewPager/PagerAdapter experts can help me out. Thanks for your help!

@Override
    public Object instantiateItem(View view, int position) {
        LinearLayout layout = null;
        int position2 = position;
        if(position == 0){
            layout = (LinearLayout) inflater.inflate(R.layout.activity_slide_info, null);
        }else{
            position2--;
            cursor.moveToPosition(position2);
            layout = (LinearLayout) inflater.inflate(R.layout.activity_card, null);

            TextView cardTitle = (TextView) layout.findViewById(R.id.pirate_card_title);
            TextView cardExample = (TextView) layout.findViewById(R.id.pirate_card_example);
            TextView cardDefinition = (TextView) layout.findViewById(R.id.pirate_card_definition);

            cardTitle.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_TITLE)));
            cardExample.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_SENTENCE)));
            cardDefinition.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_DEFINITION)));
        }
        ((ViewPager) view).addView(layout);
        return layout;
}
Was it helpful?

Solution

ViewPager can be a bit finicky at times.

The way to deal with the second issue is simpler: are you implementing getCount()? That should solve your second problem- set the length as the total number of cards you have (minus one, of course). That should make it where it won't go past the total number of cards- if you leave it blank, it will crash when you go to the last card (as it tries to load the next but finds nothing).

The first issue is a bit more complicated: how do you make it run code when it gets to the last location? The best way to do this would be to implement an onPageChangeListener wherever your ViewPager is implemented (likely onCreate()). Here's an example:

mPageChangeListener = new OnPageChangeListener() {
 @Override
 public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub
 }

 @Override
 public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
 }

 @Override
 public void onPageSelected(int pos) {
     if (pos == lastPage) {
         //display your XML or do anything else you want to do when done here
         }
  }

};
yourViewPager.setOnPageChangeListener(mPageChangeListener);

Hopefully this solves your other problem!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top