如何通知我的通知

时间:2014-09-25 12:10:13

标签: java android notifications gesture

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

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

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码,您可以将滑动手势应用于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;

        }

    }