How can i know swipe viewPager in android - android

In my application I have viewPager and into this viewPager I show Video and I set this video into ViewPagerAdaper.
I want when swipe right o left, stop video!
I write below codes in adapter for show video :
#Override
public Object instantiateItem(ViewGroup container, final int position) {
View view = this.inflater.inflate(R.layout.row_coming_soon, container, false);
ButterKnife.bind(this, view);
final ImageView comingSoonCover = (ImageView) view.findViewById(R.id.row_comingSoonCoverImg);
final ImageView comingSoonPlayBtn = (ImageView) view.findViewById(R.id.row_comingSoonPlayBtn);
final VideoView comingSoonVideoPlayer = (VideoView) view.findViewById(R.id.row_comingSoon_videoPlayer);
final RelativeLayout comingSoonImagesLay = (RelativeLayout) view.findViewById(R.id.row_comingSoonImagesLay);
comingSoonPlayBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
comingSoonVideoPlayer.setVideoURI(Uri.parse(model.get(position).getTrailerDirectLink()));
comingSoonVideoPlayer.start();
comingSoonImagesLay.setVisibility(View.GONE);
}
});
I want when swipe viewPager, if comingSoonVideoPlayer.isPlaying(); , stop it and swipe viewPager.
How can I it?

I think this listener can halp you
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Now it's deprecated. Must using instead addOnPageChangeListener / removeOnPageChangeListener

You can try to create stop and star method in your adapter , for example
private void start(int position) {
if(comingSoonVideoPlayer != null {
comingSoonVideoPlayer.setVideoURI(Uri.parse(model.get(position).getTrailerDirectLink()));
comingSoonVideoPlayer.start();
}
}
private void stop() {
if(comingSoonVideoPlayer != null) {
comingSoonVideoPlayer.stop();
}
}
then in your fragment
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
mViewPager.getAdapter().stop();
mViewPager.getAdapter().start(position);
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
EDIT:
Make comingSoonVideoPlayer as a class field :
final VideoView comingSoonVideoPlayer;
#Override
public Object instantiateItem(ViewGroup container, final int position) {
View view = this.inflater.inflate(R.layout.row_coming_soon, container, false);
ButterKnife.bind(this, view);
final ImageView comingSoonCover = (ImageView) view.findViewById(R.id.row_comingSoonCoverImg);
final ImageView comingSoonPlayBtn = (ImageView) view.findViewById(R.id.row_comingSoonPlayBtn);
comingSoonVideoPlayer = (VideoView) view.findViewById(R.id.row_comingSoon_videoPlayer);
final RelativeLayout comingSoonImagesLay = (RelativeLayout) view.findViewById(R.id.row_comingSoonImagesLay);
comingSoonPlayBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
comingSoonVideoPlayer.setVideoURI(Uri.parse(model.get(position).getTrailerDirectLink()));
comingSoonVideoPlayer.start();
comingSoonImagesLay.setVisibility(View.GONE);
}
});
}
Hope this helps.
Sorry for my english.

Related

Refresh RecyclerView in TabLyout

I have Activity with LastTransactionsFragment, this fragment contains TabLayout with 3 tabs (AllTransactionsFragment , IncomesTransactionsFragment and OutgoesTransactionsFragment). All the tabs shows RecyclerView with list of transactions. User can delete row in each fragments. I need to implement if user delete row in 2nd fragment it also delete it immediately in 1st fragment. For now it delete row in main fragment and in database, but not in others fragments. It changes when i move to other and go back. All fragments look the same:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_transactions_list, container, false);
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
ArrayList<TransactionModel> lastTransactionsList = databaseHelper.getAllTransactionList();
Collections.reverse(lastTransactionsList);
RecyclerView recyclerView = view.findViewById(R.id.rv_allTransactionsList);
adapter = new LastTransactionsAdapter(getContext(), lastTransactionsList, false);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
EDIT
here is short video of the issue: https://streamable.com/398zke
EDIT 2
On Fragment with TabLayout
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position== 0){
IncomesTransactionsListFragment fragment = new IncomesTransactionsListFragment();
fragment.refreshRecyclerView();
} else if (position == 1){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
allTransactionsListFragment.refreshRecyclerView();
} else if (position == 2){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
allTransactionsListFragment.refreshRecyclerView();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
and the method in each fragments:
public void refreshRecyclerView(){
adapter.notifyDataSetChanged();
}
EDIT 3
main fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_last_transactions, container, false);
final ViewPager viewPager = view.findViewById(R.id.vp_allTransactions);
TabLayout tabLayout = view.findViewById(R.id.tl_allTransactions);
adapter = new TransactionsViewPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, tabLayout.getTabCount());
viewPager.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 0){
IncomesTransactionsListFragment fragment = new IncomesTransactionsListFragment();
System.out.println(0);
fragment.refreshRecyclerView();
} else if (position == 1){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
System.out.println(1);
allTransactionsListFragment.refreshRecyclerView();
} else if (position == 2){
AllTransactionsListFragment allTransactionsListFragment = new AllTransactionsListFragment();
System.out.println(2);
allTransactionsListFragment.refreshRecyclerView();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
}
tab fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_transactions_list, container, false);
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
ArrayList<TransactionModel> lastTransactionsList = databaseHelper.getAllTransactionList();
Collections.reverse(lastTransactionsList);
RecyclerView recyclerView = view.findViewById(R.id.rv_allTransactionsList);
adapter = new LastTransactionsAdapter(getContext(), lastTransactionsList, false);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
public void refreshRecyclerView(){
adapter.notifyDataSetChanged();
}
}
You need to add the addOnPageChangeListener() to update any fragments data from activity. for ex.
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetpxl) {
}
#Override
public void onPageSelected(int position) {
/*get your fragments instance and write one method in that fragment to refresh the list. call this method from here*/
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Use this link for reference: https://www.legendblogs.com/blog/how-to-use-tablayout-in-viewpager-with-recyclerview/121810

ViewPager and 3 instantiates at once, video plays on its left image

I have a ViewPager in my Activity. I set it's adapter with PagerAdapter.
Items of Adapter are VideoViews and ImageViews. When I click on a thumbnail in another activity, ViewPager appears.
But, when page of an image is selected, video is playing in background because of default number of instantiations of ViewPager items (3)_left and right items of the active item.
Any ideas how to handle video playback while ViewPager is focused on image.
Here is how my Image is shown, the right item is a videoview and its MediaController.
Adapter class:
public class FullScreenViewAdapter extends PagerAdapter {
ActivityFullscreenSlideView _activity;
LayoutInflater inflater;
ArrayList<HashMap<String, Integer>> _media;
VideoView videosrc;
ImageView imageView;
MediaController mediaController;
View viewLayout;
public FullScreenViewAdapter(ActivityFullscreenSlideView activityFullscreenSlideView,
ArrayList<HashMap<String, Integer>> media) {
this._activity = activityFullscreenSlideView;
Collections.reverse(media);
this._media = media;
}
#Override
public int getCount() {
return _media.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) _activity
.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
if (_media.get(position).get("type").equals(ActivityAppPage.IMAGE_TYPE)) {
viewLayout = inflater.inflate(R.layout.fullscreenview_image, container, false);
imageView = (ImageView) viewLayout.findViewById(R.id.fullscreenimage);
imageView.setImageResource(_media.get(position).get("src"));
} else if (_media.get(position).get("type").equals(ActivityAppPage.VIDEO_TYPE)) {
viewLayout = inflater.inflate(R.layout.fullscreenview_video, container, false);
videosrc = (VideoView) viewLayout.findViewById(R.id.video);
Uri videoAddress = Uri.parse("android.resource://" + _activity.getPackageName() + "/"
+ _media.get(position).get("src"));
videosrc.setVideoURI(videoAddress);
DisplayMetrics metrics = new DisplayMetrics();
_activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
videosrc.setLayoutParams(new LinearLayout.LayoutParams(metrics.widthPixels, LinearLayout.LayoutParams.WRAP_CONTENT));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
mediaController = new MediaController(_activity);
mediaController.setAnchorView(videosrc);
videosrc.setMediaController(mediaController);
videosrc.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaController.hide();
}
});
videosrc.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.i("LOG", "\nonprepared");
mediaController.show(0);
videosrc.start();
}
});
}
} else {
videosrc.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.i("LOG", "\nonprepared OLD API");
videosrc.start();
}
});
}
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
My Activity that has ViewPager:
public class ActivityFullscreenSlideView extends AppCompatActivity
implements View.OnClickListener {
private FullScreenViewAdapter adapter;
private ViewPager viewPager;
int dotsCount;
ImageView[] dots;
LinearLayout pager_indicator;
ArrayList<Integer> viewPagersArray;
ArrayList<HashMap<String, Integer>> media;
ActivityFullscreenSlideView thisActivity = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_viewpager);
viewPager = (ViewPager) findViewById(R.id.pager);
Intent i = getIntent();
viewPagersArray = (ArrayList<Integer>) i.getExtras().get("viewPagersArray");
int position = i.getIntExtra("position", 0);
media = (ArrayList<HashMap<String, Integer>>) i.getExtras().get("mAdapter.media");
adapter = new FullScreenViewAdapter(ActivityFullscreenSlideView.this, media);
viewPager.setAdapter(adapter);
int RtlPosition = media.size() - position - 1;
viewPager.setCurrentItem(RtlPosition);
pager_indicator = (LinearLayout) findViewById(R.id.viewPagerCountDots);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(final int position) {
if (adapter.mediaController != null)
if (media.get(position).get("type").equals(ActivityAppPage.IMAGE_TYPE)) {
Log.i("LOG", "Image Selected");
adapter.mediaController.hide();
adapter.videosrc.stopPlayback();
} else if (media.get(position).get("type").equals(ActivityAppPage.VIDEO_TYPE)) {
Log.i("LOG", "Video Selected");
adapter.mediaController.show(0);
}
for (int i = 0; i < dotsCount; i++) {
dots[i].setImageDrawable(getResources().getDrawable(R.drawable.nonselecteditem_dot));
}
dots[position].setImageDrawable(getResources().getDrawable(R.drawable.selecteditem_dot));
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
setUiPageViewController(RtlPosition);
}
I found what I had to do. I just used "setOnFocusChangeListener" in my Adapter.
videosrc.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
videosrc.start();
else
videosrc.stopPlayback();
}
});
And also I removed "OnPreparedListener".
Remove all other coding related to play video when the page is active. Only make these changes in adaptive, it's working for me.
videosrc.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
((VideoView)v.findViewById(R.id.videoView).start();
else
((VideoView)v.findViewById(R.id.videoView).pause();
});

