I have an ExpandableListView which I've been using for a while in some of my apps now.
Recently however, I had to set the layout param of it as follows:
android:layout_width="match_parent"
android:layout_height="wrap_content"
since in this particular instance of the listview i have very few children. When I load the app at first, everything is working properly, and the height of the listview is exactly as much as it's children.
Then I added an EditText box above my list. Naturally, the keyboard appears and pushes up the listview. Lets say it has 8 children total. When the keyboard has been pressed 2 of them become invisible and the getChildCount() method returns 6, and a scrollbar appears, allowing me to view the 2 invisible ones.
The problem is, that when i get the keyboard out of the screen, the listview retains it's scrollbar and getChildCount() keeps returning 6, even though it now has enough space to stretch its layout to the way it wass on app start...
I rely very heavily on getChildCount to perform some calculations in onMeasure() and this behaviour is totally bugging everything.
Any insight as to why this is happening and how i could fix it?
Related
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
I have a RecyclerView in my app. It is part of a fragment (one of several) in an activity. The problem is, when the keyboard is closed it will max out in height and use its internal scroller. When the keyboard opens, the internal scroller turns off and the RecyclerView shows all its children.
The RecyclerView has the option for elements to be added or removed by the end user. In my full implementation, it shows four elements before starting to scroll (with the keyboard closed). When it is the sole fragment, it will max out its height at the screen height.
I've tried setting setting the NestedScrollEnabled to false and while this does stop scrolling, the items it would normally scroll to are no longer accessible. The RecyclerView still changes height depending on keyboard status so the 'hidden' rows become visible when the keyboard is open.
So in short, my RecyclerView is changing its height depending on keyboard visibility. How do I always make it show all its children?
Simplified fragment code that still shows the issue.
Java: https://gist.github.com/anonymous/bd46e137a0fb52f79399c11ba5be61bf
XML: https://gist.github.com/anonymous/c9bfb3f7577f75befc7aa6d5569311ce
I'm using com.android.support:recyclerview-v7:24.2.1
I've encounter an issue using last RecyclerView version.. and even AOSP project don't use the last RecyclerView version.
So ,maybe this will solve your problem, use 23.x.x version and let me know if that resolved the problem :)
I have a ScrollView with a RelativeView beneath it. Within that RelativeView, there are 3 views in it. 2 of those 3 are initially hidden.
Even though on my phone the scroller_rel doesn't take up the whole screen, I can still scroll through as if scroller_rel2 and scroller_rel3 are there and not hidden.
Programmatically I will decide if scroller_rel2 and scroller_rel3 are hidden or visible, and I'm wondering how to also then decide if we should be able to scroll or not.
The easy way to ask this question is: How can I programmatically tell ScrollView the height of the visible contents, so that if the contents are not larger than the container, we disable scrolling, and if say 2 of the 3 are showing, how can we then enable scrolling only to the bottom of scroller_rel2?
The reason you're having this problem is because setting the view to be View.INVISIBLE does just that, but it still takes up space in your layout, so that's why you can still scroll. What you should be using is View.GONE, this actually sets the view to be invisible and it removes it from the layout. Check out the documentation here:
GONE
INVISIBLE
I'm working with a rather large layout that includes a ListView and a LinearLayout beneath it as a footer. I first tried to wrap this in a ScrollView to make the whole layout scrollable, but ran into problems due to both the ListView and the ScrollView being incompatible with each other since they both have scrollable features. So, a workaround was to include the LinearLayout as a footer to the ListView.
Now, in the LinearLayout, I have buttons at various places that the user can click to return to the top of the page. The behaviour I am getting from this is odd, to say the least.
If I have not scrolled down too far, the buttons function normally. However, if I scroll down a bit too far, then clicking the button (even the same buttons that previously worked) will result in the layout scrolling up to roughly half of the way up the listview instead of the top.
Here is the method that does the scrolling, it's rather simple:
public void backToTop(View view) {
lv = (ListView)findViewById(R.id.listview);
lv.smoothScrollToPosition(0);
}
This method is triggered when any of the buttons are clicked.
I have also tried to use the "scrollTo(0, 0)" function, but that failed to do anything.
Any help would be appreciated!
**edit: After testing some more, it appears as though the point where scrolling does not seem to function properly anymore is when the listview is no longer visible on the page. As soon as I scroll past it, the buttons begin to function incorrectly.
Edit 2: SOLVED. My solution: I changed the line
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!
My solution: I changed the second line of my backToTop method:
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!
Is there a way I can get a context menu pop up when a user long presses on the blank space of a listview? I know that this can be done by setting wrap_content to the layout_height parameter of the listview. In fact I have been doing that successfully for a while. However, sometimes this wrap_content behaves very strangely and though there is enough space on the screen the listview restricts itself to a % of the screen and items scroll within that space. To avoid that problem I have moved to the path of setting the height as 0dp and weight as 1. However, that has disturbed the functionality I had in terms of long pressing the empty area of a list to add a new item to the list. Any help will be greatly appreciated.
Note: I have looked at multiple similar questions on SO throughout the day today but couldn't find any conclusive and elegant solution.
You can use ListView#addHeaderView() or ListView#addFooterView() to add extra view at the top or bottom of ListView, which you can make it looks like blank space.
Also I suggest you use match_parent to the layout_height attribute of ListView.