我使用以下内容在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上的最后一项是可见的。