gpt4 book ai didi

android - 如何为水平 ScrollView 中的图像制作点击监听器

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:31 24 4
gpt4 key购买 nike

我正在制作水平 ScrollView 以在 Android 应用程序中显示图像。但我不知道如何为每个单独的图像制作点击监听器。我的代码是

    private Integer images[] = {R.drawable.mba, R.drawable.lifestylemin, 
R.drawable.jee,R.drawable.comic,R.drawable.defence};
// private ReaderView mReader;
// ImageView imageGallery;
LinearLayout im1;
LinearLayout imageGallery;
LinearLayout imageGallery2;
ImageView imageView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view=inflater.inflate(R.layout.tab2, container, false);
// init PlugPDF
LinearLayout adcat=(LinearLayout)view.findViewById(R.id.adcat);
adcat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),ebookSelection.class));
}
});

imageGallery = (LinearLayout) view.findViewById(R.id.imageGallery);
imageGallery2 = (LinearLayout) view.findViewById(R.id.imageGallery2);
addImagesToThegallery();

//edit code
for(int i=0;i<images.length;i++){
final ImageView imageView = (ImageView) getImageView(images[i]);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(getActivity(),book.class);
int id=imageView.getId();
i.putExtra("id",id);
startActivity(i);
}
});
}
////
return view;
}
private void addImagesToThegallery() {

for (Integer image : images) {
imageGallery.addView(getImageView(image));
imageGallery2.addView(getImageView(image));
}
}


private View getImageView(Integer image) {
ImageView imageView = new
ImageView(getActivity().getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(260, 400);
lp.setMargins(0, 0, 10, 0);

imageView.setLayoutParams(lp);
imageView.setImageResource(image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}

我想为每个图像制作单独的点击监听器,这样我就可以从不同的图像中执行不同的口味。提前谢谢。

最佳答案

您还可以在 getImageView 方法上设置 OnClickListener

编辑:使用 ImageResource 在您的 imageView 上添加一个 TAG,然后在图像上恢复 TAG OnClick 方法,如下所示:

private View getImageView(Integer image, Integer index) {
ImageView imageView = new
ImageView(this.getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(260, 400);
lp.setMargins(0, 0, 10, 0);

imageView.setLayoutParams(lp);
imageView.setImageResource(image);
//Create a TAG on your Strings Resources
//"IMAGE_TAG" with any value
imageView.setTag(R.string.IMAGE_TAG, image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int image = (int)v.getTag(R.string.IMAGE_TAG);
switch (image)
{
case R.drawable.mba:
{
//action for your image
} break;
//and so on..

}
}
});

return imageView;
}

关于android - 如何为水平 ScrollView 中的图像制作点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38032009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com