使用Xamarin Android中的SwipeLayout点击事件不适用于Recyclerview项目

时间:2016-06-25 18:15:10

标签: android xamarin android-recyclerview

我按照https://developer.xamarin.com/guides/android/user_interface/recyclerview/上的指南

在Recyclerview项目上设置了一个带有点击事件的viewholder的recyclerview

一切正常。

之后,我想将此组件用于滑动功能 https://components.xamarin.com/view/androidswipelayout

如果工作正常,滑动,但点击recyclerview项目不再触发事件。有什么建议吗?

itemlayout是

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <com.daimajia.swipe.SwipeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/swipe_layout"
        android:clickable="false"
        android:focusable="false">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF5534"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Elimina"
                android:layout_marginLeft="30dp"
                android:textColor="#fff"
                android:textSize="17sp" />
            <View
                android:layout_height="wrap_content"
                android:layout_width="0px"
                android:layout_weight="1" />
            <ImageView
                android:id="@+id/trash"
                android:layout_width="36dp"
                android:layout_height="40dp"
                android:layout_marginRight="50dp"
                android:src="@drawable/trash" />
        </LinearLayout>
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardElevation="4dp"
            app:cardUseCompatPadding="true"
            app:cardCornerRadius="3dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="150dp"
                    android:layout_margin="3dp"
                    android:id="@+id/imageView" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:id="@+id/title"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginLeft="5dp"
                        android:layout_marginBottom="5dp"
                        android:layout_marginRight="5dp"
                        android:lines="1"
                        android:maxLines="1"
                        android:ellipsize="end" />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:id="@+id/author"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginLeft="5dp"
                        android:layout_marginBottom="5dp" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="@color/fairNav"
                        android:id="@+id/progress"
                        android:layout_alignBottom="@+id/imageView"
                        android:layout_alignRight="@+id/imageView"
                        android:layout_gravity="bottom"
                        android:layout_margin="7dp" />
                </LinearLayout>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </com.daimajia.swipe.SwipeLayout>
</FrameLayout>

适配器:     公共类BookCardAdapter:RecyclerView.Adapter     {         //项目点击的事件处理程序:         公共事件EventHandler ItemClick;

    List<LibraryEdition> items;
    Context mContext {get; set;}

    public BookCardAdapter(List<LibraryEdition> myDataset, Context context) {
        items = myDataset;
        mContext = context;
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int viewType)
    {
        // Inflate the CardView for the photo:
        View itemView = LayoutInflater.From (parent.Context).
            Inflate (Resource.Layout.CardBookListItem, parent, false);

        BookCardViewHolder vh = new BookCardViewHolder (itemView, OnClick);

        vh.Icon.Click += (object senerObj, EventArgs eve) => {
                    AndroidUtils.ReadList.Delete (items [vh.AdapterPosition].EditionID);
                    FairbooksAPI.DeleteFromReadingList (items [vh.AdapterPosition].VersionID);
                    items.RemoveAt (vh.AdapterPosition);
                    NotifyItemRemoved (vh.AdapterPosition);
                    NotifyItemRangeChanged (vh.AdapterPosition, items.Count);
                    vh.SwipeLayout.Close (true);
                    AndroidUtils.toUpdateHomeReadings = true;
        };

        vh.SwipeLayout.SetShowMode (SwipeLayout.ShowMode.LayDown);
        vh.SwipeLayout.Opened += (sender, e) => {
            YoYo.With (Techniques.Tada)
                .Duration (700)
                .PlayOn (vh.Icon);
        };
        return vh;
    }

    // Fill in the contents of the book (invoked by the layout manager):
    public override void OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
    {
        BookCardViewHolder vh = holder as BookCardViewHolder;
        vh.Image.SetImageBitmap(null);

        string coverUrl = items[position].CoverUrl;

        AndroidUtils.SetImage (mContext, coverUrl, vh.Image);

        vh.Title.Text = items[position].Title;
        vh.Author.Text = "by " + items [position].Author.UserName;

        int value = (int)items [position].Percentage;
        vh.Progress.Text = value > 100 ? "100%" : value + "%";
    }

    // Return the number of books:
    public override int ItemCount
    {
        get { return items.Count; }
    }

    // Raise an event when the item-click takes place:
    void OnClick (int position)
    {
        if (ItemClick != null)
            ItemClick (this, position);
    }
}

观看者:

public class BookCardViewHolder : RecyclerView.ViewHolder
{
    public SwipeLayout SwipeLayout;
    public ImageView Icon;
    public ImageView Image { get; private set; }
    public TextView Title { get; private set; }
    public TextView Author { get; private set; }
    public TextView Progress { get; private set; }

    public BookCardViewHolder (View itemView, Action<int> listener) 
        : base (itemView)
    {
        SwipeLayout = itemView.FindViewById<SwipeLayout> (Resource.Id.swipe_layout);
        Icon = itemView.FindViewById<ImageView> (Resource.Id.trash);
        Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
        Title = itemView.FindViewById<TextView> (Resource.Id.title);
        Author = itemView.FindViewById<TextView> (Resource.Id.author);
        Progress = itemView.FindViewById<TextView> (Resource.Id.progress);

        itemView.Click += (sender, e) => listener (base.AdapterPosition);
    }
}

并在父片段中设置适配器,如下所示:

mAdapter = new BookCardAdapter (Books, this.Activity);

                        // Register the item click handler (below) with the adapter:
                        mAdapter.ItemClick += OnItemClick;
                        mRecyclerView.SetAdapter (mAdapter);

和:

    void OnItemClick (object sender, int position)
        {
//          ProgressDialog progress = new ProgressDialog (this.Activity);
//          progress.SetMessage ("Opening Book...");
//          progress.Show ();
            AndroidUtils.Read (Books [position], this.Activity);
        }

0 个答案:

没有答案