如何找到listview中item控件并进行操作

有时候我们要动态修改listview中item某一项的属性(例如实现全选 或者单选后imageview的效果)

1 例如如下的布局 绝对布局中两个item


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/since_the_number_relativeLayout_select"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <TextView
        android:id="@+id/since_the_number_select_tv"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="15dp"
        android:gravity="center"
        android:text="1号桩"
        android:textColor="#FF333333"
        android:textSize="16dp" />

    <ImageView
        android:id="@+id/since_the_number_select_iv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:src="@drawable/list_btn_default" />
</RelativeLayout>
1 设置listview的点击事件后实现单选并改变此item项中imageview的状态
下面是实现循环遍历item 并得到里面的控件问题
listGroupSataion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.since_the_number_relativeLayout_select);
        for (int i = 0; i < listGroupSataion.getChildCount(); i++) {
            View viewChild = listGroupSataion.getChildAt(i);
            RelativeLayout layMain = (RelativeLayout) viewChild.findViewById(R.id.since_the_number_relativeLayout_select);
            ImageView iv = (ImageView) layMain.findViewById(R.id.since_the_number_select_iv);
            iv.setImageResource(R.drawable.list_btn_default);
        }
        ImageView iv = (ImageView) relativeLayout.findViewById(R.id.since_the_number_select_iv);
        iv.setImageResource(R.drawable.list_btn_selected);
        sineTheNumberListener.onSineTheNumberClick(list.get(position));
    }
});
假如布局过于复杂 ,那就必须进行一步一步的解析找到item
展开阅读全文

没有更多推荐了,返回首页