instantiateItem
的{{1}}上的PagerAdapter
将错误的项目位置发送给有意图的活动。当我单击图像1时,它将发送图像2的信息,而当我单击图像2时,它将发送图像3的信息,依此类推。接收方活动收到的点击的发送方活动收到错误的图像和标题。我正在将viewpager
与floatingActionButton
一起使用来检测点击。
onclickListener
答案 0 :(得分:1)
重写此方法以获取片段的当前位置。使用该位置获取有关当前点击的信息。
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Log.i(TAG, "setPrimaryItem: " + position);
super.setPrimaryItem(container, position, object);
}
答案 1 :(得分:0)
Vishrut Mavani的代码可以正常工作 最终代码就是这种方式
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View slideLayout = inflater.inflate(R.layout.slide_item, null);
final ImageView slideImg = slideLayout.findViewById(R.id.slide_img);
final TextView slideText = slideLayout.findViewById(R.id.slide_title);
final FloatingActionButton fab = slideLayout.findViewById(R.id.floatingActionButton);
slideImg.setImageResource(mList.get(position).getImage());
slideText.setText(mList.get(position).getTitle());
container.addView(slideLayout);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, MovieDetailActivity.class);
intent.putExtra("title", slideTitle);
intent.putExtra("imgURL", thumbnail);
mContext.startActivity(intent);
}
});
return slideLayout;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object){
Log.i(TAG, "setPrimaryItem_che: " + position);
super.setPrimaryItem(container, position, object);
slideTitle = mList.get(position).getTitle();
thumbnail = mList.get(position).getImage();
}