从某个位置开始查看适配器
View the adapter starting from a certain position
我有一个显示图像列表的 RecyclerView & 当用户点击其中一张图像时,例如位置 2,它会将他们带到具有相同位置的 ViewPager & 他们可以水平滑动以从那里查看其他图像.
我遇到的问题是如何获取特定位置并以编程方式调用 instantiateItem
,因为在滑动时,用户会看到相同的图像。
我的代码
RecyclerView Adapter(应该把点击的位置传给ViewPager适配器)
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 适配器
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Glide.with(context)
.load(myList.get(catPositionToView).getImage()) //Issue is here
.into(myImageView);
}
我可以查看已通过位置的图像,但如何从那里按顺序查看其他位置?
请试试这个方法可能对你有帮助
在 Recyclerview 适配器中
Intent intent = new Intent(context,ViewPagerActivity.class);
intent.putExtra("currentPosition",your recyclerview click position);
context.startActivity(intent);
在 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);
}
}
如果你想手动更新或刷卡
从页面更改侦听器或在哪个页面上保存最后位置以帮助返回上一页,如
currentItemPosition = last position;
viewPager.setCurrentItem(currentItemPosition);
更新指定位置
我有一个显示图像列表的 RecyclerView & 当用户点击其中一张图像时,例如位置 2,它会将他们带到具有相同位置的 ViewPager & 他们可以水平滑动以从那里查看其他图像.
我遇到的问题是如何获取特定位置并以编程方式调用 instantiateItem
,因为在滑动时,用户会看到相同的图像。
我的代码
RecyclerView Adapter(应该把点击的位置传给ViewPager适配器)
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 适配器
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Glide.with(context)
.load(myList.get(catPositionToView).getImage()) //Issue is here
.into(myImageView);
}
我可以查看已通过位置的图像,但如何从那里按顺序查看其他位置?
请试试这个方法可能对你有帮助
在 Recyclerview 适配器中
Intent intent = new Intent(context,ViewPagerActivity.class);
intent.putExtra("currentPosition",your recyclerview click position);
context.startActivity(intent);
在 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);
}
}
如果你想手动更新或刷卡 从页面更改侦听器或在哪个页面上保存最后位置以帮助返回上一页,如
currentItemPosition = last position;
viewPager.setCurrentItem(currentItemPosition);
更新指定位置