Android InvalidStateException on ViewPager when setting page limit too small

I have a problem with my ViewPager Image slider gallery. I am setting my page limit to 3 but then my app craches on the third image because the child has already a parent. I set the limit to this number because I want to avoid large memory leaks, event with 3 images it uses up like 150Mb of allocated memory which is incredibly huge ammount. Maybe if I resize the image before uploading I can get them to work with more pages ? Anyways, here is my code for the viewPager:
ViewPager mViewPager = (ViewPager) findViewById(R.id.imagepager);
ViewerAdapter adapter = new ViewerAdapter(this);
//adapter.notifyDataSetChanged();
mViewPager.setAdapter(adapter);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setCurrentItem(number);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
time.setText(caluculateTimeAgo(MainActivity.time[position]));
if (MainActivity.distances[position] < 1000) {
dist.setText(String.valueOf(MainActivity.distances[position]) + "m");
} else {
long f = (long) (MainActivity.distances[position] / 1000);
dist.setText(String.valueOf(f) + "km");
}
author.setText(MainActivity.users[position]);
text.setText(MainActivity.text1[position]+MainActivity.tags[position]);
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
and here is my adapter definition:
public class ViewerAdapter extends PagerAdapter {
Context mContext;
public ViewerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
LayoutInflater mLayoutInflater;
#Override
public int getCount() {
return MainActivity.imgLink.length;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (View) arg1;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Picasso.with(container.getContext()).cancelRequest((ImageView) object);
}
#Override
public CharSequence getPageTitle(int position) {
return (position + "/" + getCount());
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
final View itemView = mLayoutInflater.inflate(R.layout.pager_item,container,false);
final ImageView photo = (ImageView) itemView.findViewById(R.id.photoSlide);
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fullscreen = ((BitmapDrawable)photo.getDrawable()).getBitmap();
Intent intent = new Intent(ShowMarkerContent.this, FullScreenImageActivity.class);
startActivity(intent);
}
});
Picasso.with(container.getContext()).load("MyWebsite" + MainActivity.imgLink[position]).into(photo);
container.addView(photo);
return photo;
}
}
Well, I managed to make a work-around for this problem. I forgot to set the cropping of the photo and therefore the photos were too large for this. And also, I set the offScreenPageLimit to 30 to avoid this error (This is that work-around because with large quantity of image it will come to out of memory error) So now, here is my adapter code:
#Override
public Object instantiateItem(ViewGroup container, int position) {
final View itemView = mLayoutInflater.inflate(R.layout.pager_item,container,false);
final ImageView photo = (ImageView) itemView.findViewById(R.id.photoSlide);
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fullscreen = ((BitmapDrawable)photo.getDrawable()).getBitmap();
Intent intent = new Intent(ShowMarkerContent.this, FullScreenImageActivity.class);
startActivity(intent);
}
});
Picasso.with(container.getContext()).load("MyWebsite" + MainActivity.imgLink[position]).fit().centerCrop().into(photo);
container.addView(itemView);
return itemView;
}

