【问题标题】:Android - How to generate onClick code for ScrollView items?Android - 如何为 ScrollView 项目生成 onClick 代码?
【发布时间】:2020-05-27 09:34:47
【问题描述】:

我有一个垂直显示图像的可滚动布局:

<RelativeLayout 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"
    tools:context=".chatActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp">

        <LinearLayout
            android:id="@+id/imageGallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>
</RelativeLayout>

这就是我在代码中生成视图的方式:

    LinearLayout imageGallery;
    File[] fList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        imageGallery = (LinearLayout) findViewById(R.id.imageGallery);
        addImagesToTheGallery();
}

    private void addImagesToTheGallery() {

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        fList = path.listFiles();
        if(fList!=null)
        {
            int len = fList.length;
            for(int i=0; i<len; i++)
            {
                imageGallery.addView(getImageView(i));
            }
        }
    }

    private View getImageView(int image) {
        ImageView imageView = new ImageView(getApplicationContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 10, 0);
        imageView.setLayoutParams(lp);
        Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
        imageView.setImageBitmap(bitmap);
        return imageView;
    }

现在,我想为每个图像添加一个 onClick 事件。当有人点击图片时,我想显示一些特定于该图片的消息。所以我应该能够获得图像或其位置。我该怎么做?

【问题讨论】:

    标签: android android-linearlayout android-scrollview


    【解决方案1】:

    在CLickListener上设置“imageView”。

    private View getImageView(int image) {
        ImageView imageView = new ImageView(getApplicationContext());
        LinearLayout.LayoutParams lp = new 
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 10, 0);
        imageView.setLayoutParams(lp);
        Bitmap bitmap = BitmapFactory.decodeFile(fList[image].getAbsolutePath());
        imageView.setImageBitmap(bitmap);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO do something
                Toast.makeText(this, "item num" + 
                    image, Toast.LENGTH_LONG).show();
            }
        }); 
        return imageView;
    }
    

    【讨论】:

    • 感谢您的回答,但我应该让我的问题更清楚。我点击的每张图片都应该显示不同的信息。有什么方法可以获取图像或其位置?
    • 你的“(int image)”是每张图片的位置。并且“clickListener”适用于每个“imageView”。
    • 当我这样做时,我的应用程序崩溃了。
    • 您可以使用Toast,但不能使用this 作为Context,因为您在onClick() 方法中。您还可以创建 ArrayList 的视图以保留对您创建的 ImageViews 的引用