BaseAdapter导致notifyDataSetChanged()无效的四个原因及处理方法
2016-03-09 18:26
387 查看
原地址: http://blog.csdn.net/dawanganban/article/details/21376979?utm_source=tuicool&utm_medium=referral
前一段时间在做一个项目的时候遇到了一个关于BaseAdapter的notifyDataSetChanged()方法无效问题,当时在网上搜了一个解决方法,今天又遇到了一个类似的问题,我在这里做个记录,防止以后再次发生,或者其他朋友再次遇到。
一、ScrollView中嵌套ListView或GridView
原因:两个的滚动监听冲突
解决方法:重写ListView或GridView
二、ListView或GridView的外部容器重写onTouchEvent(MotionEvent event)方法
详细请看:http://blog.csdn.net/xxxzhi/article/details/12314775
这类问题解决方法很简单,只需要onTouchEvent返回false即可
例如:
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, 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)即可。
前一段时间在做一个项目的时候遇到了一个关于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 >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
二、ListView或GridView的外部容器重写onTouchEvent(MotionEvent event)方法
详细请看:http://blog.csdn.net/xxxzhi/article/details/12314775
这类问题解决方法很简单,只需要onTouchEvent返回false即可
例如:
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, 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)即可。
相关文章推荐
- DOM编程
- ubuntu 15.10 上编译 android 4.4 编译环境搭建
- 移动端专用数据库Realm介绍
- 蓝桥杯_算法练习《输出星号》
- 在创业公司做架构师,是一种修炼
- Boost之Json解析
- 【Win 10 应用开发】在后台进行多媒体转码
- MDT2013折腾记-不能抓取映像unable to determine Destination disk,partition driver
- Tomcat工作原理详解
- 安卓用户小心!安卓惊现支付宝钓鱼木马
- C#获得当前执行的函数名、当前代码行、源代码文件名
- Messenger实现Activity与Service的双向通信
- [WPF]Slider控件常用方法
- (三)基于PHP——复杂的WSDL的创建(WSDL篇)
- 什么样的公司值得加入?
- Ecstore中的微信支付怎么样配置
- sublime3 eslint 安装
- (二)基于PHP——简单的WSDL的创建(WSDL篇)
- Ecstore中的微信支付怎么样配置
- 构建之法阅读笔记02