import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.ListView;
/**
* Created by user on 26/7/2017.
*/
public class MyListView extends ListView {
private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;//可滑动范围
private Context mContext;
private int mMaxYOverscrollDistance;
public MyListView(Context context){
super(context);
mContext = context;
initBounceListView();
}
public MyListView(Context context, AttributeSet attrs){
super(context, attrs);
mContext = context;
initBounceListView();
}
public MyListView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
mContext = context;
initBounceListView();
}
private void initBounceListView(){
//get the density of the screen and do some maths with it on the max overscroll distance
//variable so that you get similar behaviors no matter what the screen size
final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){
//This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
}
@Override
/**
* 重写该方法,ScrollView中嵌套ListView
* 达到使ListView自适应高度
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
阻尼回弹效果,以及ScrollView嵌套listview导致显示一行数据的问题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 背景 咣当咣当咣当,乘着北京的地铁上班,突然俩小伙谈话被我听到了。"今天我遇到了一个很奇怪的问题,一个Scroll...
- 摘要:开发中经常遇到ScrollView嵌套ListView,GridView,或者RecyclerView嵌套R...
- 关于为什么只显示一行,个人理解是:如果单独使用GridView和ListView,里面的内容是固定的,底层系统很容...