Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

expandablelistview.itemclick does not get fired

$
0
0

I have been working on this for days now.
I set android:focusableInTouchMode="false" but it didn't help.
Here is my code:

Main Activity:

`using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Android.Media;
using Android.Graphics.Drawables;
using Android.Graphics;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Provider;

namespace JMGPhotoPrinterAndroid
{
[Activity (Label = "JMGPhotoPrinter", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity, GestureDetector.IOnGestureListener,ScaleGestureDetector.IOnScaleGestureListener
{
public ExpandableListView lv = null;
public TextView FooterView;
private GestureDetector _gestureDetector;
private ScaleGestureDetector _ScaleGestureDetector;

    public bool OnScale(ScaleGestureDetector s){
        lv.ScaleX = lv.ScaleX * s.ScaleFactor;
        lv.ScaleY = lv.ScaleY * s.ScaleFactor;
        return true;
    }

    public bool OnScaleBegin(ScaleGestureDetector s){
        return false;
    }

    public void OnScaleEnd(ScaleGestureDetector s){

    }

    public bool OnDown(MotionEvent e)
    {
        return false;
    }
    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        FooterView.Text = String.Format("Fling velocity: {0} x {1}", velocityX, velocityY);
        return false;
    }
    public void OnLongPress(MotionEvent e) {}
    public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }
    public void OnShowPress(MotionEvent e) {}
    public bool OnSingleTapUp(MotionEvent e)
    {
        return false;
    }
    public override bool OnTouchEvent(MotionEvent e)
    {

        _ScaleGestureDetector.OnTouchEvent (e);
        _gestureDetector.OnTouchEvent(e);
        return base.OnTouchEvent (e);
    }
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        // Get our button from the layout resource,
        // and attach an event to it

        lv = FindViewById<ExpandableListView> (Resource.Id.lvItems);
        FooterView = new TextView (this);
        lv.AddFooterView (FooterView);
        FooterView.Text = "Fling Velocity: ";
        lv.ChoiceMode = ChoiceMode.Multiple;
        lv.Clickable = true;
        lv.Focusable = true;
        lv.FocusableInTouchMode = true;
        lv.Touch += (object sender, View.TouchEventArgs e) => {this.OnTouchEvent(e.Event); e.Handled = false;};

        _gestureDetector = new GestureDetector(this);
        _ScaleGestureDetector = new ScaleGestureDetector (this,this);
        System.Collections.Generic.List<ImgFolder> BMList = new System.Collections.Generic.List<ImgFolder>();
        System.Diagnostics.Debug.Print (Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).Path);

        string selection = null;
        string[] selectionArgs = null;
        string[] projection = new string[] {MediaStore.MediaColumns.Id, MediaStore.MediaColumns.Data,
            MediaStore.Images.Media.InterfaceConsts.BucketDisplayName, MediaStore.Images.Media.InterfaceConsts.BucketId
        }; 

        Android.Database.ICursor mediaCursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri,
            projection, selection,selectionArgs, null);
        lib.GetThumbnails (this,false,mediaCursor, BMList);

        mediaCursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.InternalContentUri,projection,selection,selectionArgs, null);
        lib.GetThumbnails (this,true,mediaCursor, BMList);

            //ArrayAdapter Adapter = new ArrayAdapter<ImgListItem>(this,Android.Resource.Layout.ActivityListItem,BMList);
        PhotoFolderAdapter Adapter = new PhotoFolderAdapter (this, BMList);
        lv.SetAdapter (Adapter);
        lv.ItemClick += lv_ItemClick;
        /* (object sender, AdapterView.ItemClickEventArgs e) => {
            View v = e.View;
            if (v.Tag != null) {
                ViewHolder holder = (ViewHolder)(v.Tag);
                ImgListItem ImgListItem = holder.item;
                lib.StartViewer (this, ImgListItem.Uri);
            }
        };
        */
        ///new EventHandler<AdapterView.ItemClickEventArgs>(lv_ItemClick);

    }
    private void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        View v = e.View;
        if (v.Tag != null) {
            ViewHolder holder = (ViewHolder)(v.Tag);
            ImgListItem ImgListItem = holder.item;
            lib.StartViewer (this, ImgListItem.Uri);
            }

    }
}

}

`
Adapter:

