Android Development Pulltorefresh Click to tap Event Listener implementation Long Press DELETE item

Source: Internet
Author: User

This is the original blog, from Http://blog.csdn.net/minimicall

To this day, the seller version of the search should have been basically completed, a tough hard to come all the way. The speed is also controlled relatively well.

Progress of the project process





From the task allocation amount, the basic or my individual heroism. That's not going to work. But temporarily there is no way, the younger brother also need a learning process. Good intelligence, and attitude is correct. Believe in the search for the buyer, he can assume more development tasks.


Next go to the point, say our Pulltorefresh click event. In fact, I want to do a long press into the delete.

See. Of course this is what I did after it, but it's not easy to make it.

First on



At this point, the user presses an item and goes to delete selected mode.


This time the menu on Actionbar changed and became a select all and delete.


We can also select all the item by clicking the actionbar all option


Of course, you can cancel the selection at this time, or you can cancel the item on a single item.

Select, and then press the Delete menu, which is the Trash can icon. will be deleted, deleted and synced to the server.

The problem is first. Found clicked invalid ...!

Troubleshoot problems

Many ways online.

The first more useful is: http://www.tuicool.com/articles/ria6Zf


[Java]View Plain Copy
    1. View.setfocusable (True), corresponding to xml:android:focusable= "true".
    2. View.setfocusableintouchmode (True), corresponding to Xml:android:focusableintouchmode= "true".

Note: These two properties are used at the same time.

both means that the component can get the focus. However, there are some differences, the former execution of the false condition, the execution of true, still cannot get the focus. The latter performs the above process, or can get the focus.
When you add the above code, when you create the activity, the Requestfocus () that corresponds to the view is called, (Requestfocus () needs to be executed after Setcontentview) so that the focus is available. When EditText loses focus, there's no soft keyboard.

But for the ListView you can also use the android:descendantfocusability property, Let's look at android: A brief analysis of descendantfocusability usage

Following excerpt from: http://www.cnblogs.com/eyu8874521/archive/2012/10/17/2727882.html

A common problem in development is that the ListView in the project is more than just a simple text, it often needs to define the ListView itself, adapter to inherit Baseadapter, and write in adapter as required. The problem arises and may occur when clicking on each item without reacting and not getting the focus. This is mostly due to the presence of child controls such as Imagebutton,button,checkbox in your own definition, such as the child control of a Button or checkable, in which the child controls get the focus. So often when clicking on item changes the child control, the item itself's click does not respond.

This time you can use descendantfocusability to solve, the API description is as follows:

Android:descendantfocusability

Defines the relationship between the ViewGroup and its descendants when looking-a View to take focus.

Must be one of the following constant values.

This property is defined as the relationship between ViewGroup and its child controls when one gets the focus for the view.

There are three types of values for the property:

Beforedescendants:viewgroup takes precedence over its subclass control and gets to focus

Afterdescendants:viewgroup gets focus only if its subclass control does not need to get focus

Blocksdescendants:viewgroup overrides the subclass control and gets the focus directly

Usually we use the third type, that is, the root layout of the item layout plus the android:descendantfocusability= "blocksdescendants" properties, so that the ListView click on the supernatural event is over. Experience: Encounter will not understand the place in addition to online query information, but also can be a lot to try the role of each attribute, read more official documents (I always think it is better to read the original than the translation of understanding)

The second more intentional is:

http://blog.csdn.net/kankankankan2222/article/details/7693190

If a view such as Checkbox,button is present in the view of a single item in the ListView, it will cause Listview.setonitemclicklistener to be invalid.

The event is captured by Quilt view, and the ListView cannot capture the event for processing.

Workaround:

Add android:focusable= "False" to the view where checkbox and button correspond
Android:clickable= "false" Android:focusableintouchmode= "false"

Where focusable is the key

from onclicklistener call getselecteditemposition () CLICK  SELECTION  selection d-pad or trackball  click

