在做Android的一个弹跳的ListView [英] Make a bouncing ListView in Android

查看:132
本文介绍了在做Android的一个弹跳的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的列表视图反弹。为了解释自己,我想在ListView具有相同的行为IOS列表视图对象。在顶部和上列表的底部,用户可以通过刷他的手指打开名册

I'm trying to make my listview "bounce". To explain myself, I want the ListView to have the same behavior as the iOs List View object. On the top and on the bottom of the list, the user can go over the list by swiping his finger.

这行为。

在最设备我测试,该列表现在作用于滚动不同,它会显示一个蓝线变亮,当你同时扫描指纹。

On the most devices I tested, the list is now acting different on scrolling, it displays a blue line that gets brighter when you swipe your finger moreover.

我找到了BounceListView像这样的:

I found the BounceListView like this one :

public class BounceListView extends ListView
{
    private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;

    private Context mContext;
    private int mMaxYOverscrollDistance;

    public BounceListView(Context context) 
    {
        super(context);
        mContext = context;
        initBounceListView();
    }

    public BounceListView(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        mContext = context;
        initBounceListView();
    }

    public BounceListView(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);  
    }

}

但是,这ListView中的问题是,它没有一个滚动的列表上后返回到所述第一或到最后一个项目...它停留在哪里名单没有充满的位置上。

But the problem of this ListView is that it doesn't go back to the first or to the last item after a scroll over the list... It stays on a position where list is not filled.

任何人有一个想法,使其工作?

Anyone got an idea to make it work ?

在此先感谢!

推荐答案

您应该重写 onOverScrolled ,这就是所谓的说,名单已经结束滚动,并在该函数滚动的ListView 回到你想要使用的点 smoothScrollToPosition

You should override onOverScrolled, which is called to say that the list has been over scrolled, and in that function scroll the ListView back to the point where you want it using smoothScrollToPosition.

这将会是这个样子:

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    if(scrollY < 0) {
        smoothScrollToPosition(0);
    } else if(scrollY > MAX_SCROLL) {
        smoothScrollToPosition(getAdapter().getCount());
    }
}

MAX_SCROLL将必须由您使用列表项的高度和适配器项目的数量来确定,虽然它看起来像你已经想通了你的问题,以便它不应该是一个问题。

MAX_SCROLL will have to be determined by you using the height of your list items and the number of items in your adapter although it looks like you've already figured that out in your question so it shouldn't be a problem.

这篇关于在做Android的一个弹跳的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