我有一个自定义列表视图,其中有一个文本视图和一个图像。当我单击文本视图时,该特定行的隐藏布局将展开。但发生的情况是,例如,当我单击第 2 行时,第 10 行也会展开。这是我的代码,
自定义列表适配器.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;
}
我怎样才能解决这个问题?非常感谢任何形式的帮助或建议。谢谢。
Update
列表行.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 行也会展开。
您正在与 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 语句来确保回收的布局正确显示或隐藏“添加信息”部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)