2

我有一个自定义列表视图,它有一个 Textview 和一个图像。当我单击 textview 时,将为该特定行展开隐藏的布局。但是发生的事情是,例如,当我点击第 2 行时,第 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

4 回答 4

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 语句来确保回收的布局正确显示或隐藏“添加信息”部分。

于 2013-02-25T21:17:21.330 回答
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;
}

我的意思是您遇到了视图回收问题。如果您修改项目 n°2,然后从视图 n°2 构建项目 n°10 的视图,您最终会得到一个不需要的项目(看起来就像单击它一样)。

单击列表中的一项时:

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

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

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

于 2013-02-25T19:50:43.000 回答
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);
                    }
于 2013-02-25T19:24:39.120 回答
0

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

观看此 YouTube 视频,您可能会了解如何解决您的问题。Chet Haase(谷歌工程师)“DevBytes”系列“ListView Animations”:

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

于 2013-02-25T20:11:28.967 回答