The mistake I made was that I accidentally set the monitor for click in the GetView. It's been commented out by me.

    Private class Clicklistner implements Onclicklistener {//        private int position;////        private Clicklistner (int position) {//            this.position = position;//        }////        @Override//Public        void OnClick ( View v) {//        log.d (TAG, "xxxxxxxxxxx");//            MCloth cloth = mcloths.get (position);//            if (!isenabled ( Position) {//                return;//            }//            Context context = V.getcontext ();//            //detailactivity.launch ( Context, movie, referer.new_arrivial);//        }//    }
Next I'll talk about how to implement Pulltorefresh to implement long press ENTER to delete selected mode.

Long Press to enter delete mode

First, we open a function where we need to initialize the view's various listeners. As below, I believe everyone is familiar with it,

private void Setupviews (View rootview) {Mlistview = (Pulltorefreshlistview) Rootview.findviewbyid (R.ID.CLOTHS_LV); Mlistview.setmode (PullToRefreshBase.Mode.BOTH);((ViewGroup) mlistview.getparent ()). AddView (Merrorview); Mlistview.setemptyview (merrorview); mclothmanageadapter = new Clothmanageadapter (getactivity (), GetImageFetcher ()); Mlistview.setadapter (mclothmanageadapter);((mainactivity) getactivity ()). Getindicator (). Setontabselectedlistener (mtabselectdlistener); Mprogressbar = (ProgressBar) Rootview.findviewbyid (R.id.pb_progress ); Setlistviewrefresh (); Setlongclicklistener ();}

We focus on the Setlongclicklistener, this is a private method I sealed, as follows:

private void Setlongclicklistener () {LOG.D (TAG, "Setlongclick"); Mlistview.getrefreshableview (). Setonitemclicklistener (Monitemclicklistener);//Click the event's Listener Mlistview.getrefreshableview (). Setchoicemode ( Listview.choice_mode_multiple_modal)///Select mode is selectable Mlistview.getrefreshableview (). Setmultichoicemodelistener ( Mmutilchoicelistener);//multi-Select Listener}
OK, let's take a closer look. Click Time to listen

Private final Onitemclicklistener Monitemclicklistener = new Onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> Parent, view view, int Position,long ID) {if (Mactionmode = = null) {////have not pressed long press, go here. LOG.D (TAG, "Onitemclick,mactionmode is null");} else {//already has a long press, enter the selection mode, go here log.d (TAG, "Onitemclick,mactionmode is not  null, select item" + Position + "to select") ; Mlistview.getrefreshableview (). setitemchecked (position, True);}};
Well, we're looking at our very critical multi-select listener. Directly on the code

Private final Multichoicemodelistener Mmutilchoicelistener = new Multichoicemodelistener () {@Overridepublic Boolean Oncreateactionmode (actionmode Mode, menu menu) {log.d (TAG, "Oncreateactionmode, Mode:" + Mode + ", Menu:" + menu); Mactionmo de = Mode;getactivity (). Getmenuinflater (). Inflate (R.menu.menu_favorite_delete, menu);//Load menu to Actionbar return true;} @Overridepublic boolean Onprepareactionmode (Actionmode mode, menu menu) {return false;} @Overridepublic void Ondestroyactionmode (Actionmode mode) {Mactionmode = null;} @Overridepublic boolean onactionitemclicked (actionmode mode, MenuItem Item) {switch (Item.getitemid ()) {//Menu click event Case R.id.menu_delete://delete log.d (TAG, "delete Menu"); list<mcloth> deleted = new arraylist<mcloth> (); Sparsebooleanarray checked = Mlistview.getrefreshableview (). Getcheckeditempositions (); for (int i = 0; i < Checked.size (); i++) {log.d (TAG, "Get from Adapter (" +checked.keyat (i) + "," +checked.valueat (i) + ")"), if (Checked.valueat (i)) { Deleted.add (MclothmanageaDapter.getitem (Checked.keyat (i)));}} for (MCloth cloth:deleted) {//if (cloth! = null) {////LOG.D (TAG, "Deleting the cloth id=" +cloth.id+ "in remote server No W ");//Webinterface1.delcloth (cloth.id);//}//}new deleteclothtask (). Executeonexecutor (Asynctask.thread_pool_ EXECUTOR, deleted); Mclothmanageadapter.remove (deleted); Mode.finish (); LoadData (true); Break;case r.id.menu_ SelectAll://Select All if (Misselectall) {item.settitle ("deselect All"); misselectall = false;} else {item.settitle ("Select All"); Misselectall = true;} for (int i = 0; i < Mlistview.getrefreshableview (). GetCount (); i++) {Mlistview.getrefreshableview (). setitemchecked (i ,!misselectall);} break;} return true;} @Overridepublic void onitemcheckedstatechanged (actionmode mode, int position,long ID, Boolean checked) { Setactionmodetitle (Mode, Mlistview.getrefreshableview (). Getcheckeditemcount ());}; private void Setactionmodetitle (actionmode mode, int count) {log.d (TAG, "Setactionmodetitile,mode:" + mode + ", Count:" + C Ount); Mactionmode.settitle ("Checked" + CounT + "a cloth");} 
Such
The core code is above, the implementation of long press DELETE, the effect is good. If you don't know, you can contact me. Hope that our search products can grow into a tree day by day.good night. If you want to join our team, you can also contact me. I will give you the help you can, but also look forward to everyone's happy cooperation.










Android Development Pulltorefresh Click to tap Event Listener implementation Long Press DELETE item

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.