Add a ListView or RecyclerView to new NavigationView

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I am using the new NavigationView from revision 22.2.0 of the support library by Google. It works perfectly fine to generate a navigation drawer populated using a menu res.

I was wondering is it possible to add a ListView or RecyclerView to the navigation drawer so that it can be populated using my custom adapter code, which allows for far greater flexibility than menu resources.

Here is my current XML:

Where in my XML would I add the ListView or RecyclerView?

EDIT

As per Basant's suggestion, I nested a ListView into the NavigationView. You lose the ability to inflate from a menu res (as far as I know) but it succeeds in what I want it to do. The header XML is unchanged, it is just included into XML.

New code:

回答1:

You can just nest the ListView or RecyclerView inside the NavigationView.

NOTE: Keep in mind that if you use use a ListView inside it, you can't use the NavigationView's header. You will have to use the header view of the ListView that you are adding. Don't forget to remove the app:menu and app:header fields.



回答2:

If you want to adding views inside the NavigationView you can do something like this. This way you have no restriction of adding Header on your NavigtionView with ListView.

 

That look something like this



回答3:

In response to Shubham's comment

This will not scroll the header view like the Navigation View does

I solved it by putting the LinearLayout inside a NestedScrollView. Now it scrolls properly with the header.



回答4:

***** try this way *****



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!