安卓:ListView的错误 [英] Android: ListView bug

查看:147
本文介绍了安卓:ListView的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表视图,有一个TextView和图像。当我点击TextView的一个隐藏的布局将被扩展为特定行。但是,发生了,对例如,当我点击第二排,10排也越来越扩大。这是我的code,

CustomListAdapter.java

 公开查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
    支架=无效;
    数据域rowItems =(数据域)的getItem(位置);

    LayoutInflater充气=(LayoutInflater)上下文
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    如果(convertView == NULL){
        convertView = inflater.inflate(R.layout.home_field_row,NULL);

        持有人=新ViewHolder();
        holder.dataFields = items.get(位置);
        holder.mName =(TextView中)convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage =(ImageView的)convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.deleteMainRL =(RelativeLayout的)convertView
                .findViewById(R.id.hmdeleteMainRL);
        holder.mDeleteImage.setTag(位置);
        holder.mName.setTag(位置);
        holder.deleteMainRL.setTag(位置);
        最后查看clickView = convertView;

        holder.mName.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                RelativeLayout的displayAddInfo =(RelativeLayout的)clickView.findViewById(R.id.displayRecordRL);
                动画expandAnim =扩展(displayAddInfo,
                                真正);
                displayAddInfo
                        .startAnimation(expandAnim);
                    }
                });

        convertView.setTag(保持器);
    }

    其他 {
        支架=(ViewHolder)convertView.getTag();
    }

    holder.mName.setText(rowItems.getName());

    返回convertView;
}
 

我该如何解决这个问题?任何形式的帮助或建议是多AP preciated。谢谢你。

更新

list_row.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / hmFieldMainRL
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@可绘制/ grid_shape>

    <的TextView
        机器人:ID =@ + ID / hmFieldName
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / displayRecordRL
        机器人:layout_alignParentLeft =真
        机器人:重力=左
        机器人:填充=15dp
        机器人:则shadowColor =#000000
        机器人:shadowDx =0
        机器人:shadowDy =0
        机器人:可点击=假
        机器人:shadowRadius =2
        机器人:文本=@字符串/ NO_DATA
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=#F2F2F2/>

    < RelativeLayout的
        机器人:ID =@ + ID / displayRecordRL
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ ID / hmFieldName
        机器人:layout_centerVertical =真
        机器人:layout_marginBottom =5DP
        机器人:layout_marginLeft =1DP
        机器人:layout_marginRight =1DP
        机器人:layout_marginTop =1DP
        机器人:背景=@可绘制/ display_record_bg
        机器人:能见度=水涨船高>

        <的EditText
            机器人:ID =@ + ID / displayRecordName
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentTop =真
            机器人:layout_marginLeft =10dp
            机器人:layout_marginTop =10dp
            机器人:layout_toLeftOf =@ + ID / displayRecordUpdate
            机器人:填充=10dp
            机器人:单线=真
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

        <的EditText
            机器人:ID =@ + ID / displayRecordPwd
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_below =@ ID / displayRecordName
            机器人:layout_marginLeft =10dp
            机器人:layout_marginTop =5DP
            机器人:layout_toLeftOf =@ + ID / displayRecordShow
            机器人:inputType =textPassword
            机器人:以下属性来=10dp
            机器人:单线=真
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

        < ImageView的
            机器人:ID =@ + ID / displayRecordAddInfoImg
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_marginLeft =10dp
            机器人:layout_below =@ ID / displayRecordPwd
            机器人:contentDescription =@字符串/ RIGHT_ARROW
            机器人:能见度=水涨船高
            机器人:SRC =@可绘制/信息/>

        <的EditText
            机器人:ID =@ + ID / displayRecordAddInfo
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ ID / displayRecordAddInfoImg
            机器人:layout_marginLeft =10dp
            机器人:layout_marginRight =10dp
            机器人:layout_marginTop =2DP
            机器人:提示=附加信息
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium
            机器人:能见度=水涨船高/>

        < ImageView的
            机器人:ID =@ + ID / displayRecordUpdate
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:背景=@可绘制/ display_record_img
            机器人:contentDescription =@字符串/ RIGHT_ARROW
            机器人:填充=10dp
            机器人:SRC =@可绘制/ update_rec/>

        < ImageView的
            机器人:ID =@ + ID / displayRecordShow
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:layout_below =@ ID / displayRecordUpdate
            机器人:layout_marginTop =10dp
            机器人:contentDescription =@字符串/ RIGHT_ARROW
            机器人:填充=10dp
            机器人:SRC =@可绘制/眼/>

        < ImageView的
            机器人:ID =@ + ID / displayRecordShowRed
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:layout_below =@ ID / displayRecordUpdate
            机器人:layout_marginTop =10dp
            机器人:contentDescription =@字符串/ RIGHT_ARROW
            机器人:填充=10dp
            机器人:SRC =@可绘制/红眼
            机器人:能见度=水涨船高/>
    < / RelativeLayout的>

