我有一个viewpager。有一个按钮布局。当用户点击它时,它应该从当前图像中提供数据。但是它给出了下一张图片的标题。
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
imageViewPreview.setOnTouchListener(new ImageMatrixTouchHandler(view.getContext()));
Photos image = images.get(position);
img_title = image.getTitle();
Glide.with(getActivity())
.load(image.getUrl())
.thumbnail(Glide.with(getActivity()).load(R.drawable.loader))
.fitCenter()
.crossFade()
.into(imageViewPreview);
Button save_img = (Button) view.findViewById(R.id.save_img);
save_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), img_title, Toast.LENGTH_SHORT).show();
}
});
share_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
shareImage(view);
}
});
container.addView(view);
return view;
}
如何从当前图片中获取标题?
答案 0 :(得分:2)
因为ViewPager初始化三个对象:当前位置的对象,前一个位置的对象和下一个位置的对象。而且我看到你每次更新img_title并且对象正在初始化
img_title = image.getTitle();
你要做的就是把它变成一个局部变量:
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
imageViewPreview.setOnTouchListener(new ImageMatrixTouchHandler(view.getContext()));
Photos image = images.get(position);
final String img_title = image.getTitle();
Glide.with(getActivity())
.load(image.getUrl())
.thumbnail(Glide.with(getActivity()).load(R.drawable.loader))
.fitCenter()
.crossFade()
.into(imageViewPreview);
Button save_img = (Button) view.findViewById(R.id.save_img);
save_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), img_title, Toast.LENGTH_SHORT).show();
}
});
share_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
shareImage(view);
}
});
container.addView(view);
return view;
}