简体   繁体   English

ListView的Click事件在Xamarin android中不起作用

[英]Click event of listview is not working in xamarin android

Longclickable of listview is working like this: Listview的Longclickable的工作方式如下:

Listview.LongClickable = true/false;

But Clickable is not working: 但是Clickable无法正常工作:

Listview.Clickable = true/false;

No syntax error. 没有语法错误。 I have tried another way around like this: 我已经尝试过另一种方法:

Listview.IsEnabled = false;

But in this way, I have also lost LongItemClick. 但是这样,我也失去了LongItemClick。

What I want is that "ItemClick not clickable" and "LongItemClickable clickable" at same time. 我要的是同时“ ItemClick不可点击”和“ LongItemClickable可点击”。

Please help. 请帮忙。 I am working on xamarin with visual studio 2015. 我正在使用Visual Studio 2015开发xamarin。

You can achieve this funtion in the way of putting a listview in an Activity, not using ListActivity. 您可以通过将Listview放入Activity中而不使用ListActivity的方式来实现此功能。 And then set the android:listSelector property of the listview to "@android:color/transparent". 然后将listview的android:listSelector属性设置为“ @android:color / transparent”。 So the demo code is like this: 所以演示代码是这样的:

MainActivity.cs MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using System;
using Android.Views;

namespace App46
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : Activity
    {
        string[] items;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.activity_main);
            items = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
            ArrayAdapter adapter = new ArrayAdapter<String>(this, Resource.Layout.list_item, items);
            ListView lv = FindViewById<ListView>(Resource.Id.listView1);
            lv.Adapter = adapter;

            lv.ItemLongClick += delegate (object sender, AdapterView.ItemLongClickEventArgs args)
            {
                Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).Show();
            };
        }


    }

}

activity_main.axml activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1"
        android:listSelector="@android:color/transparent"/>

</LinearLayout>

list_item.xml list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp">
</TextView>

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

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