【问题标题】:How get gestures on my notification如何在我的通知上获取手势
【发布时间】:2014-09-25 12:10:13
【问题描述】:

我创建带有通知的 android 应用程序。

是否存在任何方式以编程方式确定我的通知上的用户手势(水平和垂直滑动)?

【问题讨论】:

    标签: java android notifications gesture


    【解决方案1】:

    尝试使用以下代码,您可以将滑动手势应用于 ListView。

      public class NotificationsFragment extends Fragment {
    
            ListView mListView;
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                mContext = getActivity();
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                // mOnTouchListener = new MyTouchListener();
                view = inflater.inflate(R.layout.fragment_notification, container, false);
                mListView = (ListView) view.findViewById(R.id.notification_list);
                notificationtypeList = new ArrayList<String>();
                mListView.setOnTouchListener(new OnSwipeTouchListener(mContext));
                return view;
            }
    
    
            /**
             * Detects left and right swipes across a view.
             */
            public class OnSwipeTouchListener implements OnTouchListener {
    
                private final GestureDetector gestureDetector;
    
                public OnSwipeTouchListener(Context context) {
                    gestureDetector = new GestureDetector(context, new GestureListener());
                }
    
                public void onSwipeLeft() {
                }
    
                public void onSwipeRight() {
                }
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return gestureDetector.onTouchEvent(event);
                }
    
                private final class GestureListener extends SimpleOnGestureListener {
    
                    private static final int SWIPE_DISTANCE_THRESHOLD = 100;
                    private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    
                    @Override
                    public boolean onDown(MotionEvent e) {
                        return true;
                    }
    
                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        float distanceX = e2.getX() - e1.getX();
                        float distanceY = e2.getY() - e1.getY();
                        int id = mListView.pointToPosition((int) e1.getX(), (int) e1.getY());
                        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD
                                && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (distanceX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                            return true;
                        }
                        return false;
                    }
    
    
                return listviewElementsheight;
    
            }
    
        }
    

    【讨论】:

    • 在通知栏(面板)中
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多