簡體   English   中英

Xamarin.Android Recyclerview項目單擊偵聽器不起作用

[英]Xamarin.Android Recyclerview Item Click Listener not working

Recyclerview項目單擊不起作用。 下面是我的適配器的代碼和ViewHolder.I隨后從教程模式中可用的xamarin文件上點擊這里

public class MainMenuRecyclerAdapter : RecyclerView.Adapter
{
    private readonly IList<ListMenuItem> items;



    public MainMenuRecyclerAdapter(IList<ListMenuItem> data)
    {
        items = data;
    }

    public override int ItemCount => items.Count;
    public event EventHandler<ClickEventArgs> ItemClick;
    public event EventHandler<ClickEventArgs> ItemLongClick;

    /// <summary>
    ///     Create new views (invoked by the layout manager)
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="viewType"></param>
    /// <returns></returns>
    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        //Setup your layout here
        const int id = Resource.Layout.MenuList;

        MainMenuRecyclerAdapterViewHolder vh;

        View itemView;
        using (itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false))
        {
            vh = new MainMenuRecyclerAdapterViewHolder(itemView, OnClick, OnLongClick);
        }
        return vh;
    }


    /// <summary>
    ///     Replace the contents of a view (invoked by the layout manager)
    /// </summary>
    /// <param name="viewHolder"></param>
    /// <param name="position"></param>
    public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
    {
        var item = items[position];
        // Replace the contents of the view with that element
        var holder = viewHolder as MainMenuRecyclerAdapterViewHolder;
        if (holder == null) return;
        holder.mnuHeader.Text = item.MenuHeader;

        holder.mnuDescription.Text = item.MenuDetails;

        switch (item.MenuKey)
        {
            case "CreateNew":
                holder.mnuIcon.SetImageResource(Resource.Drawable.createrecord);
                break;
            case "ViewRecords":
                holder.mnuIcon.SetImageResource(Resource.Drawable.vieweditrecord);
                break;
            case "SNR":
                holder.mnuIcon.SetImageResource(Resource.Drawable.keyinformantsubmissions);
                break;
            case "SNRLog":
                holder.mnuIcon.SetImageResource(Resource.Drawable.taskchecklist);
                break;
            case "AnalysisView":
                holder.mnuIcon.SetImageResource(Resource.Drawable.viewprogressreport);
                break;
            case "SendUpdates":
                holder.mnuIcon.SetImageResource(Resource.Drawable.syncwithserver);
                break;
            case "DeviceStatus":
                holder.mnuIcon.SetImageResource(Resource.Drawable.devicestatus);
                break;
            case "SystemSettings":
                holder.mnuIcon.SetImageResource(Resource.Drawable.settings);
                if (item.MenuDetails.Contains("Harare") && item.MenuKey == "SystemSettings")
                {
                    holder.mnuDescription.SetTextColor(Color.Green);
                    holder.mnuDescription.Text += " - TRAINING MODE";
                }
                if (!item.MenuDetails.Contains("Harare"))
                {
                    holder.mnuDescription.SetTextColor(Color.Red);
                    holder.mnuDescription.Text += " - VBCI MODE";
                }
                break;
            case "DBSettings":
                holder.mnuIcon.SetImageResource(Resource.Drawable.database);
                break;
            default:
                holder.mnuIcon.SetImageResource(Resource.Drawable.addbutton);
                break;
        }

    }

    private void OnClick(ClickEventArgs args)
    {
        ItemClick?.Invoke(this, args);
    }


    private void OnLongClick(ClickEventArgs args)
    {
        ItemLongClick?.Invoke(this, args);
    }
}

public class MainMenuRecyclerAdapterViewHolder : RecyclerView.ViewHolder
{
    public TextView mnuDescription;
    public TextView mnuHeader;
    public ImageView mnuIcon;


    public MainMenuRecyclerAdapterViewHolder(View itemView, Action<ClickEventArgs> clickListener,Action<ClickEventArgs> longClickListener) : base(itemView)
    {
        mnuHeader = itemView.FindViewById<TextView>(Resource.Id.txtMenuHeader);
        mnuDescription = itemView.FindViewById<TextView>(Resource.Id.txtMenuDetail);
        mnuIcon = itemView.FindViewById<ImageView>(Resource.Id.imgMenuIcon);

        itemView.Click += (sender, e) => clickListener(
            new ClickEventArgs { View = itemView, Position = AdapterPosition });

        itemView.LongClick +=(sender, e) => longClickListener(
                new ClickEventArgs { View = itemView, Position = AdapterPosition });
    }

}

