Resize ListView Element While Viewing in Android? - android

What I am trying to do is populate a listView with views/elements that could be a textview/imageview. Potentially, these textviews are expandable. I want the listview to resize and wrap the content if the textview is expanded.
There are several solutions out there already:
1. Make the elements of the ListView element invisible so that the view in ListView is already the size you want. In this case, create the text view with the original long string and then collapse it.
2. Resize the element in the adapter then call notifyDataSetChanged();
However I'm looking for a solution where the view in ListView automatically resizes, even when its visible. I have tried using Solution #2 and while it works for changing values inside the view such as a string, it doesn't resize the view altogether unless the view is scrolled off screen then comes back into view.
The relevant part of my code is as follows:
holder.text.setTrim(!holder.text.getTrim());
holder.text.setText();
holder.text.requestFocusFromTouch();
notifyDataSetChanged();
holder.text.invalidate();
vi.invalidate();
vi.requestLayout();
I put this inside an OnClickListener inside an adapter's getView function.
Anyone have any solutions?
PS. Has anyone tried doing a method with multiple adapters? This one doesnt seem to work too well for me because I have potentially hundreds of elements.
Edit:
So I've come up with part of a stupid solution that is absolutely ridiculous. The behavior makes no sense. Below (primaryTextView is the same text as above eg. holder.primaryTextView == holder.text)
holder.primaryTextView.setTrim(!holder.primaryTextView.getTrim());
if(!holder.primaryTextView.getTrim()){
holder.primaryTextView.setVisibility(View.GONE);
}
else{
holder.primaryTextView.setVisibility(View.VISIBLE);
}
notifyDataSetChanged();
vi.invalidate();
This allows you to expand the view while viewing it but does not allow you to shrink the view once the text is collapsed. Furthermore, View.GONE and View.VISIBLE are NOT interchangable. Finally, the textView does not even dissapear in the event that View.GONE is triggered. Perhaps this might be a clue?

it doesn't resize the view altogether unless the view is scrolled off screen then comes back into view.
It is working, but you are not refreshing the listview, when you scroll off & return, listview recreates the updated view.
You should notifyDataSetChanged(); adapter.notifyDataSetChanged();

Nevermind found the problem:
In my layout, I was using height="0dp" instead of "wrap_content"

Related

Android Switch Widget: Loses custom thumb image when scrolling

I'm using a ListView with a custom Adapter and custom item layout where every item has a description with a switch.
Depending on what the item is, I give the switch different thumb selector-drawables per code in getView().
swtch.setThumbDrawable(getContext().getResources()
.getDrawable(R.drawable.green_switch)); // or red_switch
This works fine, but:
When I scroll the ListView, the custom images are gone, until I touch a switch again, then the custom image of it comes back immediately.
When the state list contains an element of the “disabled” state at first, the switch will have this image instead of the image it should.
It's either a bug or an improperly use of the ListAdapter by me, I think.
A workaround for me would be to include switches with different styles in the layout and show one of them depending on the item type I have and hide the others.
Is this the only possibility?
Sound like you have problems with view recycling. You have to set all the fields in the getView each time.
Please read How ListView's recycling mechanism works. Understanding view recycling is very important when using ListViews.

View for a list of items that don't scroll, and handle on item click events by the item position

Just like a ListView but instead it doesn't scroll. Its content is added programatically via an ArrayAdapter. Is there any view that can be used for this purpose?
PS: Not LinearLayout, since it doesn't get its content from an adapter (I want the observer pattern)
Edit: Let me explain a little bit more. Suppose you need a list of items, but the list itself is not scrollable, what is scrollable is the screen.
That being said, the list of items should show ALL items, not a limited amount based on a fixed height. The way to go is LinearLayout as it is both non-scrollable and shows all items within itself.
But there is a third requierement. You don't want to add(View) directly, but instead, you want something similar to an ArrayAdapter so that you have a control of the items and their position, so you can handle on item click events based on their position. As far as I know, this can't be done using a LinearLayout. So my question is, does any view exist for this purpose?
You could try using a ListView, but disable scrolling, as described here
Put your layout inside a ScrollView and yes you have to use a Linearlayout. You need to set tag for each of your view that you add dynamically and whenever your view is clicked you can get it's position by view.getTag() and perform the required operation.
Note : Adding views at run time may affect performance.

How to add Default Background if ListView / Adapter is empty

Here is an example of what I am saying in title. Take the app "Feedly". If there is even ONE item i your list, it looks like this:
Now, if the list is empty, there is a nice way to handle this to avoid a big, blank, white space, like so:
How can one achieve this?
Off hand I am thinking maybe this way, see my pseudo-code:
if (adapter.size < 1) {
// change ListView Background;
}
Check out the ListView#setEmptyView method.
If your have an empty ListView, the height should be 0 and it may be no use to set the background for ListView.
I have several solutions here:
Set the background for the LinearLayout/RelativeLayout, whatever which contain the ListView according to size of adapter.
Create another ImageView which is the "All read" image, and set its visibility according to data size of the ListView adapter.
Create a view for what you want to display when adapter size = 0, and use ViewSwitcher to switch between ListView and "All read" view according to adapter size. I will recommend this one since it may be more flexible for the "All read" view. You can have button, TextView, ImageView or whatever layout element you want to put in it.
2 and 3 are similar but using ViewSwitcher may make the code readability better than setting Visibility for views.
Here is an example for using ViewSwitcher.

button at bottom of list view is now showing up [duplicate]

I have a ListView declared inside a layout. When I addFooterView(v) it does NOT display. What are the possible reasons for this? Is there some setting in ListView or my Adapter to make this display immediately? It does eventually display but only after reinitializing the view, adapter and all.
Update: It seems that resetting the adapter finally makes it appear .... Obviously not something that I should have to do everytime I call addFooterView(v).
The view containing the ListView is inside a View maintained by PageViewer so not sure if that effects this or not.
Add the footer view before you set the adapter is the only thing we can replicate with when we are not able to see your code.

ListView recycle views that are visible on screen

I have a problem with ListView which recycles views that are visible on the screen.
I can have up to 4 items in my ListView, they are all visible on screen.
After I update a property of an object in the ArrayList that the ListAdapter uses i call the notifyDataSetChanged() method of the list adapter.
This causes the ListView to recycle the views and to redraw it self.
The problem is that it's doing the recycling in a reverse order. so if i have a button on the first Listview item it will be in the second list view item after the notifyDataSetChanged().
I have getView that changes the convertView properties except for the button onTouchListener.
This is very problematic if i have a button that works with touch event (Like PTT button). its visible for sometime and then it becomes invisible :-(.
1. why does the ListView recycle items that are visible on the screen ? is this normal behaviour? why does it do in reverse order ?
2. what can I do to solve my issue ?
You should provide code for getview() method. And adding button to an item could cause problems when you are not using checks on adding button.
Rather than adding button you should include button in all items and just make visible invisible the button at specific position where you want.
Also make check of "null" for creating convertview in getview() method. This way it would not recycle/create views/item if they are not null.

Categories

Resources