改变然后在Android平滑滚动行为 [英] Change Android smooth scroll behaviour on click

查看:171
本文介绍了改变然后在Android平滑滚动行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,并用定时我通过调用控制此列表的滚动运动 smoothScrollToPositionFromTop(位置,偏移,持续时间)。在的ListView 有一个监听 OnItemClickListener

I have a ListView and with a Timer I control the scroll movement of this list by calling smoothScrollToPositionFromTop(position, offset, duration). The ListView has a listener for OnItemClickListener.

如果同时平滑滚动的情况是点击一个项目,滚动停止,但 onItemClick 事件不会被触发。为了做到这一点,你需要在项目再次点击。

If an item is clicked while the smooth scroll is happening, the scroll stops but the onItemClick event is not triggered. In order to do so you need to click again on the item.

我想知道如何重写此行为。我的意思是,如果我在项目上点击而平滑滚动,从停止滚动发生,除了我要触发 onItemClick 该项目点击

I want to know how to override this behaviour. I mean, if I click on an item while the smooth scroll is happening, apart from stopping the scroll, I want to trigger onItemClick on the item clicked.

我真的不知道,如果有一个简单的方法来做到这一点。我试着用 GestureDetector OnTouchListener 名单,我听 onSingleTapConfirmed 为了调用 performClick 在那里,但我不知道如何获得该项目的位置是从<$ preSS C $ C> MotionEvent 。

I really don't know if there is an easy way to do this. I tried with a GestureDetector for the OnTouchListener of the list and I listen to the onSingleTapConfirmed in order to call performClick in there, but I don't know how to get the position of the item being press from the MotionEvent.

推荐答案

我终于找到了解决方案,通过使用 GestureDetector

I finally found a solution by using the GestureDetector:

final GestureDetector gestureDetector = new GestureDetector(MyActivity.this, 
  new GestureDetector.SimpleOnGestureListener(){
    public boolean onSingleTapConfirmed(MotionEvent e) {
        int position = listView.pointToPosition((int)e.getX(), (int)e.getY());
        if(position != ListView.INVALID_POSITION){
            listView.playSoundEffect(SoundEffectConstants.CLICK);
            //onItemClick code goes here
            //or call listView.performItemClick
            return true;
        }
        return false;
    };
});

listView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});

listView.setSelector(android.R.color.transparent);

在我的情况,我说我在 onItemClick 在做内部的 onSingleTapConfirmed 和删除 OnItemClickListener 。这就是为什么我还添加了 playSoundEffect 函数来模拟点击。

In my case, I added what I was doing in onItemClick inside onSingleTapConfirmed and removed the OnItemClickListener from the list. That's why I also added the playSoundEffect function to simulate the click.

在最后一行我在的ListView禁用亮点单击线以来得到了强调,只有当平滑滚动没有发生。通过禁用它,我获得每次点击的行为相同。

In the last line I disable highlight on ListView click since lines got highlighted only when the smooth scroll was not happening. By disabling it I get the same behaviour for every click.

这篇关于改变然后在Android平滑滚动行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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