如何禁用在Android的ListView的反弹时和反弹? [英] How do I disable the overscroll and the bounce in an android listview?

查看:140
本文介绍了如何禁用在Android的ListView的反弹时和反弹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListView 2设备的行为,要么变成黄色/橙色时,我反弹时,或者它可以overscrolled,然后扣回。后者的行为是不好的,因为它显示了,我想prevent它下面的背景。

The behaviour of my listview on 2 devices is that either it turns yellow/orange when I overscroll it, or that it can be overscrolled and then snaps back. The latter behaviour is bad because it shows the background beneath it which I want to prevent.

我试过:

listview.setOverScrollMode(ListView.OVER_SCROLL_NEVER);

和不显示背景了,但现在有一个非常恼人的反弹效果。是否有可能都禁止反弹和滚动反弹并使其所以没有滚动,当它到达终点的影响刚刚结束?

and it doesn't show the background anymore but now there is a very annoying bounce effect. Is it possible to both disable the bounce and the overscrolling and make it so the scrolling just ends without any effect when it reaches the end ?

PS:我在两台设备上采用Android 2.3

PS: I am using android 2.3 on both devices.

推荐答案

下面是我如何解决这一点,希望这将帮助那些搜索。关键是一个OnScrollListener附加到列表中,跟踪何时正在处理一个抛姿态,并且当列表的末端已经达到。然后,当一扔仍在继续,继续位置复位到结束时,如果系统尝试移动它。

Here's how I solved this, hopefully it will help those searching. The key is to attach an OnScrollListener to the list, keep track of when a fling gesture is being processed, and when the end of the list has been reached. Then, while the fling is still going on, keep on resetting the position to the end if the system tries to move it.

private ListView mListView;
private ListAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_list);

    mListView = (ListView) findViewById(R.id.listView);
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getList(25));
    mListView.setAdapter(mAdapter);
    mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    if(Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD || Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1){
        mListView.setOnScrollListener(new OnScrollListener(){
            private boolean flinging = false;
            private boolean reachedEnd = false;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                flinging = (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING);
                reachedEnd = false;
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(reachedEnd && flinging && (firstVisibleItem + visibleItemCount < totalItemCount)){
                    mListView.setSelection(mAdapter.getCount() - 1);
                }else if(firstVisibleItem + visibleItemCount == totalItemCount){
                    reachedEnd = true;
                }else{
                    reachedEnd = false;
                }

            }

        });
    }

}

这篇关于如何禁用在Android的ListView的反弹时和反弹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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