0

I am developing an app which uses ViewPagerand 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?

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Muhammad Nadeem
  • 211
  • 2
  • 14
  • Please check : https://stackoverflow.com/questions/8538035/android-pageradapter-get-current-position – jessica May 23 '18 at 04:28

2 Answers2

0

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.

Sahil
  • 952
  • 1
  • 7
  • 14
0

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);
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Mayur Patel
  • 2,300
  • 11
  • 30