在LinearLayout中收听ImageView点击

时间:2014-01-17 12:52:26

标签: java android android-linearlayout android-imageview onclicklistener

我将imageViews添加到LinearLayout,由于某种原因,clickListener不适用于每个imageView,所以我决定尝试将标签添加到imageView然后将其添加到LinearLayout,然后当点击linearLayout时我想要检查views标签以找出按下哪个imageView。这不起作用,特别是view标记始终为null。我希望能够区分点击的图像。

以下是每个图像的路径的数组列表,图像正在加载到屏幕上。

private void displayImages(ArrayList<String> al) {
    final int THUMBSIZE = 640;//was originally 64

    linearLayout1.removeAllViews();
    for (int i = 0; i < al.size(); i++){
        Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(
                Environment.getExternalStorageDirectory()+"/ImageSpace/"+al.get(i)), 
                        THUMBSIZE, THUMBSIZE);
        Drawable d = new BitmapDrawable(getResources(),ThumbImage);
        imageView = new ImageView(MainActivity.this);
        imageView.setBackground(d);
        //linearLayout1.removeAllViews();
        //imageView.setTag(i+"");
        linearLayout1.addView(imageView);
        linearLayout1.getChildAt(i).setTag(i+"");
    }

}

这是linearLayout点击监听器:

    linearLayout1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "yeh", Toast.LENGTH_SHORT).show();
            Log.v("TAG","views tag: "+v.getTag());
        }
    });

XML布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
    android:id="@+id/linearLayout1"
    android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="@drawable/ic_launcher" 
            android:onClick="test"/>

    </LinearLayout>
</ScrollView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

另外,我正在初始化我使用的imageView,

imageView = (ImageView) findViewById(R.id.imageView1);

这似乎可能不正确,因为它只会引用一个imageView,这可能是问题所在。

1 个答案:

答案 0 :(得分:3)

您希望将ID添加到您添加到ImageView中的每个Linear Layout

for (int i = 0; i < al.size(); i++){
    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(
            Environment.getExternalStorageDirectory()+"/ImageSpace/"+al.get(i)), 
                    THUMBSIZE, THUMBSIZE);
    Drawable d = new BitmapDrawable(getResources(),ThumbImage);
    imageView = new ImageView(MainActivity.this);
    imageView.setId(i);
    imageView.setBackground(d);
    //linearLayout1.removeAllViews();
    //imageView.setTag(i+"");
    linearLayout1.addView(imageView);
     imageView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v1) {

                    System.out.print("Imageview clikcked Path!::::" +al.get(v1.getId()));

                    }

                }
            });
}

当您点击任意ImageView以获取该视图的ID时获取特定数据。试试这个。