public class ClickEventArgs : EventArgs
{
    public View View { get; set; }
    public int Position { get; set; }
}

我的活動代碼

private void InitRecyclerView() {
        try
        {
            SetContentView(Resource.Layout.MainLayout);

            LoadMenuItems();

            dbPath = CheckDB();

            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            toolbar.SetPadding(0, GetStatusBarHeight(), 0, 0);

            //Using the new recycler view
            recyclerView = FindViewById<RecyclerView>(Resource.Id.recycleView);

            recyclerView.SetLayoutManager(new LinearLayoutManager(this));

            mainMenuRecyclerAdapter = new MainMenuRecyclerAdapter(lstMnu);

            mainMenuRecyclerAdapter.ItemClick += RecyclerOnItemClick;

            recyclerView.SetAdapter(mainMenuRecyclerAdapter);
        }
        catch (Exception ex)
        {
            new AlertDialog.Builder(this)
                           .SetTitle("Homescreen - InitRecyclerView()")
                           .SetMessage("Error Occurred: " + ex.Message)
                           .SetPositiveButton("Ok", delegate { })
                           .Show();
        }}

我的XAML代碼

    <android.support.design.widget.CoordinatorLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true"><android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
      android:id="@+id/collapsingToolbar"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_scrollFlags="scroll|exitUntilCollapsed"
      android:fitsSystemWindows="true"
      app:contentScrim="@android:color/white"
      app:expandedTitleMarginStart="48dp"
      app:expandedTitleMarginEnd="64dp">
      <ImageView
        android:src="@drawable/RwimsHomeScreen"
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:scaleType="fitCenter"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax"
        android:background="@android:color/white"/>
      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_collapseMode="pin" />
    </android.support.design.widget.CollapsingToolbarLayout>
  </android.support.design.widget.AppBarLayout>

  <android.support.v7.widget.RecyclerView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/recycleView"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
  </android.support.v7.widget.RecyclerView>

  <android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_date_range_white_48dp"
    app:layout_anchor="@id/appBar"
    app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>

我在Activity OnCreateView方法上調用了InitRecyclerView()函數。 我的Item Click方法基本上是這樣的:

private void RecyclerOnItemClick(object sender, ClickEventArgs e)
        {

                var vibrator = (Vibrator)GetSystemService(VibratorService);
                vibrator.Vibrate(60);

                switch (lstMnu[e.Position].MenuKey)
                {
                    case "CreateNew":
                        var intentCW = new Intent(this, typeof(ActFormSelectionMenu));
                        intentCW.PutExtra("CreateNew", "true");
                        StartActivity(intentCW);
                        break;
                    case "ViewRecords":
                        var intentVw = new Intent(this, typeof(ActFormSelectionMenu));
                        intentVw.PutExtra("CreateNew", "false");
                        StartActivity(intentVw);
                        break;
default:
                        Log.Debug("EmptyMenu", "---");
                        break;
                }
            }

MenuKeyListMenuItem對象的一個​​屬性,它基本上包含其他屬性,例如描述,標頭等lstMnu變量在初始化recyclerview之前被初始化和加載(通過另一種方法)。 我無法弄清楚代碼出了什么問題,但是recyclerview不響應click事件。如果我將recyclerview更改為使用列表視圖,則菜單運行正常。 幫幫我。

Recyclerview項目單擊不起作用。

確實做到了,但是這發生在您在OnResume方法中定義的SetRecyclerAdaptor()中。

protected override void OnResume()
{
     base.OnResume();
     SetRecyclerAdaptor();
}

private void SetRecyclerAdaptor()
{
     mainMenuRecyclerAdapter = new MainMenuRecyclerAdapter(lstMenu);
     mainMenuRecyclerAdapter.ItemClick += (sender, e) =>
     {
          Toast.MakeText(this,"Click happened here!",ToastLength.Short).Show();
     };

     recyclerView.SetAdapter(mainMenuRecyclerAdapter);
}

注釋掉上面的代碼將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM