The instantiateItem
on the PagerAdapter
of the viewpager
sends the wrong item position to the activity called with the intent. When I click on image 1, it sends the info of image 2, and when i click on image 2, it sends info of image 3, and so on. The receiver activity receives the wrong image and title for the clicked on sender activity. I'm using a floatingActionButton
with an onclickListener
to detect the click.
@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());
slideTitle = slideText.getText().toString();
thumbnail = mList.get(position).getImage();
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 this method to get current position of a fragment. use this position to get information about the current click.
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Log.i(TAG, "setPrimaryItem: " + position);
super.setPrimaryItem(container, position, object);
}
The code from Vishrut Mavani works pefectly The final code gets this way
@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();
}
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.