我有一个带有自定义单元格布局的列表视图。实际上,它显示了表中的数据,有两个按钮,一个用于编辑,另一个用于删除记录。这两个按钮是隐藏的,当长时间单击行时,这两个按键就会显示出来。这是单元格布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Customer Code and Name "
android:textSize="16sp"
android:textColor="#ff000000" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginRight="25dp">
<TextView
android:id="@+id/txtCusCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cus code"
android:textSize="16sp"
android:textColor="#ff000000" />
<TextView
android:id="@+id/txtCusName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cus Name"
android:textSize="16sp"
android:textColor="#ff000000"
android:layout_marginLeft="10dp" />
</LinearLayout>
<ImageView
android:id="@+id/imgbtnOrderActions"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/down"
android:layout_alignParentEnd="false"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="@drawable/test"/>
</RelativeLayout>
<TableLayout
android:id="@+id/tblLayoutOrderAction"
android:layout_width="fill_parent"
android:layout_height="0dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/lmgbtnOrderEdit"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="@drawable/edit"
android:layout_weight="1"
android:layout_column="1"
android:clickable="true"
android:background="#ff00b4df" />
<ImageView
android:id="@+id/ImgbtnOrderDelete"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="@drawable/delete"
android:layout_weight="1"
android:layout_column="2"
android:background="#ffff625a"
android:clickable="true" />
</TableRow>
</TableLayout>
</LinearLayout>
这两个按钮在表格布局中,我给它们0dp的高度来隐藏它们。
这是listView:的OnLongItemClick事件
lstviewOrders.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l)
{
final TableLayout tblLay = (TableLayout) view.findViewById(R.id.tblLayoutOrderAction);
TableLayout.LayoutParams lay = new TableLayout.LayoutParams(30, ViewGroup.LayoutParams.MATCH_PARENT);
tblLay.setLayoutParams(lay);
return false ;
}
});
问题来了。当长时间单击列表视图中的某个项目时,它会显示该项目的编辑和删除按钮,但也会显示位于下一个第7位的项目中的这些按钮。例如,如果我单击位置3上的项目,然后单击按钮3,10,17,。。。。还展示了。。。如何解决这个问题???
这听起来像是在处理ListView的视图回收功能。这个答案提供了一个很好的解释。
基本示例:如果ListView总共有20个项目,但只有足够的空间在屏幕上同时显示4个,那么ListView将只使用4个视图对象,但会为列表中的每个项目回收它们。因此,如果您更改视图2上的某些内容,然后向下滚动,您会发现此更改也适用于视图6。这就是ListViews难以使用动态视图的原因。
在上面的示例中,如果适配器正在加载视图6,则在适配器的getView
方法中,convertView
对象是来自2的视图,然后将其重新用于数据元素6。我会尝试存储按钮是否显示在数据中,并在此方法中重置convertView
,然后根据底层的数据显示/隐藏按钮。
ListViews处理的重点是显示基础数据,但不一定要编辑视图中的数据。
您可以尝试跳过getView
中尝试使用convertView
的部分,以便始终创建新视图,但我发现这可能会导致其他一些意外的UI体验。祝你好运
问题的出现是因为列表视图单元格正在重复使用。当您显示特定单元格的按钮并滚动列表视图时,重用该单元格的项目也将显示按钮。
为了避免这个问题,您可以通过任何int变量在适配器中定位,并在长按适配器上更新位置。
在适配器的getView方法中,如果项目具有相同的位置,则放置位置检查将显示按钮,否则将设置可见性GONE。
类似于适配器:-
int selectedposition = -1;
public View getView(.........){
// your code
if (position == selectedposition){
button.setVisibility(VISIBLE);
}
else{
button.setVisibility(GONE);
}
return convertedView;
}