简体   繁体   English

如何在RecyclerView行上单击以打开菜单项

[英]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.

相关问题 RecyclerView 项目单击并打开另一个 recyclerview - RecyclerView item click and open another recyclerview 如何将点击分别添加到RecyclerView的整个行和特定的行项目? - How to add click to RecyclerView whole row and a specific row item separately? 单击自定义列表视图特定的行项目时,打开上下文菜单 - open context menu when click on custom listview specific row item 长按recyclerview项目后如何在底部导航中显示菜单 - How to show a menu in bottom nav after long click on recyclerview item 如何在recyclerview不同的项目点击上打开几个不同的片段? - How to open few different fragments on recyclerview different item click? 如何在android中的recyclerView项目上的Webview中动态打开URL数据? - How to open URL data dynamically in Webview on recyclerView item click in android? 如何在RecyclerView中使用不同的数据和照片在项目单击上打开新的活动 - How to open new Activity on item click with different data and photo in RecyclerView 单击导航抽屉默认菜单上的项目时如何打开另一个菜单 - how to open another menu on click of item on the default menu of navigation drawer 如何在android中的recyclerView行中单击按钮以打开alertDialog? - How to open a alertDialog on click of the button in the row of a recyclerView in android? 带有单击项上的导航栏的recyclerview打开活动 - recyclerview open activity with navbar on click item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM