C# Android Recyclerview项单击侦听器不工作
Recyclerview项目单击“不工作”。下面是我的适配器和ViewHolder的代码C# Android Recyclerview项单击侦听器不工作,c#,android,xamarin.android,android-recyclerview,C#,Android,Xamarin.android,Android Recyclerview,Recyclerview项目单击“不工作”。下面是我的适配器和ViewHolder的代码 MenuKey是ListMenuItem对象的一个属性,它基本上包含其他属性,如description、header e.t.c.lstMnu变量在初始化recyclerview之前被初始化和加载(通过另一种方法)。 我无法找出代码的错误,但不知何故,recyclerview没有响应click事件。如果我将recyclerview更改为使用列表视图,则菜单工作正常。帮帮我,伙计们 Recyclerview项
MenuKey
是ListMenuItem
对象的一个属性,它基本上包含其他属性,如description、header e.t.c.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);
}
注释掉上述代码将解决您的问题
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);
}
注释掉上面的代码将解决您的问题。您尝试过调试您的项目吗?您可以检查
click
方法是否被激发。不,没有。将在调试后更新。谢谢@YorkShen MSFT。已尝试在方法中添加toast,但它没有显示。我已测试了您的代码,我使用string
替换您的ListMenuItem
类,并发现它工作正常。是这样吗?这是否意味着我不能在列表中使用自定义对象?我注意到调试时事件正在触发,但没有发生任何事情。您可以使用它,但我无法重现您的问题,您能否共享一个可以重现此问题的基本演示?您是否尝试过调试您的项目?您可以检查click
方法是否被激发。不,没有。将在调试后更新。谢谢@YorkShen MSFT。已尝试在方法中添加toast,但它没有显示。我已测试了您的代码,我使用string
替换您的ListMenuItem
类,并发现它工作正常。是这样吗?这是否意味着我不能在列表中使用自定义对象?我注意到在调试时,虽然没有发生任何事情,但事件仍在触发。您可以使用它,但我无法重现您的问题,您能否共享一个可以重现此问题的基本演示?
<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>
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;
}
}
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);
}