[英]Clickable Button in a custom listview
我的問題是我無法使添加到自定義列表視圖中的按鈕可單擊。
這是我的自定義列表視圖的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="335dp"
android:layout_height="100dp"
android:minHeight="80dp"
android:maxHeight="250dp"
android:background="#ffffff"
android:id="@+id/list"
android:baselineAligned="false"
android:longClickable="false"
android:descendantFocusability="blocksDescendants"
android:clickable="false">
<Button
android:background="@drawable/grbgCan"
android:layout_width="15dp"
android:layout_height="20dp"
android:id="@+id/deleteMemo"
android:layout_gravity="start"
android:layout_marginLeft="270dp"
android:layout_marginTop="5dp"
android:duplicateParentState="false"
android:clickable="true" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id"
android:layout_marginLeft="290dp"
android:layout_marginTop="-20dp"
android:layout_marginRight="15dp"
android:clickable="true" />
<TextView
android:id="@+id/top"
android:text="Title"
android:textColor="#ff7e7777"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="255.5dp"
android:layout_height="19.9dp"
android:paddingLeft="10dp"
android:layout_marginTop="-20dp" />
<TextView
android:id="@+id/bottom"
android:textColor="#ff7e7777"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="63.3dp"
android:paddingLeft="50dp"
android:layout_gravity="left"
android:text="Note" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/date"
android:layout_marginLeft="250dp"
android:layout_marginRight="5dp" />
</LinearLayout>
在我的主要活動中,我嘗試了Btn.Click事件,但它只會產生一個錯誤。 從我讀過的類似問題中,他們的按鈕已在其數組適配器中初始化,但是我正在使用SimpleCursorAdapter,因為我是從sqlite數據庫中獲取數據的。
那我該怎么辦? 請不要給我Java代碼,因為有些代碼不適用於xamarin Studio,因為它是c#。 謝謝。
您無法通過這種方式進行操作,因為ListView中有很多相同ID的相同項目
您必須像這樣在Adapter的getView()方法中將OnClickListener設置為按鈕
( 這是 android studio的Java代碼 ,因此它可能與c#代碼不同, 但要旨是相同的 )
Button btn = (Button) convertView.findViewById(R.id.deleteMemo);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here you go
}
});
嘗試這樣的操作(如果您沒有片段,請刪除下面的視圖。)
var _view = view.FindViewById<ListView>(Resource.Id.listView1);
if (_view.ChildCount > 0){
Button _button = (Button)_view.GetItemAtPosition(0).GetChildAt(0);
}
因為列表上有很多相似的對象,所以這是我認為訪問它們的唯一方法,請記住,項目位置是相對於屏幕上當前可見項目的,並且您不能訪問不可見的項目
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.