BaseAdapter导致notifyDataSetChanged()无效的三个原因及处理方法

时间:2023-06-08 13:13:56

原文  http://blog.csdn.net/dawanganban/article/details/21376979

前一段时间在做一个项目的时候遇到了一个关于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
} /**
* 为了取消滚动效果,可以放入滚动组建中重写了此方法
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> ,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
} }

二、ListView或GridView的外部容器重写onTouchEvent(MotionEvent event)方法

详细请看:http://blog.csdn.net/xxxzhi/article/details/12314775

这类问题解决方法很简单,只需要onTouchEvent返回false即可

例如:

@Override
public 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, );
} break;
case MotionEvent.ACTION_UP:
System.out.println("父类放开onTouchEvent");
int velocityX = ;
if (mVelocityTracker != null)
{
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity();
velocityX = (int) mVelocityTracker.getXVelocity();
}
if (velocityX > SNAP_VELOCITY && mCurScreen > ) {
// Fling enough to move left
Log.e(TAG, "snap left");
snapToScreen(mCurScreen - );
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - ) {
// Fling enough to move right
Log.e(TAG, "snap right");
snapToScreen(mCurScreen + );
} 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();