BaseAdapter導致notifyDataSetChanged()無效的四個原因及處理方法

來源:互聯網
上載者:User

前一段時間在做一個項目的時候遇到了一個關於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)即可。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.