HorizontalListView: scrollTo last items
If i use scrollTo to the one of the last item mMaxX is not calculated properly. This causes problem in scrolling to right (it can't be fully scrolled)
Current mMaxX forumla is
mMaxX = mCurrentX + rightEdge - getWidth();
But rightEdge can be 0 after all the items removed in removeNonVisibleMethods which gives inaccuracy in calc
Scenario is like this First list is created and added 8 items. item width is 100.
mCurrentX is 0
Next requested scroll to 10000 removeNonVisible method removes all the childs so the rightEdge passed to fillListRight become 0 When the last item reached mMaxX become calculated. But such as starting rightEdge was 0 mMaxX will be less than necessary on 800 pixels and list will not be scrollable properly on the right side
Fix proposal. Modify fillList method and use default edge value as mDisplayOffset instead of 0. Currently testing such solution so not sure how proper it is.
int edge = mDisplayOffset;
View child = getChildAt(getChildCount() - 1);
if (child != null) {
edge = child.getRight();
}
fillListRight(edge, dx);
I'm getting a similar issue, but this fix does not work on my case.
On a 1280px screen I'm drawing 6 items with width 220. All of them are visible, but when i scroll to the right, since none of the items need to be re-calculated, i'm only able to scroll 40px, which is about the middle of the last item.
Any suggestions on this?
@Dq do you use scrollTo method?
@Dq also do you have padding specified on the root layout container for your items?
@httpdispatch yes, the scrollTo method is being called when I swipe on the list. Also, the horizontal list view itself does not have padding, but the items have a padding of 15 on left and right sides. The list work well if I have more or less than 6 item,
I think this is because of padding. It is used wrongly for offset calculation in one place.
private void positionItems(final int dx) {
if(getChildCount() > 0){
mDisplayOffset += dx;
int left = mDisplayOffset;
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
child.layout(left, 0, left + childWidth, child.getMeasuredHeight());
left += childWidth + child.getPaddingRight();
}
}
}
child.getPaddingRight() seems incorrect here
try to remove padding attribute from the items root and add pading to child
<root layout>
<child layout padding=15/>
</root layout>
Assuming
HorizontalListView childs root should not have padding. So if you have layout for child item LinearLayout with padding 15 move that padding from root to child Layout for child item before
<LinearLayout padding = 15>
<ChildView/>
</LinearLayout>
after
<LinearLayout>
<ChildView padding = 15/>
</LinearLayout>
OK, so that was indeed the issue. Had to change the padding to the child views. Thanks!
I'm getting an issue too with the scrollTo method, but I make no use of paddings. Sometimes, I can't scroll to the x position that I would like to reach. For example, if I ask for 180 px, it scrolls for an amount of 100px.