可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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: