I would like to create a grid layout in which each item is taking as much space as possible (minus padding), but only as long as there are columns available (after that the next item would be inserted in the next row while keeping the size). Additionally, each item must be a square and is added dynamically.
Example layout with 10 items would be as follows:
I have tried to achieve this by setting weights, ratio constraints, overriding onMeasure - but I just can't get it to work. I would be happy with either a programmatic or an XML-based solution (as long as each item can be added programmatically). I would prefer the solution to be in Kotlin, but I would be happy with a Java-based one as well.
It's probably worth saying that each item in the grid layout is a layout (RelativeLayout as of now) to make inflating it and setting a layered background drawable programmatically easy.
I think you might be able achieve what you want with a different Layout
Have a look at https://github.com/google/flexbox-layout it has lots of methods to control how the cells grown or shrink and includes automatic or manual wrapping of cells.
Take a look at RecyclerView. You would need to pass GridLayoutManager. This tutorial may or may not help you. For square items, I suggest using CardView but it's not necessary. If you are targeting tablets as well as smartphones, check this out. And for dynamically adding new items, you should notify recyclerView's adapter. See this link. You can also extend RecyclerView or GridLayoutManager for more control over items.
Related
I searched for a few hours over the internet but I didn't find any example or documentation explaining how to create an horizontal list view with fixed number of elements.
Basically, I would like to have, let's say 3 elements out of n(total number of elements) which are displayed on the screen without taking into consideration the size of the scree. The elements can be bigger or smaller proportionate to the screen but the number of visible elements should be the same, fixed. see the image.
How can I do that? Any hint is appreciated! Thank you!
You can use TwoWayWiew third party library (i dont really recommend this solution), or if you want to avoid to use lib for this, just use RecyclerView, and you can set HORIZONTAL param to layout manager.
I would not use a ListView for this, but a RecyclerView instead. Performance is better in a RecyclerView and I honestly find them easier to work with. You can allow for horizonatal scrolling via the LayoutManager for your RecyclerView.
If don't mind scrolling horizontally by 3 items you could use a ViewPager with each View containing a LinearLayout (orientation horizontal) with three of your elements that have a
android:layout_width="0dp" and an
andriod:layout_weight="1"
for even distribution.
When you "scroll" you would just animate the next "page" into the screen bringing in the next 3 elements.
This might not be the most elegant solution but I think it would behave the way you want.
use the linear layout with its orientation set as horizontal under the relative layout
Everyone knows that GridView does not supports headers and footers like a ListView.
There is a few ways to implementing this:
Use a ListView and form columnt manually via ViewGroups. It not works for me, because it's require a lot of layout operations like measuring and layouting, and it's difficult to implement draw selector on top.
Use special adapter. It works fine with a footer: we should fill last cells with a Space and manually insert after them out footer with width that equals GridView width. But this not works with headers: although header is stretched, next cells float on it.
Use a GridLayout. GridLayout is good, but what about performance with 500-1000 cells? AdapterView supports caching and reusing Views, as far as I know, this is not possible with GridLayout.
Extend GridView and write custom class, that allows to draw a header before the grid content. It's difficult, but it's should work very fast. Let's try to figure out how to do this:
Measure the header. It's very simple, I have not questions about this.
Layout header in the top of the grid. We also should consider with scrolling position to allow move header with whole grid content, so my first question is: how to know where bottom border should be located while scrolling?
Layout whole grid content after the header. How to do that? I've newer do this before.
Dispatch draw to the header view too and resolve overscrolling effect if it's not work well.
Handle the scroll event and refresh header position.
So what you can suggest me? How to do header offset? Is it right to invoke relayouting with every scroll event?
I searched an answer on a same situation with a GridView (but for a FooterView).
I've read attentively your suggestions and some from other websites. I had the same reflexion. I found a simple way as your tip: "Use special adapter. It works fine with a footer..." and this answer by #RaulSoto helped me a lot. But when I tried to update my gridview, I had a NPE, because my footer was not like the layout of my items and I had a custom filter which recalculated the getCount() method, but without understand that another view was added.
Finally, I found only solution which works: a custom class.
Create your own class as you said: "Extend GridView and write custom class" but don't extend GridView. You should extend with ListView and measure the entire width, the column width and the number of columns. I think, it's less difficult that to extend GridView, calculate the height of the header view and move it as you move your gridview or refresh the header each time you handle a scroll event..
I searched to do it in this way and I took this little project on GitHub: HFGridView by Sergey Burish. It was exactly what I need, no more.
I only added a custom attrs file in my app and customize a bit his project to have the expected result (especially, it was to have one column in portrait, two in landscape mode, refering to the numColumns attribute in my layout).
And when I try, just for test, to add a HeaderView and refresh the content with adding new items, the header view stays at the top of my gridview list, without refreshing himself.
So, I think you should search to create your class as GridView extends ListView. Refer you to the HFGridView by SBurish, it is very simple to understand how it does.
Hope this helps you with your purpose.
re: example http://www.youtube.com/watch?v=Ej--SFh963M
Similar to the above app, I am trying to create a scrolling list of images, like a Listview but with images only. (I also need to set it to fill a set height and width, say 85% of the height, 10% of the width of the screen). Is a one column Griview best? (I have had troubles in the past selecting the image height of a gridview programatically).
Any online tutorials precisely for this you have used?
If your view is going to have only one element per row, then the best and correct way of implementation is to use a ListView. The GridView should or can be used when there are going to be multiple elements per row. I believe the purpose of ListView satisfies its role for your need.
For creating a GridView with uneven rows similar to how Pinterest looks. Includes own OnItemClickListener and OnItemLongClickListener, selector, and fixed position restore. Check out this link, there is a library named "StaggeredGridView" : https://github.com/maurycyw/StaggeredGridViewDemo
As there is no text and only one column of buttons, it seems like I overlooked the obvious answer of using a scrollview of buttons, which is what I will go with. Thanks for your assistance.
I am going to start one app where my activity page will contain "n" grouped views. Grouped view means "collections of views (i.e. One group can have TextView+Button+ImageView)". So the page will have "n" number of such grouped views.
I need suggestions like what would be the best practice to implement this. I could think of below ones:
1) Should a ScrollView be used (Then I will have to create groups in runtime and place one under another)?
2) Or a ListView be used (Then how can I accommodate the height of each row as grouped views height may differ from each other?)
Or is there any other way I can go along with?
Appreciate the suggestions and any sample examples if have. Advance Thanks.
Both options would work, it really depends on your use case.
Place a vertical LinearLayout inside of a ScrollView and add your grouped-views to the LinearLayout. I would recommend this if you have a relatively small number of such views (not necessarily a fixed number, but small enough that you wouldn't have to scroll many "pages" to see them all). Make sure the ScrollView has android:layout_height="match_parent" and the LinearLayout has android:layout_height="wrap_content".
If the number of grouped-views is not small, you could use a ListView and make an Adapter for it. This lets you take advantage of ListView's automatic view recycling when items get scrolled off screen.
For either case, make an XML file for just the grouped-views. In code, you would get a LayoutInflater object (usually by calling Activity.getLayoutInflater()) and call inflate(R.layout.your_grouped_views, null). If using the LinearLayout, you would add it in code with one of the LinearLayout.addView(..) methods; if using the ListView, your adapter would return the whole thing from getView(...).
create one xml layout containing the constant elements of your group view.
in you main xml layout which will be the contentView of your application, put a ScrollView and a single LinearLayout.
then in the program inflate as many views of your group view as you want.
For your answer i want to give you referance of this website, on this website you can learn create dynamic view in android...
I would like to build an heterogeneous grid widget in Android like the one is used in apps like Google+ for tablets or Pinterest. I cannot find any tutorial and it seems particularly difficult.
Unfortunately GridView can have only cells of same size while I want to achieve different sizes cells. Can you point me to any good tutorial? I only know that I have to extend AbsListView.
Depending on how variable the items' heights are, you have a few options:
If you're looking to do a GridView with many variations of heights (e.g. Pinterest) you should play around with StaggeredGridView - as far as I can tell, it's the best open source solution out there right now.
In the image you posted, it looks as if there are parent rows with variable height children - this is a slightly easier problem to solve, and can be mimicked by using a regular ol' ListView and a subclass of BaseAdapter. Writing out the code for this is too involved for a SO answer, but i'll give you an overview of how I might achieve this effect.
The BaseAdapter must determine the rough layout of each row ahead of time such that it can report to the listview how many rows it will require - you'll need to override getCount().
In getView() of your adapter, you will inflate a single LinearLayout (or perhaps use convertView if it's been recycled) and add as many children (again, more LinearLayouts) as you will require for the width of the device (you should figure this out at runtime, unless you want to just create a phone and tablet version).
Each child LinearLayout added to the row should be set to weight=1 such that they stretch to the entire width of the parent row. In the screenshot you posted, there are 3 columns (the third id cut off).
Each child LinearLayout you just added to the row represents a column. You should set their orientations to vertical, being as they are columns!
Within each column, if you were to add a single child, you'd have a gridview.
If you wish to achieve the effect above, you can add more items to each column.
For example, row 1, column 1 has one large child. Columns 2 and 3 in row 1 both have 2 half-height children.