< / RelativeLayout的>
 

解决方案
  

但发生了,对例如,当我点击第二排,10排也越来越扩大。

您是战斗的方式列表视图回收行布局。但是,这是很容易对付,首先让我们来添加一些新的类变量:

  SparseBooleanArray扩大=新SparseBooleanArray();
LayoutInflater充气; //在构造函数初始化此
 

现在,我们将使用扩展来检查是否 displayRecordRL 应该是可见的:

 公开查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
    数据域rowItems =(数据域)的getItem(位置);

    如果(convertView == NULL){
        convertView = inflater.inflate(R.layout.home_field_row,NULL);

        持有人=新ViewHolder();
        holder.dataFields = items.get(位置);
        holder.mName =(TextView中)convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage =(ImageView的)convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.deleteMainRL =(RelativeLayout的)convertView
                .findViewById(R.id.hmdeleteMainRL);

        //添加到您的持有者
        holder.mAddInfo =(RelativeLayout的)convertView
                .findViewById(R.id.displayRecordRL);

        holder.mDeleteImage.setTag(位置);
        holder.mName.setTag(位置);
        holder.deleteMainRL.setTag(位置);

        holder.mName.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //获取ViewHolder
                支架=(ViewHolder)((视图)v.getParent())getTag()。
                动画expandAnim =扩展(holder.mAddInfo,真正的);
                holder.mAddInfo.startAnimation(expandAnim);

                //记住,这种观点是扩大
                INT POS =(整数)v.getTag();
                expanded.put(POS,真正的);
            }
        });

        convertView.setTag(保持器);
    }

    其他 {
        支架=(ViewHolder)convertView.getTag();
    }

    holder.mName.setText(rowItems.getName());
    如果(expanded.get(位置,FALSE))
        holder2.mAddInfo.setVisibility(View.VISIBLE);
    其他
        holder2.mAddInfo.setVisibility(View.GONE);

    返回convertView;
}
 

注意如何跟踪每个行是否应该是可见的,并使用一个简单的if语句,以确保回收的布局正确地显示或隐藏添加信息部分。

I have a custom list view which has a Textview and an image. When I click on the textview a hidden layout will be expanded for that particular row. But what happening was, for eg., when I click on 2nd row, 10th row is also getting expanded. Here is my code,

CustomListAdapter.java

public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;
    DataFields rowItems = (DataFields) getItem(position);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);

        holder = new ViewHolder();
        holder.dataFields = items.get(position);
        holder.mName = (TextView) convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage = (ImageView) convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.deleteMainRL = (RelativeLayout) convertView
                .findViewById(R.id.hmdeleteMainRL);
        holder.mDeleteImage.setTag(position);
        holder.mName.setTag(position);
        holder.deleteMainRL.setTag(position);
        final View clickView = convertView;

        holder.mName.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RelativeLayout displayAddInfo = (RelativeLayout)clickView.findViewById(R.id.displayRecordRL);
                Animation expandAnim = expand(displayAddInfo,
                                true);
                displayAddInfo
                        .startAnimation(expandAnim);
                    }
                });

        convertView.setTag(holder);
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.mName.setText(rowItems.getName());

    return convertView;
}

