I need to show multiple image in grid like example given here.
and also need plus icon while images is more than some certain counts
There are multiple ways to implement it
Two horizontal/vertical LinearLayout
TableLayout with two rows
ConstraintLayout which its children are chained together
GridView
RecyclerView with GridLayoutManager
and more ... :D
Possibly you want to fetch the images dynamically, and there are several collection views: RecyclerView, ListView etc. RecyclerView is common since you can modify the layout(for your case, columns) with various LayoutManagers. Since you haven't provide any code, I cannot help you with that, but here is a guide that might be helpful.
For the plus icon mechanism, you can modify your RecyclerViewAdapter code, limiting the count(in getItemCount()) to a certain number and visible a transculent foreground with '+number' in the ViewHolder code for the last item.
Related
I am writing my self a ListView that will contain multiple types of items. I have done that using ArrayAdapter and a ListView now my issue is that I need to have some Items inside my Adapter that is gonna expand and that will have inner items of any type. Now the issue is how can I do this? I know I can use ExpandableListView but I don't need some items to be expanded. I saw this post about something similar, The person suggested to use ExpandableListView or a Custom Item, I would like to do the Custom Item but my concern is that the OP of the answer quoted this.
If the number of items is low consider using a linearlayout to look
like a listview and another linearlayout for the last item.
I'm not sure what they mean by "low" How many items can I have inside it? Would it cause lag?
so my question is what is the best way to do this? I need to put items
in a ListView that is of multiple types, and one of the types can
expand and have inner views that can again contain the same types and
so on.
Edit: Since 2 of you confused of what I want. I want to do the following inside my ArrayAdapter
Item
Item
Item
ExpandableItem {
Item
Item
Item
Item
}
Item
Item
I am trying to make some of the Items expand to have more items inside them and control the onClick of the inner items and such.
About the meaning of "low": I think it's about performance but devices today are better than devices were in 2013 when the linked post was written - maybe you'll never experience a sluggish UI with your approach. Of course this depends not only on your UI structure but also on the type of data you'll be showing (videos or just text?) and on other factors like does the device have to perform heavy work in the background or not.
RecyclerView was developed to be a "better ListView", so if performance can be an issue, then maybe it is a good choice. (The adapters for both ViewGroups can handle different View types but RecyclerView offers a better means of showing changes to single items and customizing change animations, another point in its favor)
How can I do this?
Have different View types for expandable and flat items
You can "manually" expand/ shrink a View (AFAIK your only choice when working with ListView) by using some type of animation (or the Transition framework) but you always have to...
Keep track of the expanded state of each expandable item (e.g. in the Adapter) and use them in getView() respectively onBindViewHolder().
Add another ListView like RecyclerView on clicking of any item.
Since RecyclerView uses ViewHolders, it means that you must have a predefined layout for each row. But what if each row needs to have a variable amount of views displayed?
For example, say I'm creating an instant messaging application where users can attach pictures to messages. They could attach anywhere from 0 to x pictures. When you create a RecyclerView to display these pictures (that are downloaded from a server), how would you make the rows include the correct number of ImageViews to display them?
There's a few ways I can think of doing this, but none really seem like good options.
Create ImageViews in onBindViewHolder, add them to a layout in the ViewHolder. (Isn't this what the ViewHolder tries to prevent? This would probably be laggy, especially with lots of pictures)
Restrict the amount of pictures the user can attach to a message to x, then add x ImageViews to the layout that are set to invisible/gone. In onBindViewHolder, set the ImageViews to display the picture and set these ImageViews visible. (What if I allow pictures & videos to be attached? Do I then need to add x ImageViews and x VideoViews as well?)
Put a GridView in each RecyclerView item and populate the GridView
inside onBindViewHolder. (I assume this would have no benefit over
option #1 because it's pretty much the same thing?)
That's all I can think of. Is there any other option that is designed for this sort of situation that I have not come across? Or what are the typical approaches taken towards this problem?
You need to create a heterogenous RecyclerView which will display multiple different ViewHolders. There are several ways to accomplish this.
The basic approach is that based on the number of photos/videos associated with each item in your RecyclerView.Adapter you would override getItemViewType(int position) (a method which is part of the RecyclerView.Adapter class) to return a different int. This int is the viewType parameter passed into createViewHolder(ViewGroup parent, int viewType). You would then use the correct ViewHolder/layout based on the viewType argument.
See here for a good tutorial.
If you don't mind using a library for an alternate approach I recommend AdapterDelegates.
I am trying to create a custom view like a GridView but with the difference of scroll bar functionality. i.e. I want that the GridView should create the column and rows dynamically and must hold a TextView or EditText view inside each cell of the grid and the custom view can be scrolled in vertically and horizontally too.
I have been tried with Table and default Grid Layout but I failed. Any idea, suggestion or comments are welcome.
Thank you.
You should use RecyclerView for this purpose. You will have to declare the number of Columns upfront. You cannot have different number of columns at runtime.
However Horizontal and Vertical Scrolling at the same time is tricky but still can be done.
Checkout: http://belladunovska.com/tech/nested-recycler-view-in-android
There is a talk on the same topic by Android Lead of NewCircle Training
Also you can checkout TwoWayView: https://github.com/lucasr/twoway-view (I think it is not using RecyclerView)
I am wondering how we can create CardView Lists (dynamically generated cardview items). I'm surprised I can't find any info on how to create CardView lists on the developer.android.com site. I can usually find whatever I'm looking for.
I have seen quite a lot of stuff online but from what I've seen, its still using generic views, not CardViews. How can we create a list with cardviews?
I am wondering how we can create CardView Lists (dynamically generated cardview items)
Use ListView or RecyclerView. Use CardView as the root layout for your row layouts, wrapping around whatever the "real" stuff is in the row. And you're done.
RecyclerView will be the more common choice, simply because it was released at the same time as CardView. Many of my RecyclerView samples use CardView, such as this one and this one.
I'm surprised I can't find any info on how to create CardView lists on the developer.android.com site
That's because there is nothing much different about creating a list containing CardView widgets than there is about creating a list containing any other sort of widget. CardView is just a subclass of FrameLayout with a nice drop shadow effect. It is not rocket science, nor is it brain surgery.
I have an app on the iPhone that is completely table view driven. On android all I see is a static table, how can I create a table that I can populate with data that I requested from the network.
You could use either ListView or GridView. ListView has a method getView which you override to customize ListView item. This very much like (UITableViewCell *)tableView... in iphone, except that you needn't count the height of each cell. If you want to have something like tableView with sections you could use ExpandableListView. Here are links to Android ListView, Android Expandable ListView and Android GridView. There can also be found very useful examples in Android Dev web-site.
You can use GridView or ListView with custom View for row. In both cases you can supply a BaseAdaper (or a subclass of it) through which you can manipulate the data.
If you want the exact grid border like example here you go Android Grid Border... Obviously you can render data dynamically from whatever the source is...