`using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Android.Media;
using Android.Graphics.Drawables;
using Android.Graphics;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Provider;

namespace JMGPhotoPrinterAndroid
{
public class PhotoFolderAdapter : BaseExpandableListAdapter
{
readonly IList rows;
readonly Activity context;
public PhotoFolderAdapter (Activity context,ListList)
{
this.context = context;
this.rows = List;
}

    public override bool HasStableIds
    {
        // Indexes are used for IDs:
        get { return true; }
    }

    //---------------------------------------------------------------------------------------
    // Group methods:

    public override long GetGroupId(int groupPosition)
    {
        // The index of the group is used as its ID:
        return groupPosition;
    }

    public override int GroupCount
    {
        // Return the number of produce ("vegetables", "fruits", "herbs") objects:
        get { return rows.Count; }
    }

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        // Recycle a previous view if provided:
        View view = convertView;

        // If no recycled view, inflate a new view as a simple expandable list item 1:
        if (view == null)
        {
            var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
            view = inflater.Inflate(Android.Resource.Layout.SimpleExpandableListItem1, null);
        }

        // Grab the produce object ("vegetables", "fruits", etc.) at the group position:
        ImgFolder imgFolder = rows[groupPosition];

        // Get the built-in first text view and insert the group name ("Vegetables", "Fruits", etc.):
        TextView textView = view.FindViewById<TextView>(Android.Resource.Id.Text1);
        textView.Text = imgFolder.Name;

        return view;
    }

    public override Java.Lang.Object GetGroup(int groupPosition)
    {
        return null;
    }

    //---------------------------------------------------------------------------------------
    // Child methods:

    public override long GetChildId(int groupPosition, int childPosition)
    {
        // The index of the child is used as its ID:
        return childPosition;
    }

    public override int GetChildrenCount(int groupPosition)
    {
        // Return the number of children (produce item objects) in the group (produce object):
        return rows[groupPosition].items.Count;
    }

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        // Recycle a previous view if provided:
        View view = convertView;

        // If no recycled view, inflate a new view as a simple expandable list item 2:
        if (view == null)
        {
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView,null);
        }
        ImgListItem item = rows [groupPosition].items[childPosition];
        ImageView Image = view.FindViewById<ImageView> (Resource.Id.Image); 
        Image.SetImageBitmap (item.Img);
        Image.Clickable = false;
        Image.Focusable = false;
        Image.FocusableInTouchMode = false;
        Image.LongClickable = false;
        //view.FindViewById<ImageView> (Android.Resource.Id.Icon).ScaleX = 5;
        //view.FindViewById<ImageView> (Android.Resource.Id.Icon).ScaleY = 5;
        TextView Text1 = view.FindViewById<TextView> (Resource.Id.Text1); 
        Text1.Text = item.FileName;
        Text1.Clickable = false;
        Text1.Focusable = false;
        Text1.FocusableInTouchMode = false;
        Text1.LongClickable = false;
        if (item.Img != null) {
            TextView Text2 = view.FindViewById<TextView> (Resource.Id.Text2); 
            Text2.Text = (item.size != null) ? item.size : item.Img.Width + "*" + item.Img.Height;
            Text2.Clickable = false;
            Text2.Focusable = false;
            Text2.FocusableInTouchMode = false;
            Text2.LongClickable = false;
        }
        //if (item.Img != null) view.FindViewById<TextView> (Resource.Id.Text2).Text = (item.size != null) ? item.size : item.Img.Width + "*" + item.Img.Height;
        //view.FindViewById<TextView> (Android.Resource.Id.Text1).
        view.Tag = new ViewHolder(item);
        //view.ScaleX = 2;
        //view.ScaleY = 2;
        view.Clickable = false;
        view.Focusable = false;
        view.FocusableInTouchMode = false;
        view.LongClickable = false;
        /*view.Click += (object sender, EventArgs e) => {
            System.Diagnostics.Debug.Print ("Click");
        };
        */
        //view.Touch += new EventHandler<View.TouchEvent1Args> (touch);
        return view;
    }
    private void touch(object sender, View.TouchEventArgs e){
        MainActivity a = context as MainActivity;
        //a.lv.OnTouchEvent(e.Event);
    }


    public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
    {
        return null;
    }

    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }
}
public class ViewHolder : Java.Lang.Object{
    public ImgListItem item;
    public ViewHolder(ImgListItem item){
        this.item = item;
    }
}
public class ImgFolder : Object
{
    public ImgFolder(string Name){
        this.Name = Name;
    }
    public string Name = "";
    public List<ImgListItem> items = new List<ImgListItem>();
}

public class ImgListItem : Object
{
    public ImgListItem(Bitmap img, String FileName, Android.Net.Uri Uri, String size)
    {   
        this.Img = img;
        this.FileName = FileName;
        this.Uri = Uri;
        this.size = size;
    }
    public Bitmap Img;
    public String FileName;
    public Android.Net.Uri Uri;
    public String size;

}

}

`

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/list"
    android:scrollbars="vertical"
    android:verticalScrollbarPosition="right">
    <ExpandableListView
        android:id="@+id/lvItems"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:clickable="true"
        android:longClickable="true"
        android:listSelector="@drawable/list_selector" />
<!--
        android:listSelector="@layout/list_selector"
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img" />
    -->
</LinearLayout>

CustomView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFDAFF7F"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:padding="1dp"
    android:id="@+id/ImageList">
    <ImageView
        android:id="@+id/Image"
        android:layout_height="78dp"
        android:layout_width="78dp"
        android:padding="1dp"
        android:src="@drawable/res"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:layout_alignParentLeft="true"
        android:longClickable="false" />
    <LinearLayout
        android:id="@+id/Text"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:focusable="false"
        android:layout_marginLeft="80dp">
        <TextView
            android:id="@+id/Text1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".70"
            android:textColor="#FF7F3300"
            android:textSize="20dp"
            android:textStyle="italic"
            android:text="Testing 1234"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:longClickable="false"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="20dp" />
        <TextView
            android:id="@+id/Text2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".25"
            android:textSize="14dp"
            android:layout_marginTop="23dp"
            android:textColor="#FF267F00"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:text="Testing 1234"
            android:longClickable="false"
            android:layout_gravity="center_vertical" />
    </LinearLayout>
</RelativeLayout>

Viewing all articles
Browse latest Browse all 58056

Trending Articles