ListView控件:如何以编程方式从外部访问项目的内容是什么? [英] ListView: how to access Item's elements programmatically from outside?

查看:109
本文介绍了ListView控件:如何以编程方式从外部访问项目的内容是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的情况。

我有一个ListView,ListView中的每一个项目是由不同的小部件(TextViews,ImageViews,等...)膨胀形式布局的 getView()自定义适配器的方法。

I have a ListView, each item of the ListView is comprised of different widgets (TextViews, ImageViews, etc...) inflated form a Layout in the getView() method of the custom adapter.

现在,我想实现如下:

当某个事件触发我想改变的视图是项目内的背景。

when a certain event is triggered I want to change the background of a View which is inside the item.

请我怎么办呢?

这是该项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardlayout"
android:layout_width="320dp"
android:layout_height="130dp"
android:background="@android:color/transparent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >

<FrameLayout
    android:layout_width="320dp"
    android:layout_height="117dp" >

    <View
        android:id="@+id/card"
        android:layout_width="320dp"
        android:layout_height="117dp"
        android:background="@drawable/card_selector" />

</FrameLayout>  
</LinearLayout>

我需要改变卡的背景

我曾尝试这样做:

View v=lv.getAdapter().getView(index, null, lv);
View card =(View)v.findViewById(R.id.card);
card.setBackgroundResource(R.drawable.pressed_background_card);

但没有成功: - ((

But no success :-((

推荐答案

在你的事件被触发,你应该只需要调用一个notifyDataSetChanged您的适配器,这样它会再次呼吁getView你所有可见的元素。

When your event is triggered you should just call a notifyDataSetChanged on your adapter so that it will call again getView for all your visible elements.

您getView方法应该考虑到某些因素可能有不同的背景颜色(不要忘记将其设置为正常颜色,如果元素不需要改变的背景下,与其他循环,你将有许多内容与变更的背景当您滚动)

Your getView method should take into account that some elements may have different background colors (and not forget to set it to normal color if the element doesn't need the changed background, else with recycling you would have many elements with changed background when you scroll)

编辑:

我会尝试这样的:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null)
    {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.card, parent, false);
    }

    //This part should also be optimised with a ViewHolder
    //because findViewById is a costly operation, but that's not the point of this example
    CardView cardView =(CardView)convertView .findViewById(R.id.card);

    //I suppose your card should be determined by your adapter, not a new one each time
    Card card = getItem(position);

    //here you should check sthg like the position presence in a map or a special state of your card object
    if(mapCardWithSpecialBackground.contains(position))
    {
        card.setBackgroundResource(specialBackground);
    }
    else
    {
        card.setBackgroundResource(normalBackground);
    }
    cardView.setCard(card);

    return convertView;
}

和对特殊事件我要补充该项目的位置到地图,并呼吁notifyDataSetChanged。

And on the special event i would add the position of the item into the map and call notifyDataSetChanged.

这篇关于ListView控件:如何以编程方式从外部访问项目的内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