【问题标题】:Change Android smooth scroll behaviour on click单击时更改 Android 平滑滚动行为
【发布时间】:2012-10-09 15:02:22
【问题描述】:

我有一个ListView 和一个Timer,我通过调用smoothScrollToPositionFromTop(position, offset, duration) 来控制这个列表的滚动移动。 ListView 有一个OnItemClickListener 的侦听器。

如果在平滑滚动发生时单击项目,则滚动停止但不会触发onItemClick 事件。为此,您需要再次单击该项目。

我想知道如何覆盖这种行为。我的意思是,如果我在平滑滚动发生时点击了某个项目,除了停止滚动之外,我想在点击的项目上触发onItemClick。 p>

我真的不知道是否有一种简单的方法可以做到这一点。我尝试使用GestureDetector 作为列表中的OnTouchListener,并收听onSingleTapConfirmed 以便在其中调用performClick,但我不知道如何获取正在按下的项目的位置MotionEvent。

【问题讨论】:

    标签: android android-listview onitemclicklistener smooth-scrolling


    【解决方案1】:

    我终于通过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 函数来模拟点击。

    在最后一行中,我禁用了 ListView click 的高亮显示,因为只有在没有平滑滚动时才会高亮显示行。通过禁用它,我每次点击都会得到相同的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-16
      • 2014-05-14
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多