【问题标题】:How to change ImageView resource in nested listview items?如何更改嵌套列表视图项中的 ImageView 资源?
【发布时间】:2017-06-10 16:06:00
【问题描述】:

我有一个列表视图,其中有一个图像视图和一些文本视图。如果单击图像,如何更改图像视图资源。

【问题讨论】:

  • 什么意思?您是否尝试更改您点击的同一张图片?
  • 另外,我们不会为您提供完整的解决方案。你有没有尝试过?
  • 完全正确。当我点击任何图像时,它会变成另一个
  • 很难不向我们展示一些代码,基本上是在适配器中,您单击项目并更改图像,显示您的适配器
  • 我在 imageview 上添加了 onclick 监听器,但是当我点击一张图片时,两个或更多的图片资源也会发生变化。

标签: java android android-layout listview android-studio


【解决方案1】:

如果我理解你的逻辑,你需要:

  • 设置 onItemClickListener
  • 保存在听者内部选择的位置
  • 在适配器内设置图片的 onClickListener
  • 使用所选位置从适配器中保存的数据中恢复图像

Código setOnItemClickListener:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        lastSelectedItem = position;
        // Set the collor of the backgroud of the row in the listView, remmember to test if the flag already have been initialized;
    }
});

Código setOnClickListener:

onClick(View v){
    // use some field thought the constructor of the adapter
    Drawable draw = data.getImage(lastSelectedItem);
    ImageView img = (ImageView) v;
    img.setDrawable(draw);
    // set the lastSelectedItem and change the color of the background
}

希望对你有所帮助。

【讨论】:

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