How can I fix this? Any kind of help or suggestion is much appreciated. Thanks.

Update

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hmFieldMainRL"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/grid_shape" >

    <TextView
        android:id="@+id/hmFieldName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/displayRecordRL"
        android:layout_alignParentLeft="true"
        android:gravity="left"
        android:padding="15dp"
        android:shadowColor="#000000"
        android:shadowDx="0"
        android:shadowDy="0"
        android:clickable="false"
        android:shadowRadius="2"
        android:text="@string/no_data"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#F2F2F2" />

    <RelativeLayout
        android:id="@+id/displayRecordRL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/hmFieldName"
        android:layout_centerVertical="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:background="@drawable/display_record_bg"
        android:visibility="gone" >

        <EditText
            android:id="@+id/displayRecordName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_toLeftOf="@+id/displayRecordUpdate"
            android:padding="10dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/displayRecordPwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/displayRecordName"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/displayRecordShow"
            android:inputType="textPassword"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/displayRecordAddInfoImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_below="@id/displayRecordPwd"
            android:contentDescription="@string/right_arrow"
            android:visibility="gone"
            android:src="@drawable/info" />

        <EditText
            android:id="@+id/displayRecordAddInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/displayRecordAddInfoImg"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="2dp"
            android:hint="Additional Information"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/displayRecordUpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/display_record_img"
            android:contentDescription="@string/right_arrow"
            android:padding="10dp"
            android:src="@drawable/update_rec" />

        <ImageView
            android:id="@+id/displayRecordShow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/displayRecordUpdate"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/right_arrow"
            android:padding="10dp"
            android:src="@drawable/eye" />

        <ImageView
            android:id="@+id/displayRecordShowRed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/displayRecordUpdate"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/right_arrow"
            android:padding="10dp"
            android:src="@drawable/redeye"
            android:visibility="gone" />
    </RelativeLayout>

</RelativeLayout>

解决方案

But what happening was, for eg., when I click on 2nd row, 10th row is also getting expanded.

You are fighting the way ListViews recycle the row layouts. But this is easy enough to deal with, first let's add a couple new class variables:

SparseBooleanArray expanded = new SparseBooleanArray();
LayoutInflater inflater; // initialize this in your constructor

Now we'll use expanded to check whether displayRecordRL should be visible:

public View getView(final int position, View convertView, ViewGroup parent) {
    DataFields rowItems = (DataFields) getItem(position);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);

        holder = new ViewHolder();
        holder.dataFields = items.get(position);
        holder.mName = (TextView) convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage = (ImageView) convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.deleteMainRL = (RelativeLayout) convertView
                .findViewById(R.id.hmdeleteMainRL);

        // Add this to your holder
        holder.mAddInfo = (RelativeLayout) convertView
                .findViewById(R.id.displayRecordRL);

        holder.mDeleteImage.setTag(position);
        holder.mName.setTag(position);
        holder.deleteMainRL.setTag(position);

        holder.mName.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the ViewHolder
                holder = (ViewHolder) ((View) v.getParent()).getTag(); 
                Animation expandAnim = expand(holder.mAddInfo, true);
                holder.mAddInfo.startAnimation(expandAnim);

                // Remember that this View is expanded
                int pos = (Integer) v.getTag();
                expanded.put(pos, true);
            }
        });

        convertView.setTag(holder);
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.mName.setText(rowItems.getName());
    if(expanded.get(position, false)) 
        holder2.mAddInfo.setVisibility(View.VISIBLE);
    else
        holder2.mAddInfo.setVisibility(View.GONE);

    return convertView;
}

Notice how this tracks whether each row should be visible and uses a simple if statement to make sure the recycled layout correctly shows or hide the "Add Info" section.

这篇关于安卓:ListView的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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