Refreshing views in Android HorizontalListView - android

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

Related

Android - requestFocus to off screen element in recyclerView

I am trying to scroll and request focus to an off screen element from a recycler view. I have a list of 15 languages. When app starts the user can select one language from the recycler. Then if the app starts again the recycler scrolls and request focusto that item.
But imagine the user selects the last language from the list which is not showed in the recycler when the app starts. How to scroll and therefore reqeust focus to that element which is not currently showed in the recycler?
I expected to do something like recycler.scrollToPosition(14) and then scrollToPosition(14) , but the index is outof bunds... I guess thats because the element is not created yet. Any idea?
I had similar scenario and below code worked for me:
(rv_your_recycler_view.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(position, offset)
Just set offset to 0 and position to 14, this will get you last item.
P.S. If it doesn't work check if your recyclerview's bottom constraint tied up to parent's bottom.
app:layout_constraintBottom_toBottomOf="parent"
If you try to scroll and set the focus at the same time then it may fail. While the scrolling will proceed, the request for the focus will fail if the item is not yet present in the RecyclerView. (As you state.)
The solution is to separate the two activities. Scroll to the position as you do now, but set the focus when the item is bound to the view holder in the adapter. This will require you to keep track of which item has the focus so that it can be referenced in the adapter.

How to identify if a row is visible in a listview

I have a situation involving animation of listview's item appearances.
I have a few views in a ScollView , the last of which is the listview. I have attached an appearence animation of the row (a fade in animation).
The problem I have is that when the screen is loaded , the getView() of listview already executes for the initial items , even though the listview is not currently in view.
Hence when a user scroll downs , he sees the list plainly.
I am unsure how to go about this situation . Is there any callback that can be invoked when a row from a listview becomes visible on screen ? .
Yes there is a callback (OnScrollChangeListener), first visible index and last visible index etc. You can achieve what you are trying to using combination of these.
But you need to try that yourself first. No one can simply write a code for you.
You can learn about listview here

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.

Android ArrayAdapter tell all other views to refresh themselves

I need all of the other views in my arrayadapter to check their attributes and return to a default attribute at certain times
use case: listview items (a)(b)(c)(d) , when you touch (a) it's background turns black. This is done in the ontouchListener. But when you touch view (b) it's background turns black BUT (a)'s needs to turn back to the default which is not black
I dabbled around with recalling the arrayadapter at the end of the view.OnClickListener but the problem with this is that it also resets the scroll position of the view, so if this list was much longer - which it is - and user touched item (r) , then it would reset the list and put the user back at item (a) at the top
I was looking at notifydatasetchangedbut I am not sure how to use that and where it should work
insight appreciated
notifyDataSetChanged will do the job. You can call that at any time in your code and this will trigger the adapter to call getView for the visible list items again and so update their content. So basically the only thing you have to do is to update the list item states or information before calling that method.

On Android, click to expand list -and- click on a button?

I have just started my career as an android programmer, and am currently relying heavily on the sample code and api examples. I have been working with this api example, to produce an expandable list of items (note this example does not use the ExpadableListView).
In playing with the example, I tried to add another widget that would become visible and be gone at the same time as the text (mDialogue in the sample code). This works well with another TextView, but as soon as I tried to add a button widget, it stopped working. The list would expand on first click, showing my hidden TextView and Button, but it will not disappear on further clicks. The button is however, clickable, and I was able to set up an onClick listener to change the button text back and forth.
I'm starting to wonder, is it just not possible to have a clickable item inside a clickable list item? Or is there some kind of work around? Would it solve my problem if I used ExpandableListView?
You have two options of how to handle focus within a ListView controlled by ListView#setItemsCanFocus(boolean). If you want individual Views within a list item to focus so that the user can interact with them individually instead of with the list item as a whole, call it passing true. false is the default behavior.
The default behavior where ListView manages item focus and clicks is basically a shortcut/optimization for the common case where the whole item acts as a single unit from an interaction standpoint, but its layout may be complex. When you tell ListView that its items can focus it disables this special behavior and you should use the more traditional mechanisms of handling events on the Views within the list items. (Listeners, overridden on* methods, etc.)
But why do your list items stop taking clicks when your ListView isn't set up for focusable items? ListView will only generate item click events if the list item view returns false from View#hasFocusable(). This means no children of the list item can be focusable if you want to receive item click events for it. As soon as your button becomes visible, the list item has a focusable child and will no longer receive list item click events.

Categories

Resources