I am developing an app which uses ViewPager
and PagerAdapter
. This is working fine, ViewPager
shows right content but I need the Bitmap of current Image displaced on screen. I wrote two methods getBitmap()
and getId()
. Here is the code.
public class CursorPagerAdaptor extends PagerAdapter {
private Cursor mCursor;
private Context mContext;
private Bitmap bitmap;
int id;
LayoutInflater layoutInflater;
public CursorPagerAdaptor(Context context,Cursor cursor){
mContext = context;
mCursor = cursor;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == ((RelativeLayout) object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View itemView= layoutInflater.inflate(R.layout.view_pager_items, container, false);
final ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
final ProgressBar progressBar = (ProgressBar)itemView.findViewById(R.id.progress);
//
mCursor.moveToPosition(position);
int urlIndex = mCursor.getColumnIndex(MetaDataInfo.COLUMN_PHOTOURL);
String url = mCursor.getString(urlIndex);
int idIndex = mCursor.getColumnIndex(MetaDataInfo._ID);
id = mCursor.getInt(idIndex);
container.addView(itemView);
Glide.with(mContext)
.asBitmap()
.load(url).apply(new RequestOptions().dontAnimate().fitCenter())
.into(new SimpleTarget<Bitmap>(640,480) {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
progressBar.setVisibility(View.GONE);
bitmap = resource;
imageView.setImageBitmap(resource);
}
});
ViewGroup parent = ((ViewGroup)itemView.getParent());
if (parent!=null){ parent.removeView(itemView); }
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((RelativeLayout)object);
bitmap = null;
}
public Bitmap getBitmap(){
return bitmap;
}
public int getId(){
return id;
}
}
This is working fine but I need Bitmap
and id of the current page visible. When I call mAdapter.getBitmap();
this method returns the bitmap of next off-screen page. How can I get the Bitmap of the visible page of ViewPager
?
For what you are trying to achieve use ViewPager instead of PagerAdapter, I have gone through similar issue and used
onPageSelected(int currentPage)
method of viewpager to get the current item displayed.
ViewPager class have getCurrrentItem()
method. You can get current page index from using getCurrentItem().
Use like:
viewPager.getCurrentItem()
And you can set with like:
viewPager.setCurrentItem(setCurrentItem);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.