Search code examples
androidxamarintouch-eventlistadapter

Xamarin ListActivity onTouchEvent (or any other touch listener)


I'm trying to learn Xamarin and as a very basic first app I'm trying to play around with a list adapter. the lists shows just well but I want to do things with the items in it like swiping etc. I tried the following code below but it seems like the OnTouchEvent is not being called (maybe because it's not attached to the view of the list but to something "below" it? when I tried using it in MainActivity it worked well). I managed to use OnListItemClick but it's not enough for me. How can I make the OnTouchEvent work or what other possibilities do I have to listen to touch event?

thanks

public class SomeList: ListActivity
{

    string[] someArr;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);

        someArr = getArr();

        ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SwipeListView, someArr); 

    }

    public override bool OnTouchEvent(MotionEvent e) {
        doSomething(e);
    }
}

Solution

  • For your reasons you can use listview to start with.

    ListView consists of the following parts:

    Rows – The visible representation of the data in the list.

    Adapter – A non-visual class that binds the data source to the list view.

    Fast Scrolling – A handle that lets the user scroll the length of the list.

    Section Index – A user interface element that floats over the scrolling rows to indicate where in the list the current rows are located.

    To create simple list do the following:

    create xml for your listview and listview items:

    ListView:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#000000"
            android:id="@+id/listView1" />
    </LinearLayout>
    

    ListViewItem:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:id="@+id/relativeLayout"
        android:padding="10dp">
        <TextView
            android:text="Name"
            android:textColor="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtName" />
    </RelativeLayout>
    

    Listview needs an adapter to display items so we should create one:

    public class ListAdapter : BaseAdapter<string>
    {
        List<string> _list;
        Activity _context;
        public ListAdapter(Activity context, List<string> list)
        {
            _context = context;
            _list = list;
        }
    
        public override long GetItemId(int position)
        {
            return position;
        }
    
        public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
        {
            View view = convertView;
            if (view == null)
            {
                view = _context.LayoutInflater.Inflate(Resource.Layout.ListItem, null);
            }
    
            TextView txtName = view.FindViewById<TextView>(Resource.Id.txtName);
    
            var item = _list[position];
    
            txtName.Text = item.UserName;
    
            return view;
        }
    
        public override int Count
        {
            get
            {
                return _list.Count;
            }
        }
    
        public override string this[int index]
        {
            get
            {
                return _list[index];
            }
        }
    }
    

    Then in your Activity and set your adapter to listview:

    ListView _listView;
    ListAdapter _adapter;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    
        SetContentView(Resource.Layout.List);
    
        _listView = FindViewById<ListView>(Resource.Id.listView1);
        _listView.ItemClick += ItemClick;
    
        _adapter = new ListAdapter(this, myList);
        _listView.Adapter = _adapter;
    }
    
    
        void ItemClick (object sender, AdapterView.ItemClickEventArgs e)
        {
            //Do stuff on item click.
        }
    

    Thats it. For detailed tutorial check Xamarin Documentation

    More Links: Swipe To Refresh

    For more complex functionalities and flexibility check RecyclerView