Horizo​​ntalScrollView中的可单击图像

时间:2018-02-05 08:05:24

标签: java android android-layout horizontalscrollview clickable-image

enter image description here

我正在使用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);
             }
        }
    }
}

3 个答案:

答案 0 :(得分:4)

您可以使用以下代码将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);
            }
        }
    }
} 

答案 1 :(得分:0)

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

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

谢谢

答案 2 :(得分:0)

见下文:

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);
}