How to handle ListView Item LongPress gesture

瘦欲@ 提交于 2020-01-23 11:52:53

问题


i have small issues in event handling , i have List view custom adapter data , each row having date , title , price . when i click on row i need to display details page but when i long press on price , date or title i need to sort the list view. i need to use gesture for on Long Press. please refer below code what i have tried.

Custom Adapter View

public EventAdapterView(Context context, List<EventUtil> eventList) {

        this.mContext = context;
        this.mEventUtil = eventList;
        mLayoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new DrawableManager();

    }

    @SuppressLint("DefaultLocale")
    @SuppressWarnings("deprecation")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        mView = convertView;
        EventUtil eventUtil = mEventUtil.get(position);

        mView = mLayoutInflater.inflate(R.layout.row_event_adapter, null);

        TextView eventTitleView = (TextView) mView
                .findViewById(R.id.list_view_event_title);
        TextView eventDescView = (TextView) mView
                .findViewById(R.id.list_view_event_location);
        TextView eventDateView = (TextView) mView
                .findViewById(R.id.list_view_event_price);

        // final MyGestureDetector myGestureDetector= new MyGestureDetector();

        // new ImageFeach().execute(mEventUtil.getEvent_Image_Url());

        eventTitleView.setText(eventUtil.getEvent_Title());

        // event title sorting

        eventTitleView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                new MyGestureDetector() {

                    public void onLongPress(MotionEvent event1) {

                        //if (event1.getAction() == MotionEvent.ACTION_DOWN) {
                            Collections.sort(mEventUtil,
                                    new Comparator<EventUtil>() {

                                        @Override
                                        public int compare(EventUtil obje1,
                                                EventUtil obje2) {

                                            return obje1
                                                    .getEvent_Title()
                                                    .compareTo(
                                                            obje2.getEvent_Title());
                                        }

                                    });

                            notifyDataSetChanged();
                        //}

                    };

                }.onLongPress(event);

                return true;
            }
        });

        // event location sorting
        eventDescView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                new MyGestureDetector() {

                    public void onLongPress(MotionEvent event1) {

                        //if (event1.getAction() == MotionEvent.ACTION_DOWN) {
                            Collections.sort(mEventUtil,
                                    new Comparator<EventUtil>() {

                                        @Override
                                        public int compare(EventUtil event1,
                                                EventUtil event2) {

                                            return event1
                                                    .getEvent_location()
                                                    .compareTo(
                                                            event2.getEvent_location());
                                        }

                                    });
                            notifyDataSetChanged();
                        //}

                    };

                }.onLongPress(event);

                return true;
            }
        });

        // event price sorting
        eventDateView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                new MyGestureDetector() {

                    public void onLongPress(MotionEvent event1) {

                    //  if (event1.getAction() == MotionEvent.ACTION_DOWN) {
                            Collections.sort(mEventUtil,
                                    new Comparator<EventUtil>() {

                                        @Override
                                        public int compare(EventUtil event1,
                                                EventUtil event2) {

                                            return event1
                                                    .getEvent_Price()
                                                    .compareTo(
                                                            event2.getEvent_Price());
                                        }

                                    });
                            notifyDataSetChanged();
                        //}

                    };

                }.onLongPress(event);

                return true;
            }
        });

        // event date sorting
        ImageView dateImageView = (ImageView) mView
                .findViewById(R.id.list_view_event_date);

        dateImageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                new MyGestureDetector() {

                    public void onLongPress(MotionEvent event1) {

                        //if (event1.getAction() == MotionEvent.ACTION_DOWN) {
                            Collections.sort(mEventUtil,
                                    new Comparator<EventUtil>() {

                                        @Override
                                        public int compare(EventUtil event1,
                                                EventUtil event2) {

                                            return event2
                                                    .getEvent_Date()
                                                    .compareTo(
                                                            event1.getEvent_Date());
                                        }
                                    });
                            notifyDataSetChanged();
                        //}

                    };

                }.onLongPress(event);


                return true;
            }
        });

MyGestureDetector Class

public class MyGestureDetector extends SimpleOnGestureListener {

    @Override
    public void onLongPress(MotionEvent e) {

        super.onLongPress(e);
    }



}

Help Me.. Thanks


回答1:


I think, there's no need to use SimpleOnGestureListener.

In your Custom Adapter View:

First,

eventTitleView.setFocusable(false); 
eventTitleView.setFocusableInTouchMode(false);
eventTitleView.setLongClickable(true);

eventDescView.setFocusable(false); 
eventDescView.setFocusableInTouchMode(false);
eventDescView.setLongClickable(true);

eventDateView.setFocusable(false); 
eventDateView.setFocusableInTouchMode(false);
eventDateView.setLongClickable(true);

Then, just set setOnLongClickListener (View.OnLongClickListener l) on eventTitleView, eventDescView and eventDateView as,

eventTitleView.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //do your sorting stuff here
        }
    });

eventDescView.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //do your sorting stuff here
        }
    });

eventDateView.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //do your sorting stuff here
        }
    });

Hope this helps you.




回答2:


Why do you need to override onlongpress?

You know this function is available by default right?

just put the price , date or title on the same Layout and give each one OnLongPressListener with the action you want..:

EXAMPLE: http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/




回答3:


I have done this thro onLongClickListener. It's easy using this interface . You need to do

Class YourClass implements onLongClickListener{

 public boolean onLongClick(View arg0){                                 

 // Event generated when user have long pressed the screen

  return false; // If you do not want the event to keep occuring again and again

} 



回答4:


 ListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // YOUR SORTING CODE HERE

                return false;
            }
        });


来源:https://stackoverflow.com/questions/20042572/how-to-handle-listview-item-longpress-gesture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!