RecyclerView smoothScrollToPosition bug - android

I have a RecyclerView with GridLayoutManager and a button, when i click the button i want to smoothScrollToPosition. My custom view is simply a RelativeLayout containing a TextView. When i run the program on Lollipop everything's fine, i press the button and the application scrolls smoothly to the given position. When i run the same code on pre lollipop devices or virtual machines, i click the button and the app instead of smoothScrolling starts lagScrolling, it scrolls but with so much lag it's not even funny. The same code but with ImageViews instead of TextViews in the custom View doesn't lag, almost at all. Is this a known bug or can it be solved? My code is just a basic implementation of a RecyclerView with adapter but if somebody needs to see it i'll post it.

Please post your CustomView xml and your adapter code.
You probably set your TextView's width to "wrap_content", and then you probably performing TextView setText() inside adapter, causing it to measure the width of the component again each time onBindViewHolder() is called.
You should try changing TextView width to "match_parent" or some static value in dp.
Please take a look at this question

Related

ListView with footer attached, smoothScrollToPosition not functioning correctly

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!

ListView slowing down ViewPager swipe

I have a ViewPager that contains ImageViews downloaded from the net and it slides just fine when my ListView (in another separate layout) is not initialized.
But the problems start when my ListView is created. The ViewPager still "slides" but only slides about 1/4 of the way, lags, then loads the next ImageView correctly.
All of the network and bitmap operations are done using AsyncTask. I've also used the RemoteImageCache API from Singly API which works pretty well.
Any ideas as to why ViewPager's swipe is lagging?
The slow down was caused by the ListView calling getView more than necessary (more than 3x per item in my list).
According to this solution, the ListView's width and height must be given specific values and not wrap_content or else getView() will be called multiple times.
it's just because of list view scroll is conflict with view pager scroll..
if you want to know about this just set list view visibility gone don't remove code listView.setAdapter(adapter) then try view pager will slide properly.
Recently (2020), must be to implement a ViewHolder class to you get speed, or, refactoring with RecyclerView, it depends of the size of your list and the time that you have to execute this methods, like my software was already production stage, i preferred use ViewHolder class, the speed gain came to be 40%, sufficient to me, if you need the more speed, must be to use RecyclerView (refactoring your listview, edit .xml, your adapter, use Linear or Grid layout manager...).

Android expandablelistview can not display the last group when expanded

I'm trying to implement a FAQ screen in my project and I wanted to use ExpandableListView. I display question as a text in GroupView and answer as a text in ChildView. If items can fit into screen, there's no problem, it works as it should. But when there's more items than the screen height and I have to scroll, this problem occurs that when I expand the last item, it acts like it's expanded but I can not scroll to the child view.
Any ideas or experiences on this issue?
Here's the screenshot (as you can see, scrollbar shows that it's expanded and there's place to scroll, but when I try to scroll, no luck) :
EDIT: I found out that this problem is because I'm using a WebView inside child row, when I switched to TextView it works as expected. How can I achieve the same result with WebView?
In your getGroupView() method of BaseExpandableListAdapter, give view=null as the first line of the method.

How to prevent ListView items highlighting in Black

I have been trying desperately to fix this issue where my ListView items are highlighted. I have reviewed any solution I could find here on stackoverflow, and have found none that work for me.
First off, this is a ListView in a LinearLayout in a ViewPager in a LinearLayout.
Each Item in that Listview is a LinearLayout containing a LinearLayout (Containing a TextView) and a Gallery. The Gallery contains multiple LinearLayouts containing a TextView and Imageview in a FrameLayout.
This problem occurs, not on click but on drag, as when swiping your finger over a story (sometimes while the ListView scrolls with your drag) the item underneath becomes selected. Additionally, this particular problem only occurs on devices where the over-scroll effect does not cause a bounce when scrolling.
Attempts to solve the issue are as follows:
Set the List Selector to the Background Color in XML (Fail)
Set the List Select to Alpha #00000000 in XML (Fail)
The Above but Programatically
The Above but by theme
Code that sets the selection to -1 onListItemClick
Removing the background from list item's LinearLayout (Works on 4.0)
Setting android:cacheColorHint to #00000000 (does nothing?)
I just can't figure out why it's doing this and I really need it to stop.
This was a most interesting problem. The solution of which is even more interesting.
For some reason, what I didn't try, and what nobody thought of, was to remove android:background from the LinearLayout for the ListView items.
Upon removing android:background, the layout continues to display properly and the black no longer appears.
If you have a problem similar to this that this does not solve, you may want to check the Android Blog: Why is my list black?
EDIT: This worked fine for ICS, but elicited a much larger problem in 2.1. To completely solve the ordeal, I ended up having to specify android:cacheColorHint="#000000" to the ListView (and the LinearLayout for good measure) in my inline styles as well as in the style definition I applied to the ListView.

