Android Switch Widget: Loses custom thumb image when scrolling - android

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.

Related

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.

ListView selected item highlight

I'm working on a GridView with setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL). By default, rows are not getting highlighted (with the blue highlight you see on WhatsApp or Gallery apps), so I am wondering, does the Android API already takes care of highlighting the selected items and I am missing something (if so, what am I missing?) or do I have to take care of highlighting selected rows by myself (if so, how to highlight, not just change the background)?
Here's my code:
gridView.setOnItemClickListener(this);
gridView.setEmptyView(view.findViewById(R.id.empty));
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
// multiChoiceModeListener is a subclass of AbsListView.MultiChoiceModeListener
// with no particular code on its abstracts methods.
gridView.setMultiChoiceModeListener(multiChoiceModeListener);
Use this for your ListItem background (XML layout)
android:background="?android:attr/activatedBackgroundIndicator"
It's basically only a selector, you can also build yourself: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/drawable/activated_background.xml
EDIT:
Given your comment, this solves it:
android:foreground="?android:attr/activatedBackgroundIndicator" with a FrameLayout
Also related question: How to set foreground attribute to other non FrameLayout view

Resize ListView Element While Viewing in 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"

Refreshing views in Android HorizontalListView

I am creating a HorizontalListView using the DevSmart library (GitHub link). When a user clicks on an element, I am catching the event and want to refresh all the views so I can show the user's selection. I've tried a bunch of different things and just can't get the HorizontalListView to refresh its views:
// does not work
mHorizontalListView.invalidate();
mHorizontalListView.requestLayout();
// does not work
mHorizontalListView.notify();
// does not work
mHorizontalListView.notifyAll();
// works, but scrolls to position 0 which isn't desirable
mHorizontalListView.setAdapter(mHorizontalListViewAdapter);
Is there any analog to invalidateViews on regular ListViews that I'm missing here. I know my selection update code is working because if I scroll the selected element on and off, when the element shows up again and has to be rebuilt, it shows up properly.
Maybe you need notifyDataSetChanged() method

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.

Categories

Resources