简体   繁体   English

自定义列表视图中的可点击按钮

[英]Clickable Button in a custom listview

My problem is I can't make the button I added in my custom listview to be clickable. 我的问题是我无法使添加到自定义列表视图中的按钮可单击。

Here is the XML file of my 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>

In my main activity I tried the Btn.Click event but it only produces an error. 在我的主要活动中,我尝试了Btn.Click事件,但它只会产生一个错误。 And from what I've read in similar problems, their button was initialize in their Array Adapter but I'm using SimpleCursorAdapter because I'm fetching data from my sqlite database. 从我读过的类似问题中,他们的按钮已在其数组适配器中初始化,但是我正在使用SimpleCursorAdapter,因为我是从sqlite数据库中获取数据的。

What should I do then? 那我该怎么办? Please don't give me java codes because some them aren't applicable in xamarin studio since it's c#. 请不要给我Java代码,因为有些代码不适用于xamarin Studio,因为它是c#。 Thanks. 谢谢。

You can't do it this way because you have a lot of same items in ListView with same ids 您无法通过这种方式进行操作,因为ListView中有很多相同ID的相同项目
You must set OnClickListener to your button in your Adapter's getView() method like this 您必须像这样在Adapter的getView()方法中将OnClickListener设置为按钮
( this is java code for android studio, so it can differ from c# code, but gist is the same ) 这是 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
        }
    });

Try something like this (If you don't have Fragments delete the view. below) 尝试这样的操作(如果您没有片段,请删除下面的视图。)

var _view = view.FindViewById<ListView>(Resource.Id.listView1);
if (_view.ChildCount > 0){
Button _button = (Button)_view.GetItemAtPosition(0).GetChildAt(0);
}

Because you have a lot of similar objects on a list this is the only way i think to access them, Remember that item position is relative to the current visible items on the screen and you cant access items that are not visible 因为列表上有很多相似的对象,所以这是我认为访问它们的唯一方法,请记住,项目位置是相对于屏幕上当前可见项目的,并且您不能访问不可见的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM