DevsmartLib-Android icon indicating copy to clipboard operation
DevsmartLib-Android copied to clipboard

HorizontalListView: scrollTo last items

Open httpdispatch opened this issue 13 years ago • 13 comments

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)

httpdispatch avatar Dec 09 '12 11:12 httpdispatch

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

httpdispatch avatar Dec 09 '12 11:12 httpdispatch

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);

httpdispatch avatar Dec 10 '12 12:12 httpdispatch

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 avatar Jan 06 '13 17:01 Dq

@Dq do you use scrollTo method?

httpdispatch avatar Jan 07 '13 07:01 httpdispatch

@Dq also do you have padding specified on the root layout container for your items?

httpdispatch avatar Jan 07 '13 07:01 httpdispatch

@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,

Dq avatar Jan 07 '13 09:01 Dq

I think this is because of padding. It is used wrongly for offset calculation in one place.

httpdispatch avatar Jan 07 '13 09:01 httpdispatch

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

httpdispatch avatar Jan 07 '13 09:01 httpdispatch

<root layout>
<child layout padding=15/>
</root layout>

httpdispatch avatar Jan 07 '13 09:01 httpdispatch

Assuming is the HorizontalListView, then the items I have inside of it (LinearLayouts) are the ones with the padding. So I think what you're telling me to try is the way it is already (not) working.

Dq avatar Jan 07 '13 10:01 Dq

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>

httpdispatch avatar Jan 07 '13 11:01 httpdispatch

OK, so that was indeed the issue. Had to change the padding to the child views. Thanks!

Dq avatar Jan 07 '13 13:01 Dq

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.

michaudhugo avatar Jul 17 '13 11:07 michaudhugo