列表视图捕捉项目 [英] List view snap to item

查看:158
本文介绍了列表视图捕捉项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用一个ListView图片列表,这些照片是将适合2至3张照片在屏幕上的尺寸。

I'm creating a list of pictures using a ListView and the photos are of a size that would fit 2 to 3 photos on the screen.

这时遇到的问题是,我想,当用户停止滚动,有形列表的第一项将捕捉到屏幕的顶部,例如,如果滚动端部和所述第一小部画面显示,我们滚动列表下来,画面总是完全显示出来,如果​​大部分的图片显示,我们滚动列表了,因此接下来的画面是完全可见。

The problem that I'm having is that I would like to when the user stops scrolling that the first item of the visible list would snap to the top of screen, for example, if the scroll ends and small part of the first picture displayed, we scroll the list down so the picture is always fully displayed, if mostly of the picture is displayed, we scroll the list up so the next picture is fully visible.

有没有办法在机器人与ListView来实现这一目标?

Is there a way to achieve this in android with the listview?

推荐答案

我已经找到一种方法来做到这一点只是听滚动并改变位置时,通过实施ListView.OnScrollListener结束滚动

I've found a way to do this just listening to scroll and change the position when the scroll ended by implementing ListView.OnScrollListener

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    switch (scrollState) {
    case OnScrollListener.SCROLL_STATE_IDLE:
        if (scrolling){
            // get first visible item
            View itemView = view.getChildAt(0);
            int top = Math.abs(itemView.getTop()); // top is a negative value
            int bottom = Math.abs(itemView.getBottom());
            if (top >= bottom){
                ((ListView)view).setSelectionFromTop(view.getFirstVisiblePosition()+1, 0);
            } else {
                ((ListView)view).setSelectionFromTop(view.getFirstVisiblePosition(), 0);
            }
        }
        scrolling = false;
        break;
    case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
    case OnScrollListener.SCROLL_STATE_FLING:
        Log.i("TEST", "SCROLLING");
        scrolling = true;
        break;
    }
}

的变化也不是那么顺畅,但它的工作原理。

The change is not so smooth but it works.

这篇关于列表视图捕捉项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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