手势监听onFling没有一致的代理 [英] Gesture Listener onFling Not Acting Consistant

查看:136
本文介绍了手势监听onFling没有一致的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:见扩大问题的赏金

我有一个的ListView A GestureDetector 设置。在ListView是来自窗口的侧面和部分覆盖另一个片段的整个片段。我想给接近其滑用户的能力(即是的Wunderlist上右侧该功能的一个很好的例子)。

I have a GestureDetector setup on a ListView. The ListView is an entire fragment that comes from side of window and overlays another fragment partially. I want to give user ability to swipe it close (i.e. Wunderlist is a great example of this function on right side).

下面是我的设置:

    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }

            return false;
        }
    };
    listView.setOnTouchListener(gestureListener); 

监听器本身:

public class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 180;
        private static final int SWIPE_THRESHOLD_VELOCITY = 50;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

            try {
                if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                    FragmentManager fma = getActivity()
                            .getSupportFragmentManager();



                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction t = fm.beginTransaction();

                    SherlockListFragment mFrag = new RateReviewFragment();

                    t.add(R.id.main_frag, mFrag);
                    t.setCustomAnimations(R.anim.animation_enter,
                            R.anim.animation_leave);
                    t.remove(mFrag);
                    t.commit();

                }
                return false;
            } catch (Exception e) {

            }
            return false;
        }





        @Override
        public void onLongPress(MotionEvent e) {

            // edited out; this opens up a context menu
        }

    }

有时,当的ListView 滚动到底部(或滚动列表与上一个列表行点击中断)的 GestureListener 只是停止...听着。刷卡将无法工作。你必须滚动返回页首让它重新工作。

Sometimes, when the ListView scrolls to the bottom (or list scrolling is interrupted with clicks on a list row) the GestureListener simply stops ... listening. Swiping will not work. You have to scroll back to top to get it to work again.

如果有人能帮助我隔离这些问题,我将不胜感激!

If anyone can help me isolate these issues I would be grateful!

更新:只有一个问题仍然是,听者停止侦听

更新两个:我可能已经想通了,是什么原因造成这一点;只是不知道该修补程序:

我已经找到一些东西记录我的行动之后;我得到的名单(和更多我与它进行交互),测量并不一致的低。例如,如果我把我的手指左,右一厘米 - 在列表的最顶端,它会说我感动,比如200像素。但是,当我有上述的问题,那就是低得多,也许50,为的同一个的1厘米距离。所以,窗口不会关闭,因为它不符合我的如果的条件。

I have found something out after logging my actions; The lower I get in the list (and the more I interact with it), the measurements are not consistent. For example, if I move my finger a centimeter left and right - at very top of list, it will say I moved, for example, 200 pixels. But when I have the problem stated above, it is far lower, maybe 50, for the SAME 1 centimeter distance. So the window doesn't close because it's not meeting my if conditions.

旁注的:我已经说了多次,问题是当与列表互动。这意味着:如果我迅速上下滚动;没有任何问题;但如果我慢慢地工作,我的一路下跌,也许点击屏幕上,滚动和关闭,点击,其中一些打开一个新的活动上的 ListView控件按钮(再过来回到同一位置),这是互动和

Sidenote: I have stated more than once, the problem is when "interacting with the list". This means: If I quickly scroll from top to bottom; no issue; but If I slowly work my way down, perhaps tapping on the screen, scrolling on and off, clicking buttons on the listView, some of which open a new activity (and then come back to same position), this is "interacting" with".

推荐答案

试试这个,

    gestureDetector = new GestureDetector(getActivity(),
            new GestureListener());
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
           listView.onTouchEvent(event);
           gestureDetector.onTouchEvent(event);

            return true;
        }
    };
    listView.setOnTouchListener(gestureListener);

和进行以下更改

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
  return true;
}

编辑:
我看到了应用WUNDERLIST,如果u要模拟该功能试用这种方法,

I saw the app wunderlist, if u want to simulate that functionality try out this approach,

使用onInterceptTouch根布局(其中的ListView是一个孩子),这个方法里面u必须检查whteher用户刷卡或不喜欢以下,

Use onInterceptTouch for the root layout (of which listView is a child of), inside this method u have to check whteher the user has swiped or not like the following,

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
  if (ev.getAction() == MotionEvent.ACTION_MOVE) {
    if(/* Check your condition here */) { 
      // User has swiped 
      return true;
    }
  }

  return false; // return false so that the event will be passed to child views.
}

有关更多信息的Andr​​oid onInterceptTouchEvent

这篇关于手势监听onFling没有一致的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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