Swipe image views left and right like a drag and drop

I have an image view. I want to touch the image and drag it to left or right. When the image is dragged to the left, the new image should appear from the array. When i drag it to right it should be saved in another activity. How can i possibly do this, if somebody could guide me to a tutorial or example. Thank-you
You can use ViewPager and PageAdapter.
protected class ImagePagerAdapter extends PagerAdapter {
public ImagePagerAdapter() {
}
#Override
public int getCount() {
return getImages().size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_page_view, null);
String url = getImages().get(position);
if (url != null) {
ImageLoader.getInstance().displayImage(url, (ImageView) view.findViewById(R.id.image_page_view));
}
container.addView(view, 0);
return view;
}
}
...
imagePager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
}
#Override
public void onPageSelected(int i) {
//logic to open activity or other action..;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
...
imagePager.setAdapter(new ImagePagerAdapter());

ViewPager and Toast

I have a ViewPager that loads images from a remote server. My problem is, I don't know how to give it a function once a specific position is selected (not clicked). Let's say, I swipe the view pager until it reaches the specific image like image number 5, I want to show some Toast saying "image 5".
pager.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
public class ImagePagerActivity extends BaseActivity {
private ViewPager pager;
private DisplayImageOptions options;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_pager);
Bundle bundle = getIntent().getExtras();
String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls));
pager.setCurrentItem(pagerPosition);
}
protected void onStop() {
imageLoader.stop();
super.onStop();
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
public void finishUpdate(View container) {
}
public int getCount() {
return images.length;
}
public Object instantiateItem(View view, int position) {
final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {
public void onLoadingStarted() {
spinner.setVisibility(View.VISIBLE);
}
public void onLoadingFailed(FailReason failReason) {
String message = null;
switch (failReason) {
case IO_ERROR:
message = "Input/Output error";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
imageView.setImageResource(android.R.drawable.ic_delete);
}
public void onLoadingComplete(Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
imageView.setAnimation(anim);
anim.start();
}
public void onLoadingCancelled() {
// Do nothing
}
});
((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
public void restoreState(Parcelable state, ClassLoader loader) {
}
public Parcelable saveState() {
return null;
}
public void startUpdate(View container) {
}
}
}
You can implement onPageChangeListener, Than you can set this pageChageListener to your ViewPager using (setOnPageChangeListener()) ...
public class ImagePagerActivity extends BaseActivity implements onPageChangeListener {
    private ViewPager pager;
// other variables here
    public void onCreate(Bundle savedInstanceState) {
// create view Pager
// other variables
pager.setOnPageChangeListener(this);
}
// methods for Page Change Listener
#Override
public void onPageSelected(int pos) {
if (pos==5) {
//show Toast here
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// do Nothing
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// do Noting
}
}// class def ends here
edit..
Use this short hand version
Pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
if (pos==5) {
//show Toast here
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Overridee3public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
You should use the OnPageChangeListener.
There is as onPageSelected(int position) method that you can use to get the current position.
Something like
pager.setOnPageChangeListener(this);
Then make your ImagePagerActivity implement the OnPageChangeListener interface :
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
Toast.makeText(getApplicationContext(), "image " + position, Toast.LENGTH_SHORT).show();
}
public void onPageScrollStateChanged(int state) {
}

Categories

Resources