Home Fill recyclerview with centered items
Reply: 1

Fill recyclerview with centered items

Madhan Published in 2017-09-14 05:55:46Z

In my app, I have a horizontal recyclerview with LinearLayoutManager. The recyclerview is surrounded by Relative layout whose width is match_parent. I fetch data from an API and populate the recyclerview. If the API response returns a single data item, I need to show that item in the center of recyclerview. If the API response returns two items, I need to divide the space of the recyclerview equally for the two items. If the API response returns more than two items, the items should have equal gap between each other. Please refer the below screenshot:

I tried with item decoration and custom layout managers, but no use. Suggest a better solution.

Mohit Ajwani
Mohit Ajwani Reply to 2017-09-14 06:10:44Z

You can make the gravity as center and the width as match_parent. This will make the items stick to the center, whereas if multiple items are present, it will make the items spread from center. To keep the clickable area equal, you can use LinearLayoutManager as the Layout Manager for the recyclerView.

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view); myList.setLayoutManager(layoutManager);

For the items used in recyclerView, you can keep width as 0dp and make the layout_weight as 1. So here, all items get equal weights and help you achieve your objective. Do let me know if this works.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316424 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO