如何以编程方式在Horizo​​ntalListView中滚动?

时间:2012-04-05 13:42:27

标签: java android listview

我使用以下内容在HorizontalListView类中执行新逻辑:

private DataSetObserver mDataObserver = new DataSetObserver() {

    @Override
    public void onChanged() {
        synchronized (HorizontalListView.this) {
            mDataChanged = true;
            int firstVisiblePos = HorizontalListView.this.getFirstVisiblePosition();
            int lastVisiblePos = HorizontalListView.this.getLastVisiblePosition();
            Log.i(mTag, "firstVisiblePosl " + firstVisiblePos+ " lastVisiblePos  " + lastVisiblePos );
            if (mAdapter.getCount() > lastVisiblePos) {
                int sumTotal = 0;
                for (int i = firstVisiblePos; i < mAdapter.getCount(); i++) {
                    View child = HorizontalListView.this.getChildAt(i);
                    if (child != null) {
                        sumTotal += child.getWidth();
                    }
                }
                int sumPartial = 0;
                for (int i = lastVisiblePos; i < mAdapter.getCount(); i++) {
                    View child = HorizontalListView.this.getChildAt(i);
                    if (child != null) {
                        sumPartial += child.getWidth();
                    }
                }

                Log.i(mTag, "sumTotal " + sumTotal+ " sumPartial " + sumPartial);
                mScroller.startScroll(sumTotal, 0, sumPartial, 0);
            }
        }

        Log.i(mTag, " " + mCurrentX + "mNextX " + mNextX);
        invalidate();
        requestLayout();
    }

但这没有效果:我想滚动到左边的mAdapter上的最后一项是可见的。

0 个答案:

没有答案