From 4c63dde45eca8470be598ad4b811818ed5dcdf14 Mon Sep 17 00:00:00 2001 From: Martin Appl Date: Mon, 18 Feb 2013 14:18:37 +0100 Subject: [PATCH] Fixed scrolling. It used to call request layout on each step which is wrong and would probably cost you more than you get by view pooling. --- .../android/ui/HorizontalListView.java | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java b/devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java index 076ed38..6cd7d0a 100644 --- a/devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java +++ b/devsmartlib/src/com/devsmart/android/ui/HorizontalListView.java @@ -66,7 +66,7 @@ public HorizontalListView(Context context, AttributeSet attrs) { initView(); } - private synchronized void initView() { + private void initView() { mLeftViewIndex = -1; mRightViewIndex = 0; mDisplayOffset = 0; @@ -92,7 +92,7 @@ public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener liste mOnItemLongClicked = listener; } - private DataSetObserver mDataObserver = new DataSetObserver() { + private final DataSetObserver mDataObserver = new DataSetObserver() { @Override public void onChanged() { @@ -100,14 +100,12 @@ public void onChanged() { mDataChanged = true; } invalidate(); - requestLayout(); } @Override public void onInvalidated() { reset(); invalidate(); - requestLayout(); } }; @@ -133,10 +131,10 @@ public void setAdapter(ListAdapter adapter) { reset(); } - private synchronized void reset(){ + private void reset(){ initView(); removeAllViewsInLayout(); - requestLayout(); + invalidate(); } @Override @@ -147,7 +145,7 @@ public void setSelection(int position) { private void addAndMeasureChild(final View child, int viewPos) { LayoutParams params = child.getLayoutParams(); if(params == null) { - params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); + params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); } addViewInLayout(child, viewPos, params, true); @@ -158,9 +156,7 @@ private void addAndMeasureChild(final View child, int viewPos) { @Override - protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) { - super.onLayout(changed, left, top, right, bottom); - + public void computeScroll() { if(mAdapter == null){ return; } @@ -176,6 +172,8 @@ protected synchronized void onLayout(boolean changed, int left, int top, int rig if(mScroller.computeScrollOffset()){ int scrollx = mScroller.getCurrX(); mNextX = scrollx; + + postInvalidate(); } if(mNextX <= 0){ @@ -194,17 +192,15 @@ protected synchronized void onLayout(boolean changed, int left, int top, int rig positionItems(dx); mCurrentX = mNextX; - - if(!mScroller.isFinished()){ - post(new Runnable(){ - @Override - public void run() { - requestLayout(); - } - }); - - } } + +// @Override +// protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) { +// super.onLayout(changed, left, top, right, bottom); +// +// +// +// } private void fillList(final int dx) { int edge = 0; @@ -286,10 +282,10 @@ private void positionItems(final int dx) { } } - public synchronized void scrollTo(int x) { - mScroller.startScroll(mNextX, 0, x - mNextX, 0); - requestLayout(); - } +// public void scrollTo(int x) { +// mScroller.startScroll(mNextX, 0, x - mNextX, 0); +// invalidate(); +// } @Override public boolean dispatchTouchEvent(MotionEvent ev) { @@ -303,7 +299,7 @@ protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, synchronized(HorizontalListView.this){ mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0); } - requestLayout(); + invalidate(); return true; } @@ -333,7 +329,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, synchronized(HorizontalListView.this){ mNextX += (int)distanceX; } - requestLayout(); + invalidate(); return true; }