Android:在 Gridview 中如何使图像可点击

发布于 2024-09-13 06:27:15 字数 934 浏览 6 评论 0原文

我试图使图像可点击,以便当按下它们时会将用户发送到另一个页面或链接。

我怎样才能实现这个目标?目前,GridView 中的每一行都有 2 个按钮。它如何知道 GridView 中的哪个项目被单击,以便它执行特定于被单击的项目的特定操作。

感谢您的帮助!

这是我的图像适配器类中的:

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setAdjustViewBounds(true);
       // imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(4, 8, 4, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.menu_about,R.drawable.menu_episodes
  };
 }

I am trying to make the images clickable so that when they are pressed it will send the user to another page or link.

How can I achive this? Currently each row in the GridView has 2 Buttons. How will it know which item in the GridView is clicked so that it performs a certain action, specific to the item that was clicked.

Thanks for any help!

This is in my image adapter class:

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setAdjustViewBounds(true);
       // imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(4, 8, 4, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.menu_about,R.drawable.menu_episodes
  };
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只想待在家 2024-09-20 06:27:15

只需向 GridView 添加一个侦听器

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter());

    gridView.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView parent,
        View v, int position, long id)
        {
            data.aktueltBilledeNr = position;
            Toast.makeText(getBaseContext(), "Viser billede " + (position + 1), Toast.LENGTH_SHORT).show();
            finish();
            //startActivity(new Intent(Resultatsoversigt_akt.this, Teoriproeveaktivitet2.class));
       }
    });

Just add a listener to the GridView

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter());

    gridView.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView parent,
        View v, int position, long id)
        {
            data.aktueltBilledeNr = position;
            Toast.makeText(getBaseContext(), "Viser billede " + (position + 1), Toast.LENGTH_SHORT).show();
            finish();
            //startActivity(new Intent(Resultatsoversigt_akt.this, Teoriproeveaktivitet2.class));
       }
    });
冰葑 2024-09-20 06:27:15

您可能需要考虑使用 ImageButton 类来为您执行此操作。它是一个按钮,但不是有无聊的灰色渐变,而是在其中放置图像!
Andriod ImageButton 文档
从那里您可以自由地使用点击侦听器,就像使用常规按钮一样。

You may want to consider using the ImageButton class to do this for you. Its a button but instead of having a boring gray gradient you place an image there instead!
Andriod ImageButton Docs
From there you are free to use a click listener just like you would with a regular button.

骷髅 2024-09-20 06:27:15

首先找到按钮的位置(此处图片),然后使用开关盒执行您所说的操作...

GridView gridView = (GridView) findViewById(R.id.gridview);    
gridView.setAdapter(new ImageAdapter());      
gridView.setOnItemClickListener(new OnItemClickListener() {       
    public void onItemClick(AdapterView parent,View v,int position,long     id)               {   
        switch(position) {
        case 0:
            Intent intent=new Intent(this,targer.class);
            StartActivity(intent);
            break;
        case 1:
            break;     
        .
        .
        .

        default:
            break;
        }
    }
});

first of all find the position of the buttons(Here Image) and then use the switch case to perfrom what ever you say...

GridView gridView = (GridView) findViewById(R.id.gridview);    
gridView.setAdapter(new ImageAdapter());      
gridView.setOnItemClickListener(new OnItemClickListener() {       
    public void onItemClick(AdapterView parent,View v,int position,long     id)               {   
        switch(position) {
        case 0:
            Intent intent=new Intent(this,targer.class);
            StartActivity(intent);
            break;
        case 1:
            break;     
        .
        .
        .

        default:
            break;
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文