[英]How to open menu item on click on RecyclerView Row
I have an app with BottomNavigationView
, which has 2 menu items. 我有一个带有BottomNavigationView
的应用程序,它具有2个菜单项。 Both have their Fragments
. 两者都有其Fragments
。 On the first item in the Fragment
I have a RecyclerView
. 在Fragment
的第一项上,我有一个RecyclerView
。 I want to open the second menu item on click on RecyclerView
row and also send some parameters. 我想打开RecyclerView
行上的第二个菜单项,然后发送一些参数。 How and where should I do it? 我应该如何在哪里做?
What am I doing now: In the Fragment
, which I show on the first menu item, I have a method, that I pass to RecyclerViewAdapter
, so that it can be triggered each time a row is tapped. 我现在正在做什么:在第一个菜单项上显示的Fragment
,我有一个方法,该方法传递给RecyclerViewAdapter
,以便每次敲击一行都可以触发它。 But I cannot find any method in my listener as BottomNavActivity
to open it's second menu item 但是我无法在listener as BottomNavActivity
找到任何方法listener as BottomNavActivity
来打开它的第二个菜单项
You need a reference to your activity within the adapter. 您需要引用适配器中的活动。 You can pass this in during initialisation using 您可以在初始化期间使用
SomeAdapter(var activity: Activity)
Then inside your adapter you set the onClickListener with the following action 然后在适配器内部,通过以下操作设置onClickListener
activity?.navigation?.selectedItemId = R.id.navigation_id_here
From API 25.3.0 it was introduced the method setSelectedItemId(int id)
which let's you mark an item as selected as if it was tapped. 从API 25.3.0开始,引入了方法setSelectedItemId(int id)
,您可以将其标记为已选中,就像点击它一样。
From docs: 从文档:
Set the selected menu item ID. 设置所选的菜单项ID。 This behaves the same as tapping on an item. 这与点击项目的行为相同。
Code example: 代码示例:
BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setSelectedItemId(R.id.my_menu_item_id);
For those, who still use SupportLibrary < 25.3.0 对于那些仍然使用SupportLibrary <25.3.0的用户
View view = bottomNavigationView.findViewById(R.id.menu_action_item);
view.performClick();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.