Scrolling issues with GridView in Android

I am having weird scrolling issues in my pretty simple GridView. Each item in the Grid is simply an ImageView and a TextView. The activity itself is simply an application selector. It looks like a rough version of the basic App Tray in Android.
The issue is that after spending some times scrolling through my view, it will inevitably allow me to continue scrolling past the top row of icons, to a blank screen, and the scroll bar will disappear, leaving me stuck.
It doesn't happen every time I go to the top of the view, only sometimes, and usually only after some excessive scrolling.
If I happen to notice the problem and catch it before the top row disappears off the bottom of the screen, I can usually scroll back through the view and spot some icons missing. There are empty spaces in the grid, and I can only assume that those icons have been moved to some bizarre position, which is allowing the view to scroll past the top.
This is my first Android app beyond a basic Hello World, so it's likely that I've just screwed up something in my layout files. I also realize that this is probably a pretty confusing description, so I'm hoping someone has experienced this and my search abilities simply were unable to find it.
I can post my layout files or other code if someone thinks that's useful.
Oh, and the program is built against 1.5, but is running on 2.2 (whatever state of 2.2 that was that snuck out last week) on my phone. I don't have enough apps to test this on an emulator, but could probably set something up if someone felt it necessary.
Thanks in advance for any help on the issue.
I had same problem, but I post to GridView only ImageView's so pengwang's answer didn't help me. I found explanation for this bug https://code.google.com/p/android/issues/detail?id=16218
The problem is that GridView does not currently handle items with differing heights.
My solution is to use exactly specified layout_height same for all GridView's items.
i have solved it: in the link GridView cannot show image you can try
It seems that if you scroll off the bottom of the GridView and back again very fast a couple of times, you eventually will notice that the elements of the view get shifted one place to the "left" from where they originally were. When this happens, you then can scroll all the way off the top of the GridView. When you do reach the top of the list, item 0 is not displayed, having item 1 in its place and nothing but blackness above that row.
Clicking on any item in this situation resets the list and everything is back to normal.
This must be a bug. It's highly reproducible, and I don't think anything I'm doing is causing the mix up. At this point, adding to the list adapter has stopped.
Right now I'm working with Android 2.3.3.
I just ran into this exact same issue myself -- I have a grid of images that have different heights. After reading the Android developer's doc on GridView, GridView.LayoutParams and GridLayout, I did two things that fix my issue:
find the tallest image in each row, and store the height in a map of row -> rowHeight. Each image that I download has width and height info, so this is easy for me to do by overriding my adapter's addAll(items) method.
in getView(), I create a GridView.LayoutParams(MATCH_PARENT, getRowHeight(position)), which sets each grid item to the max row height for its specific row.
wrap my ImageView inside a LinearLayout. I have tried other layout but LinearLayout is the one that works. Set android:scaleType="fitCenter" and android:adjustViewBounds="true" for the image view.
After above 3 steps I finally got the grid to look right, they have different heights, and there's no scrolling issues.
I had a similar, but probably less common problem, where just scrolling a little bit would scroll the gridview to the end and I was not able to scroll up at all. This only occurred when the gridview was smaller, for example when the keyboard was visible. The problem was that I used:
android:paddingBottom="400dp"
android:clipToPadding="false"
Making padding bottom much smaller fixed my issue. I hope this helps at least someone!

Categories

Resources