Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何平滑列表项在特定位置的移动_Android_Scroll_Android Listview_Smooth Scrolling - Fatal编程技术网

Android 如何平滑列表项在特定位置的移动

Android 如何平滑列表项在特定位置的移动,android,scroll,android-listview,smooth-scrolling,Android,Scroll,Android Listview,Smooth Scrolling,我有listView。当物品处于特定位置时,他会改变颜色。看图片。上面有蓝色的方块-这就是我现在拥有的。向下-我需要的。但是当滚动时,当项目处于所需位置并改变颜色时,它不会顺利发生。该项目似乎跳转到所需的位置。我怎样才能使这个过程更顺利?谢谢 以下是我的视图类的一部分: public class TimeView extends View { ........................... @Override public void setOnScrollChangeL

我有listView。当物品处于特定位置时,他会改变颜色。看图片。上面有蓝色的方块-这就是我现在拥有的。向下-我需要的。但是当滚动时,当项目处于所需位置并改变颜色时,它不会顺利发生。该项目似乎跳转到所需的位置。我怎样才能使这个过程更顺利?谢谢

以下是我的视图类的一部分:

public class TimeView extends View {
...........................

    @Override
    public void setOnScrollChangeListener(OnScrollChangeListener l) {
    }

    AbsListView.OnScrollListener scrollListener = new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(final AbsListView view, int scrollState) {
            if (scrollState == SCROLL_STATE_IDLE) {
                View childView = view.getChildAt(0);
                int position = view.getFirstVisiblePosition();
                if (-childView.getTop() > childView.getHeight() / 2) {
                    position++;
                }
             
              view.setSelection(position);
               }
        }

        @Override
        public void onScroll(final AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            for (int i = 0; i < view.getChildCount(); i++) {
                View childView = view.getChildAt(i);
                if (i == 1) {
                    childView.setBackgroundResource(R.drawable.selected_color);

                } else {
                    childView.setBackgroundResource(R.drawable.no_select_color);
                }
            }
        }

    };

如果切换到使用RecyclerView而不是ListView(无论如何都应该使用ListView),则可以使用SnapHelper执行以下操作:

SnapHelper helper = new LinearSnapHelper();
helper.attachToRecyclerView(recyclerView);

如果切换到使用RecyclerView而不是ListView(无论如何都应该使用ListView),则可以使用SnapHelper执行以下操作:

SnapHelper helper = new LinearSnapHelper();
helper.attachToRecyclerView(recyclerView);
试试这个:

    @Override
    public void onScrollStateChanged(final AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE) {
            View firstView = view.getChildAt(0);
            Rect rect = new Rect(0, 0, firstView.getWidth(), firstView.getHeight());
            view.getChildVisibleRect(firstView, rect, null);
            int position = view.getFirstVisiblePosition();
            if (rect.height() < (firstView.getHeight() / 2)) {
                position++;
            }
            final int finalPosition = position;
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.smoothScrollToPositionFromTop(finalPosition, 0, 500);
                }
            }, 1);
        }
    }
更新: 上述代码具有500毫秒的恒定稳定时间。下面的代码具有恒定的捕捉速度0.2像素/毫秒,这意味着向上/向下滚动一个像素需要5毫秒

    final static int SCROLL_TIME_PER_PIXEL = 5; // Time(ms) to snap a pixel.
    @Override
    public void onScrollStateChanged(final AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE) {
            View firstView = view.getChildAt(0);
            Rect rect = new Rect(0, 0, firstView.getWidth(), firstView.getHeight());
            view.getChildVisibleRect(firstView, rect, null);
            int position = view.getFirstVisiblePosition();
            int offset = rect.height();
            if (rect.height() < (firstView.getHeight() / 2)) {
                position++;
            } else offset = firstView.getHeight() - rect.height();
            final int finalPosition = position; // Snap to position.
            final int finalOffset = offset; // Snap distance in pixels.
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.smoothScrollToPositionFromTop(finalPosition, 0, finalOffset * SCROLL_TIME_PER_PIXEL);
                }
            }, 1);
        }
    }
试试这个:

    @Override
    public void onScrollStateChanged(final AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE) {
            View firstView = view.getChildAt(0);
            Rect rect = new Rect(0, 0, firstView.getWidth(), firstView.getHeight());
            view.getChildVisibleRect(firstView, rect, null);
            int position = view.getFirstVisiblePosition();
            if (rect.height() < (firstView.getHeight() / 2)) {
                position++;
            }
            final int finalPosition = position;
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.smoothScrollToPositionFromTop(finalPosition, 0, 500);
                }
            }, 1);
        }
    }
更新: 上述代码具有500毫秒的恒定稳定时间。下面的代码具有恒定的捕捉速度0.2像素/毫秒,这意味着向上/向下滚动一个像素需要5毫秒

    final static int SCROLL_TIME_PER_PIXEL = 5; // Time(ms) to snap a pixel.
    @Override
    public void onScrollStateChanged(final AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE) {
            View firstView = view.getChildAt(0);
            Rect rect = new Rect(0, 0, firstView.getWidth(), firstView.getHeight());
            view.getChildVisibleRect(firstView, rect, null);
            int position = view.getFirstVisiblePosition();
            int offset = rect.height();
            if (rect.height() < (firstView.getHeight() / 2)) {
                position++;
            } else offset = firstView.getHeight() - rect.height();
            final int finalPosition = position; // Snap to position.
            final int finalOffset = offset; // Snap distance in pixels.
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.smoothScrollToPositionFromTop(finalPosition, 0, finalOffset * SCROLL_TIME_PER_PIXEL);
                }
            }, 1);
        }
    }

加文·赖特,谢谢你的建议。但我必须用ListViewGavin Wright来做,谢谢你的建议。但是我必须使用ListViewi___________________________________?谢谢。谢谢。我会尝试报告结果。我尝试过使用你的建议,但似乎效果不错。非常感谢你!我是阿莫,你能解释一下这个方法是怎么起作用的吗?谢谢。谢谢。我会尝试报告结果。我尝试过使用你的建议,但似乎效果不错。非常感谢你!