![](/img/trans.png)
[英]Switch between onItemClick and onItemLongClick event of Android ListView
[英]OnItemLongClick event of listView Android
我有一個帶有自定義單元格布局的列表視圖。 實際上,它顯示了表中的數據,有兩個按鈕,一個用於編輯,另一個用於刪除記錄。 這兩個按鈕是隱藏的,當長按該行時,這兩個按鈕就會顯示出來。 這是cell_layout:
<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 ;
}
});
問題來了。 長時間單擊列表視圖中的項目時,它會顯示該項目的“編輯”和“刪除”按鈕,但還會在該項目的下一個第七位置顯示這些按鈕。 例如,如果我單擊位置3上的項目,則還顯示3,10,17,....的按鈕...如何解決此問題?
對我來說,這聽起來像您正在處理ListView的視圖回收功能。 這個答案提供了很好的解釋。
基本示例:如果一個ListView總共有20個項目,但只有足夠的空間可以同時在屏幕上顯示4個項目,則ListView將僅使用4個視圖對象,但會為列表中的每個項目回收它們。 因此,如果您在視圖2上進行了某些更改,然后向下滾動,您會發現此更改也適用於視圖6。這使使用ListViews進行動態視圖變得困難。
在上面的示例中,如果適配器正在加載視圖6,則在適配器的getView
方法中, convertView
對象是2的視圖,然后將其重新用於數據元素6。我將嘗試存儲按鈕是否在您的計算機中顯示數據並在此方法中重置convertView
,然后基於基礎數據顯示/隱藏按鈕。
ListView專注於顯示基礎數據,但不必在視圖中編輯該數據。
您可以嘗試跳過getView
中嘗試使用convertView
的部分,以便始終創建新視圖,但是我發現這可能會導致其他一些意外的UI體驗。 祝好運!
發生此問題,因為listview單元正在重用。 當您顯示特定單元格的按鈕並滾動列表視圖時,正在重復使用該單元格的項目也將顯示按鈕。
為避免該問題,您可以做的是通過任何int變量在適配器中放置一個位置,並在長按適配器時更新位置。
在適配器的getView方法中,放置位置檢查項目是否具有相同位置將顯示按鈕,否則將設置可見性。
像在適配器中一樣:-
int selectedposition = -1;
public View getView(.........){
// your code
if (position == selectedposition){
button.setVisibility(VISIBLE);
}
else{
button.setVisibility(GONE);
}
return convertedView;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.