Android ListView custom row overlay - android

I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of ​​how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.

Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.

I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.

Related

Access Adapter items of Recyclerview

I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.

Android - ListView items maintaining style when scrolling

I know that similar questions have already been asked but I do not understand what would be the correct approach of solving the issue, yet.
I would like to change the background color of a ListView row when the user clicks it. However due to Android reusing the row layouts when scrolling, the background color gets repeated for other rows. I am wondering what would be the correct approach of maintaining the original layout for all rows except the one changed programmaticaly and also maintain the changed layout information for that row for scrolling back. I am using a SimpleAdapter which is passed the rows layout's XML.
Regards
Your rows' capabilities within your ListView largely depend on the kind of Adapter you are using. In any Adapter where you manually construct or inflate the View per item, you can change the layout properties per item, as long as you do so within the Adapter. Simply add your background color code to when the item's View is built, and it will work like a charm. If you are not able to do so with the current Adapter, consider extending the current one or using a different adapter.
Note: I haven't placed code directly within this answer because where you add it depends upon your own implementation. For instance, I would add .setBackgroundDrawable() to bindView() in an extended CursorAdapter.
Hope this helps,
FuzzicalLogic

Rows and Columns layout in Android Widget

Things are simple, I want to make a widget with 4 lines and 4 rows in it, on each cell there would be a click-able image and an action set by the user via the Settings page.
The layout is like this:
What layout element is recommended to be used for this scenario ? Should I use a GridView, TableLayout, more Linearlayouts ? Keep in mind that the spacing between items must be the same. I want to make it as light as possible. So, what layout ?
If I decide to use a GridView do you have any simple tutorial about
this? I can't manage to find a way of accessing the GridView from
AppWidgetProvider and set it's Adapter. Thank you.
LE: It seems that GridView is supported starting from Android 3.0.. please correct me if I'm wrong. In this case the only remaining thing to do is add 16 images and for each image add a onClickListener ? Brrr...
If you use a GridView then half of your work is done for you - The only layout and formatting elements you need to consider are on a Global (GridView) and Item level.
Using a GridView will also give your Scrolling functionality and the ability to change your row/columns count based on your device (4x4 on tablet, perhaps 2x8 on a phone).
Creating an extension of BaseAdapter to attach the Grid's children will also give you the flexibility to check items, multi select and will allow you to quickly modify the implementation in future by adding and removing items at will.
If this is simply a 4x4 grid which will always ALWAYS remain the same independent of device and each "Item" will always be the same, Use a RelativeLayout as it will be the most lightweight and efficient ViewGroup.

How to work with Multiple listviews and ViewFlipper (android)

In my application I have an activity which contains a viewflipper with 3 listviews inside. I have implemented the segmentedbutton widget to change between the listviews inside the viewflipper. Doing this way, I keep three listviews loaded in memory and display them according to the button selected.
Does android allow the developer to change the list adapter in runtime? I was thiking about removing the viewflipper and keep only one listview and when the user select one button, i only change the adapter from the listview.
Is this feseable? Wouldnt it be consuming more memory and cpu instead of having the three listviews inside the viewflipper?
Any answer that can help me is very appreciated.
Many thanks
T
You can certainly change the list adapter on a list at runtime, and this sounds like a much better alternative if what you are trying to accomplish is truly just changing the contents of the list.
To add to Micah's answer, you can also have 2 ListViews in your ViewFlipper in order to create a sliding effect when moving between the two lists.

Should I use multiple ListViews?

I have a RelativeLayout with different elements. I was planning to have two ListViews on it, but I have noticed there are some problems with scrolling. Since each ListView only shows a maximum of 5 rows should I try to make some kind of custom adapter to merge those ListViews? Or is it better to replace the ListView with a LinearLayout/RelativeLayout and add the rows as I get them manually? (like the first answer in here: android listview display all available items without scroll with static header ).
Which should be the proper way on doing this? or is there another way? Also, each row will have an OnClickListener.
There's two solutions if you'd like to keep your list... list-y, without having to prerender all the row Views like the above solution suggests (which can be slow to render, eats RAM and doesn't scale nicely to more than a screen or two of Views, but is a fine quick solution for smaller lists, though I'd just use a bunch of Views in a LinearLayout in a ScrollView rather than a ListView in that case).
Write a custom ListAdapter, overriding getItemViewType, getViewTypeCount and GetView to inflate the proper kind of view and recycle appropriately for your two types of views. You'll also either need to override getItem to contain custom logic for figuring out which set of source data to look in and to map the data accordingly, or mush the data down into one list of Objects (if you're using an arrayadapter) and cast in the getView method (probably a bit slower than handling it in the getItem without casting).
Just use cwac-merge, a view-and-adapter wrapping adapter. You can put two ListAdapters into a MergeAdapter and set that as your single ListView's adapter.
I had problems with scrolling. I never figured out how to have the ListView share vertical space with a different View, and have a single scrollbar for them both.
I worked around it by having everything that needs to scroll on the layout a row in the ListView.
Adding views as rows to a LinearLayout may have problems scaling up, but I think you'll be OK if you only have 10 rows in total. On 1st gen Android devices it'll probably start to get sluggish around 20 items (depends on Layout complexity obviously). ListView scales up by only inflating views as they come on screen.
So in answer to your question either of the two alternatives you suggest will be OK, but the LinearLayout option will be the easiest to code.

Categories

Resources