Android:ListView错误

时间:2013-02-25 19:16:14

标签: android android-listview

我有一个自定义列表视图,其中包含Textview和图像。当我单击textview时,将为该特定行展开隐藏的布局。但是发生的事情是,例如,当我点击第二行时,第10行也会被扩展。这是我的代码,

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;
}

我该如何解决这个问题?任何形式的帮助或建议都非常感谢。感谢。

更新

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>

4 个答案:

答案 0 :(得分:2)

  

但是发生的事情是,例如,当我点击第2行时,第10行也会被扩展。

您正在与ListViews回收行布局的方式作斗争。但这很容易处理,首先让我们添加几个新的类变量:

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

现在我们将使用expanded来检查displayRecordRL是否应该可见:

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;
}

注意这是如何跟踪每行是否应该可见并使用简单的if语句来确保循环布局正确显示或隐藏“添加信息”部分。

答案 1 :(得分:1)

您需要向适配器添加一个属性,一个简单的int。单击某个项目时,请更新当前所选位置。构建视图时,请检查位置是否等于所选位置。

public View getView(final int position, View convertView, ViewGroup parent) {
     if(this.selectedPosition == position){
     //add a holder
     else{
     //don't add a holder

     return convertview;
}

我所说的是您遇到了View回收问题。如果修改项目编号2,然后从视图编号2构建项目编号10的视图,则最终会得到一个不需要的项目(看起来像是单击它)。

点击列表中的项目时:

1)更新所选项目(适配器的属性)。例如,如果单击列表视图的第12项,selectedItem = 12;

2)调用方法notifyDataSetChanged()。这将刷新列表视图。

3)构建视图(适配器的getView())时,检查每个位置是否与所选项目相对应。如果是,请构建您的特殊视图(我不知道您想要做什么)。如果它与所选项目不对应,请“正常”构建视图

答案 2 :(得分:0)

尝试将R.id.displayRecordRL作为viewholder的一部分,并在OnClick方法上调用它。

holder.displayAddInfo = (RelativeLayout)clickView.findViewById(R.id.displayRecordRL);

和onclickMethod:

Animation expandAnim = expand(holder.displayAddInfo,
                                true);
                holder.displayAddInfo
                        .startAnimation(expandAnim);
                    }

答案 3 :(得分:0)

您遇到的问题可能是您在滚动时为列表视图中的其他项目重新使用的View(converView)设置动画。

观看此YouTube视频,您可能会了解如何解决问题。 Chet Haase(谷歌工程师)&#34; DevBytes&#34;系列&#34; ListView动画&#34; :

https://www.youtube.com/watch?v=8MIfSxgsHIs#