Search code examples
javaandroidandroid-recyclerviewandroid-adapter

View the adapter starting from a certain position


I have a RecyclerView displaying a list of images & and when a user clicks on one of the images e.g position 2, it takes them to a ViewPager with that same position & they can horizontally swipe to view other images from there.

The issue I am having is how to get that specific position & call instantiateItem programmatically since on swiping, the user sees the same image.

My code

RecyclerView Adapter (should pass the position clicked to the ViewPager adapter)

public static int catPositionToView;
onBindViewHolder(...){
   holder.recylerviewItem.setOnClickListener(v -> {
        catPositionToView = position; //catPosition is a global static variable so that I can access it directly in the viewpager adaper

    //I then launch the next activity which has a viewpager
    context.startActivity(...)
   }
}

ViewPager Adapter

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    Glide.with(context)
            .load(myList.get(catPositionToView).getImage()) //Issue is here
            .into(myImageView);
}

I can view the image of the passed position but how do I view other positions from there sequentially?


Solution

  • Please try this way may help you

    in Recyclerview Adapter

    Intent intent = new Intent(context,ViewPagerActivity.class);
    intent.putExtra("currentPosition",your recyclerview click position);         
    context.startActivity(intent);
    

    in ViewpagerActivity

    currentItemPosition = getIntent().getIntExtra("currentPosition",0);
    List<Image> myList;// = new ArrayList<>();//your list on pager adapter activity
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new ImageAdapter(myList));
    viewPager.setCurrentItem(currentItemPosition);
    viewPager.setOffscreenPageLimit(myList.size());
    

    ImageAdapter

    class ImageAdapter extends PagerAdapter {
            List imagelist;
            public ImageAdapter(List<Image> imagelist) {
                this.imagelist = imagelist;
                //mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
    
            @Override
            public int getCount() {
                return imagelist.size();
            }
    
            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == ((Imageiew) object);//LinearLayout or your layout root view
            }
    
            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                Glide.with(context)
                        .load(imagelist.get(position).getImage()) //Issue is here//. now check here
                        .into(myImageView);
            }
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((ImageView)object);
            }
        }
    

    if you want to update manually or swipe to save last position from page change listener or on which page you have for help to get back on previous page like

    currentItemPosition = last position;
    viewPager.setCurrentItem(currentItemPosition);
    

    to update for specified position