I am using RecyclerView to display data using GridLayoutManager , but I could only generate it with fixed columns. How do I dynamically display something like this image
Use StaggeredGridLayoutManager
Try like this
yourRecylerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
For details see this article.
Related
This isn't a grid layout, I've used custom views to create this. Can anyone help me to create this with gridview . Does this even possible with GridLayout?
This might or might not be possible via GridLayoutManager, but there is no direct solution to get this using GridLayoutManager. I suggest you take a look at Flexbox layout manager here:
https://github.com/google/flexbox-layout
This is from Google and what you want can easily be done using it instead. You might need to play around with the properties a bit, but I think it can be done using the justifyContent property.
A rough code snippet would be something along the lines of
RecyclerView recyclerView = (RecyclerView) context.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND);
recyclerView.setLayoutManager(layoutManager);
I am trying to implement cardView in android, such that two cards lay adjacent to one another as shown in the image below:
enter image description here
However, I am following this tutorial. The problem with this tutorial is that in this, each row contains a single cardView element; but I need two cardViews adjacent to one another. What change should I make in the code such that two cardViews are adjacent to one another instead of one.
I am adding the cardViews dynamically. That is there is no fixed size for the number of cardViews.
In the Activity Main onCreate change
mLayoutManaget = new LinearLayoutManager(this)
to
mLayoutManaget = new GridLayoutManager(this, 2)
where 2 is the Number of Columns
What you're seeing is a RecyclerView (a list) of CardViews... presented in the default RecyclerView layout manager: LinearLayoutManager.
What you should do instead, is try using the GridLayoutManager and provide 2 columns.
You can check your XML to see if you have it defined there (if so, just remove it) and do it in code with something like:
yourRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
You get the idea (I hope).
I want to add images in gridview. can I create recyclerview in gridview form? If I can, then how to do that?
You would use a GridLayoutManager
Here is an example
The Items should look like this:
RecyclerView items as grid
Assuming an arbitrary number of columns and rows, you could try using this library with a FlexboxLayoutManager for your RecyclerView. As you noted, you could also use a GridLayoutManager in case your columns are fixed.
It's simply can be done using GridLayoutManager insted of LinerLayoutManager and without nested RecyclerView or any additional Layout like this:
RecyclerView.LayoutManager mRecyclerGrid=new GridLayoutManager(this,3,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(mRecyclerGrid);
You can set how many items should appear on a row (replace the 3).
I'm trying to create a recycler view with this type of layout. The items are strings and can apear with diferent sizes, i dont know how much items there will be in each row. Can I do this with a StaggeredGridLayoutManager?
The image is just a fake example, there can be more items in each row
You might wanna check out the open-source FlexboxLayout, developed by Google, especially FlexboxLayoutManager which is a layout manager for a RecyclerView.
To create the layout manager with the right config, just do this:
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
layoutManager.setFlexWrap(FlexWrap.WRAP);
For more information, check out the Android Developers Blog's post.