【问题标题】:get imageview from row listview (android)从行列表视图获取图像视图(android)
【发布时间】:2014-08-22 11:49:16
【问题描述】:

如何访问位于列表视图行内的单个图像视图? 我有一个包含许多行自定义 xml 的列表视图。每行包含一个 imageview 和一个 TextView。在 listview 中插入这些行后,如何将 imageview 的位图更改为特定行?

【问题讨论】:

  • 假设您使用ViewHolder 模式,您可以使用setTag()getTag() 来存储id、位置或您拥有的东西

标签: android image listview layout imageview


【解决方案1】:

假设您知道要更新哪个位置并且您在 UI 线程上,您可以使用

public View getViewForPosition(int position){
    int relativePos = position - listview.getFirstVisiblePosition();
    if( relativePos < 0 || relativePos > listview.getChildCount()){
        return null;
    }
    return listview.getChildAt(relativePos);
}

此函数返回 null 表示该位置在屏幕外。非 null 将从 getView 返回视图,然后您可以对其执行 findViewById 以找到您想要的孩子。

如果从非 UI 线程调用,由于视图映射的变化,您可能会因计算而关闭 1。有一个 hack 可以解决这个问题,我曾经想出过一次,但我建议不要这样做。

编辑:这是线程安全的技巧。

    public View getViewForPosition(int position){
        int relativePos = position - listview.getFirstVisiblePosition();
        //Hack for allowing us to get a view for a position that is currently being created
        if( (relativePos == listview.getChildCount() || relativePos == -1) && currentProcessingView != null){
            return currentProcessingView;
        }
        if( relativePos < 0 || relativePos >= listview.getChildCount()){
            return null;
        }
        return listview.getChildAt(relativePos);
    }

currentProcessingView 应该在getView 一开始就设置为当前位置的视图,在getView 结束时设置为null。如果该位置当前不在屏幕上,它将返回 null,您需要能够处理它。

【讨论】:

  • 不幸的是我需要在非UI线程中使用它,我会在1的最后一个位置崩溃。我该如何解决它?
  • 发布了线程安全黑客,但如果可能的话,我仍然建议不要使用它。
  • 对不起,我是意大利人,我不明白我应该如何处理我的帖子,谢谢 :)
【解决方案2】:

您可以对每个列表视图项进行唯一标记,然后在其上使用 findViewWithTag。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2012-10-23
    相关资源
    最近更新 更多