Button in ListView malfunctioning - android

I have a button in each item of a ListView whose background is defined by an XML, one background when enabled and another when disabled. When the ListView loads, it comes out correct. But, for some reason I can't figure out, if I scroll down and then scroll back up, the wrong background shows up.
I'd like to know the solution to this problem, but besides that, in general what I want to accomplish is this:
I have a button in the ListView to take the user to the website for the given item. If there is no website, I want the button to disappear, or be disabled. I seem to have the same problem with both options.
Thanks in advance for your efforts

It seems most likely that the problem lies with your getView() method. Android recycles views to save memory, so, for example, when you scroll down, it calls getView(int, View, ViewGroup) on your adapter where View is the item that just left the top of the screen. If you're not re-populating the item with the new data from the adapter, (ie, just returning convertView) it will put the View that left the top of the screen where the "new" one should be.

Related

Focus switching to next row in nested recyclerview in FireTv App

In our fire tv app, we are using a nested recyclerview where every vertical item have a horizontal row as a child item like playstore design.
Scrolling with dpad working fine normally but when i hold the right key for few seconds, item start scrolling very fast because it gets many events & within few second focus goes to the next random row even current row has items to scroll. So this whole problem happening in horizontal scroll(child recyclerview). I have already tried many solutions like this, this, this.
Also tried the custom layout manager, custom focus layout & other approach like slowing down the recyclerview scroll etc approach but all not working.
As far as I know, this happens at the moment when the last item is in focus, and the next item is not visible, that is, the ViewHolder has not updated the data of the first cell, so it is not clear which element to show next as if the list has ended.
You need to make sure that at least the edge of the next element is always visible on the screen.
Maybe you should play with the cell size or divider.
Edit:
The secret is to use the leanback library. It isn't lost focus!
Your gradle:
// Leanback support
def leanback_version = "1.2.0-alpha01"
implementation("androidx.leanback:leanback:$leanback_version")
Change in the layout from RecyclerView to HorizontalGridView (VerticalGridView)
Change in the Fragment import from RecyclerView.widget.GridLayoutManager to androidx.leanback.widget.GridLayoutManager
It works very fast for me and the focus no longer jumps.
Here a good article with animations but little outdated

Center aligned selection with animation in ListView (?)

I need a component that works like the picture below but I'm having trouble coming up with some kind of decent solution that works.
I want the list to have a center locked selection but being scrollable with the d-pad. This is for an application running on a TV so no need for touch scroll. So when pressing down on the remote d-pad the list will scroll and a new item will size up and the current selected one will size down and the new selection will still be in the middle.
I've tried doing this using a ListView that I extended and programmatically scrolling when pressing down or up. On scroll finished I called notifyDatasetChanged() on the ListView for re-inflating of the childs and in the ListViews adapters getView() I made the animation of the view located at the current selected position.
This is not optimal since I need to call notifyDatasetChanged(), which re-inflates all visible views, for the animation to apply. The UI becomes laggy when doing this and scrolling fast. It's also not possible to make som kind of compress animation when current selected item goes out of selection. There is also some trouble with the end items (read views) such the first or last in the list when doing animation of them, the may sometimes go out of screen.
I think that his must have been done before and maybe I'm missing it when searching for an answer.
Have anyone done something similar or do you have some suggestions of how this can be achieved? Maybe I'm just starting of with the wrong component here..
Regards,
Kristoffer

Scrolling of list below another view

I have seen an app which has different type of scrolling. I hope images attached will be able to explain it. Here the list when scrolled up, moves up by taking DownloadAll, Playall button up too. Later on DownloadAll and PlayAll button stops going up and only the list scrolls up.
I don't know what this is called as. I tried searching, but could not find anything similar.
How can I achieve this type of scrolling?
Take a look at "Libraries for developers" it has a section of listview where you can find different styles of listviews (plus scrolling animations such as sticky, animated scrollview, flabby etc...)

How to update listview item cell?

I have a problem updating android listview items cell.
The listview_item.xml is an ordinary listview item except that it has a hidden panel at the bottom. So the last part of listview_item.xml is LinearLayout that has another button inside it. This whole layout is set to gone until user clicks on a visible button in the list item. This sets the hidden LinearLayout to visible and it displays. So far so good. Now the hidden LinearLayout is open, and showing and I want to close it. So I click on a button that is inside this previously hidden linear layout and set the visibility to GONE which was its previous state. The result is nothing happens right away. The panel remains open until I scroll it out of site and back again. Then it does not show again. I'm sorry I don't have the code to post but thats the whole situation. I just cannot get a list item to refresh on the spot. Opening using a visible button works fine. But closing the same panel from a button within the panel seems to do nothing. I have tried running dataSetChanged() etc from inside the adapter but only scrolling out and into view will close the panel. Does anyone know why closing is different from opening? Why I get an immediate reaction to setting gone to visible but not the other way. The only difference it seems is that I am firing the event from a button on the previously hidden panel. Sorry its wordy, but don't have the code in front of me at the moment.
I think nobody here can help you without a code. notifyDataSetChaged() is the method that must help you, but if it is not - it obvious that you do something wrong. You must save visibility state of element of each list item in associated with this list item object (or model whatever). Check your code one more time and debug it.

Android ListView with clickable items in it's rows causes problems continuing scrolling

I've been trawling the internet looking for an answer for several hours, but I can't seem to find anyone who has been able to solve this.
I've got a listview which uses a custom adapter. A row looks like this
The list is filled by an array. Everything works great.
Now, I want the ImageView and the ToggleButton to react to clicks, so I implement the OnClickListener in my adapter, put the items position in each view's tag, and then I set their onclicklistener to this.
Works great, except now I can't use the onListItemClick for starting an activity for the item! OK, I say, I just make the relativelayout holding the text in the middle there use the same onclicklistener. Works great. Everything is clickable, and life is good.
EXCEPT! Now, when I scroll the list, I cannot "continue" the scroll by just flinging again. This causes the scrolling to stop, and I have to fling once more to get it going again. It seems the onclick-thingy causes the fling-motion to be interpreted as a tap or something (it does not trigger the logic within onClick).
I know that this is possible by just going to the phone list on my HTC Hero, which has exactly the kind of layout and behaviour I want from my app. This app even seems to have the onItemClickListener working.
So how can I make sure the list keeps scrolling, and still be able to click the togglebutton, listitem and the imageview? I've been stuck on this all day, and it's giving me a headache :(
Another quick search before I go home, and I came across this:
How to fire onListItemClick in Listactivity with buttons in list?
CodeFusionMobile mentions setting the android:descendantFocusability property of the list layout to "blocksDescendants", and so I tried doing that. Everything works as expected now. Scrolling works, onListitemClicked works, clicking the ToggleButton works, clicking the ImageView works.
Just to make it clear... In the parent RelativeLayout, right at the top of the row.xml, I added
android:descendantFocusability="blocksDescendants"

Categories

Resources