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

Source: Internet
Author: User

This article is an original blog. Originated from Http://blog.csdn.net/minimicall

Until today, the seller version number of the search should be basically complete. It's 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 there is no way to do it temporarily. The younger brother also needs a learning process. Good quality. And the attitude is correct. He is confident that he will be able to take on many other development tasks.


Then get 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 after I made it. But not easy to make.

First on

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvtwluau1py2fsba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


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


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


We are also able to select all item by clicking the actionbar all option


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

Selected. Then press the Delete menu. That's the Trash Can icon.

will be deleted. Delete Sync to server.

The problem is first. Found the click is invalid.

。!

Troubleshoot problems

Online very many methods.

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


Make a note of your mistakes. When writing a ListView Click event, the Onitemclicklistener,onitemclick method does not run, causing the ListView entry to fail, checking that there are many different answers on Baidu. But the essence of this is that the ListView item grabs focus or the item does not get focus or even has no binding on the Onitemclicklistener listener event. The mistake I made was to introduce a style in the ListView item layout, with a <item name= "android:clickable" >true</item> in the style. It is this one that causes all items to occupy focus, so the Click event of the ListView is invalidated. After I remove this item, the ListView does work properly.

What needs to be warning is. In Androidl application development, things that focus is not acquired or that other components preempt the focus often occur. We were able to preempt the focus in the code, in the XML layout, or even in the style, and in normal cases this setting would not cause any exceptions, but I need to be aware that important components have to be focused at the right time or have unintended consequences. My ListView, for example.

The general component gets the focus to use the method:

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

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

Both means that the component can get the focus.

Just a few differences, the former runs the false condition, and after running true, the focus is still not available.

The latter will still be able to gain focus by running the above process.
When you add the above code, the Requestfocus () of the corresponding view is called when the activity is created, and (Requestfocus () needs to be run after Setcontentview) to get the focus. When EditText loses focus, there's no soft keyboard.

But to use the android:descendantfocusability property for the ListView, Let's look at android: A brief analysis of the use method of Descendantfocusability

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 not simply text. Custom ListView is often required. Their adapter to inherit Baseadapter, in adapter in accordance with the requirements of the writing, the problem arises, may occur when clicking on each item does not respond. Unable to get the focus. This is mostly because of the existence of such Imagebutton,button in your custom item. A checkbox, such as a child control (which can also be said to be a button or checkable control), at which point the child controls get the focus. So often when clicking on item changes the child control, the item itself is clicked without responding.

You can use descendantfocusability to solve this. API descriptive narratives such as the following:

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 only gets focus when 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. At this point the ListView clicked on the supernatural event. Experience: Encounter will not understand the place in addition to online query information, but also can be a lot to try each of the properties of the role. Read more official documents (I always thought it would be better to read the original text than to interpret it)

The second comparison is intentional:

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

Assume a checkbox exists in the view of a single item in the ListView. button, such as view, can 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 corresponding view of the checkbox, button
Android:clickable= "false" Android:focusableintouchmode= "false"

Among them focusable is the key

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

The mistake I made was that I accidentally set the monitor for click in the GetView. I've been staring down.

    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. How to implement Pulltorefresh to achieve long press ENTER delete Check mode.

Long Press to enter delete mode

First, we open a function where we need to initialize the view of the various monitors. For example, I believe everyone is familiar with this,

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 that I sealed, for example, the following:

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) {///not yet pressed long. Go here. LOG.D (TAG, "Onitemclick,mactionmode is null");} else {//Once a long press has been made, the selection mode is entered. Go here log.d (TAG, "Onitemclick,mactionmode is not null, select item" + Position + "to select"); Mlistview.getrefreshablev Iew (). setitemchecked (position, True);}};

Well, let's look 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 the top. Realize the long press DELETE, the effect is good.

Do not understand can contact me.

Hope that our search products can grow into a tree day by day. good night.

If you want to increase our team, you can also contact me.

I will give you the help you can, but also look forward to everyone's happy cooperation.










The click-click event of the Pulltorefresh for Android development is monitored for long press to delete item

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.