Horizo​​ntalScrollView 中的可点击图像

Clickable Images in HorizontalScrollView

我正在使用 Horizo​​ntalScrollView 创建画廊,我想单击 Horizo​​ntalScrollView 中的图像。

这是我的代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
            if(data.getClipData() != null){
                int totalItemsSelected = data.getClipData().getItemCount();
                linearLayout = (LinearLayout) findViewById(R.id.linear);
                for(int i = 0; i < totalItemsSelected; i++){
                    Uri fileUri = data.getClipData().getItemAt(i).getUri();
                    ImageView image = new ImageView(this);
                    image.setAdjustViewBounds(true);
                    image.setImageURI(fileUri);
                    linearLayout.addView(image);
             }
        }
    }
}

循环设置onClickListener。所以每个 ImageView 都会有自己的点击监听器。在将图像添加到布局之前。

image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // do ur work here
    }
})

谢谢

您好,请使用以下代码 implement 单击事件 ImageView

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
        if(data.getClipData() != null){
            int totalItemsSelected = data.getClipData().getItemCount();
            linearLayout = (LinearLayout) findViewById(R.id.linear);
            for(int i = 0; i < totalItemsSelected; i++){
                Uri fileUri = data.getClipData().getItemAt(i).getUri();
                ImageView image = new ImageView(this);
                image.setAdjustViewBounds(true);
                image.setImageURI(fileUri);

                //click event
                image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.e(TAG,"Selected File URI : "+fileUri); 
                    }
                });

                linearLayout.addView(image);
            }
        }
    }
} 

见下文:

for(int i = 0; i < totalItemsSelected; i++){
    Uri fileUri = data.getClipData().getItemAt(i).getUri();
    ImageView image = new ImageView(this);
    image.setAdjustViewBounds(true);
    image.setImageURI(fileUri);

    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //what you want to do
        }
    });

    linearLayout.addView(image);
}