【问题标题】:Listen for ImageView clicks in LinearLayout在 LinearLayout 中监听 ImageView 点击
【发布时间】:2014-01-17 12:52:26
【问题描述】:

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

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

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,这可能是问题所在。

【问题讨论】:

  • 我认为你在做 v.getTag() 时得到的标签是线性布局本身而不是图像视图。尝试将 OnClickListener 添加到循环内的每个图像视图中,而不是将其设置为 linearLayout1。
  • 你只需要为你的imageview设置Uniq id。
  • 我在线性布局中有 UIImageView,我将 setOnClickListener 添加到图像视图中,效果很好。你能仔细检查一下你是否可以使用它吗?
  • 我相信 UIImageView 是 iOS 而不是 Android

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


【解决方案1】:

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

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 来获取特定数据时。试试这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多