Click event of listview is not working in xamarin android

Viewed 721

Longclickable of listview is working like this:

Listview.LongClickable = true/false;

But Clickable is not working:

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.

What I want is that "ItemClick not clickable" and "LongItemClickable clickable" at same time.

Please help. I am working on xamarin with visual studio 2015.

1 Answers

You can achieve this funtion in the way of putting a listview in an Activity, not using ListActivity. And then set the android:listSelector property of the listview to "@android:color/transparent". So the demo code is like this:

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

<?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

<?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>
Related