Android:ListView 错误 [英] Android: ListView bug
问题描述
我有一个自定义列表视图,其中包含一个 Textview 和一个图像.当我单击 textview 时,将为该特定行展开隐藏的布局.但是发生的事情是,例如,当我单击第 2 行时,第 10 行也被扩展了.这是我的代码,
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.
更新
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>
推荐答案
但是发生的情况是,例如,当我点击第 2 行时,第 10 行也被展开了.
But what happening was, for eg., when I click on 2nd row, 10th row is also getting expanded.
您正在与 ListViews 回收行布局的方式作斗争.但这很容易处理,首先让我们添加几个新的类变量:
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
现在我们将使用 expanded
来检查 displayRecordRL
是否应该可见:
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;
}
注意这如何跟踪每一行是否应该可见,并使用一个简单的 if 语句来确保回收的布局正确显示或隐藏添加信息"部分.
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.
这篇关于Android:ListView 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!