前一段時間在做一個項目的時候遇到了一個關於BaseAdapter的notifyDataSetChanged()方法無效問題,當時在網上搜了一個解決方案,今天又遇到了一個類似的問題,我在這裡做個記錄,防止以後再次發生,或者其他朋友再次遇到。
一、ScrollView中嵌套ListView或GridView
原因:兩個的滾動監聽衝突
解決方案:重寫ListView或GridView
package com.meritit.lottery.view;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;public class SerialListView extends ListView {public SerialListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public SerialListView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public SerialListView(Context context) {super(context);// TODO Auto-generated constructor stub}/** * 為了取消滾動效果,可以放入滾動組建中重寫了此方法 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expandSpec);}}
二、ListView或GridView的外部容器重寫onTouchEvent(MotionEvent event)方法
詳細請看:http://blog.csdn.net/xxxzhi/article/details/12314775
這類問題解決方案很簡單,只需要onTouchEvent返回false即可
例如:
@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stub final int action = event.getAction(); final float x = event.getX(); final float y = event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: System.out.println("父類點擊onTouchEvent"); Log.i("", "onTouchEvent ACTION_DOWN"); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(event); } if (!mScroller.isFinished()){ mScroller.abortAnimation(); } mLastMotionX = x; mLastMotionY = y; break; case MotionEvent.ACTION_MOVE: System.out.println("父類滑動onTouchEvent"); int deltaX = (int)(mLastMotionX - x); if (IsCanMove(deltaX)) { if (mVelocityTracker != null) { mVelocityTracker.addMovement(event); } mLastMotionX = x; scrollBy(deltaX, 0); } break; case MotionEvent.ACTION_UP: System.out.println("父類放開onTouchEvent"); int velocityX = 0; if (mVelocityTracker != null) { mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); velocityX = (int) mVelocityTracker.getXVelocity(); } if (velocityX > SNAP_VELOCITY && mCurScreen > 0) { // Fling enough to move left Log.e(TAG, "snap left"); snapToScreen(mCurScreen - 1); } else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) { // Fling enough to move right Log.e(TAG, "snap right"); snapToScreen(mCurScreen + 1); } else { snapToDestination(); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } // mTouchState = TOUCH_STATE_REST; break; } return false; }
三、資料傳值問題
注意改變Adapter內的資料,如下:list_contents和toparr是改變後的資料
mycqbaseAdapter.contents=list_contents;mycqtitleAdapter.toparr = toparr;mycqbaseAdapter.notifyDataSetChanged();mycqtitleAdapter.notifyDataSetChanged();
有一種錯誤的寫法就是直接調用notifyData方法
mycqbaseAdapter.notifyDataSetChanged();mycqtitleAdapter.notifyDataSetChanged();
四、ViewGroup中notifyDataSetChanged()無效
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) {// if (changed) {menu_view = getChildAt(0);content_view = getChildAt(1);content_view.measure(0, 0);content_view.layout(0, 0, getWidth(), getHeight());// }}注釋掉onLayout中的if(changed)即可。