Resignation 3, 4 months at home rest, this thought building main programmer reverse attack, the result failed to continue code farming career today began to update blog.
Body. There is a ListView content in the project more complex now to add long press DELETE feature. The landlord naturally thought of using the Onitemlongclick event to deal with the ListView. The results can be imagined in the actual experience is very bad, there will be failure some options to trigger Onitemlongclick events some but did not respond. The landlord to go online to see is focusable to set to false. But in accordance with the landlord experience this can only be solved can not trigger Onitemlongclick event problems, such as the landlord encountered some can be some failure of the estimate is not a dose of medicine. Therefore, the landlord concrete analysis of the internal details, hoping to provide some help to later people. First look at the foucsable caused by the incident did not respond to the problem.
Reason:If you have a button or a checkbox in your item, the focus is given to these child controls by default, and the ListView item can be selected based on its ability to get focus, so We can set the Focusable property of all controls contained in the item in the ListView to False so that the ListView item automatically gets the focus's permission, and it can be selected. It also responds to the Onitemclick () method in Onitemclicklistener.
Workaround:(The following two ways can be any one) 1. Set all child controls in the ListView's item Layout focusable property to False2. Set the property of the root control for item layout
(recommended lightweight especially to modify someone else's code)android:descendantfocusability= "Blocksdescendant" so that item layout blocks all child controls from gaining focus You do not need to reset the Focusable property analysis for each control in ItemLayout: Why does not set focusable to false will not trigger the Onitemlongclick event. By looking at the source code of the Abslistview, you will find that if focusable is not false, the touch event will not be distributed. The code is as follows:
A brief analysis of the use of android:descendantfocusability in the development of a very common problem, the ListView in the project is not only simple text, often need to define their own ListView, their own adapter to inherit Baseadapter , as required in adapter, the problem arises and there may be no response at the time of clicking on each item, and the focus cannot be obtained. 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:
This property is defined as the relationship between ViewGroup and its child controls when one gets the focus for the view. There are three values for the property: Beforedescendants:viewgroup takes precedence over its subclass control and gets to focus Afterdescendants:viewgroup only gets focus if its subclass control does not need to get focus blocks Descendants:viewgroup overrides the subclass control and gets the focus directly. Usually we use the third type, which is the root layout of the item layout plus android:descendantfocusability= "blocksdescendants "The properties of the actual situation according to the above set the landlord's problem is still unresolved, the landlord firmly believe that the above article authoritative, again analysis of their own code. Found in adapter Convertview is set clickable to true (another colleague in order to block item's yellow selector setting of the landlord will introduce modified ListView click Yellow Background Modification) This will inevitably cause the touch event to be intercepted by Convertview, since Convertview is the root container for item, so it is not possible to click into the ListView to get the ListView to capture the touch event. Remove the item and container's clickable settings. Sure enough to trigger the Onitemlongclick event. Failure to solve the problem found in the content of a few simple items is not a problem, but in the complex item when the child view of the item is unavoidable to set the Click event, and this view is the most part of the item, You can't trigger a Onitemlongclick event by pressing and long pressing the item's small spare place every time. Landlord here to give a relatively lightweight solution. The code snippet is as follows://adapter Internal code://hoLder.audiopanel This view occupies a large place but must implement the Click event, fortunately it has Longclick eventsHolder.audioPanel.setOnClickListener (this);Holder.audioPanel.setOnLongClickListener (this);//Key here manually triggering the ListView's long-press method naturally comes back to normal logic. @Overridepublic boolean Onlongclick (View v) {Mlistview.performlongclick (); return false;} This perfectly solves the triggering Onitemlongclick event regardless of how complex the ListView interface is. How to remove the default yellow background for the ListView. The landlord is such a solution:
Appendix the landlord on the Internet to see some small details: 1. Could the ListView itself call Setonclicklistner ()? code is possible, but running immediately throws an exception, so it is not possible to intercept the Click event of the ListView itself. When will the 2.listview.setonitemclicklistener set listener be called? When clicked on a line of content is called, but if this line contains Button,imgbutton and other controls, it will not be called, why and how to resolve see later. When is the listener of 3.listview.setonitemlongclicklistener set called? is called when a row is long pressed, and has been called before it is lifted. 4. Will the click be called after receiving the Longclick call? This depends on the return value of the Longclick listener. Java Code 1.lv.setonitemlongclicklistener (new Onitemlongclicklistener () { 2. publicbooleanonitemlongclick (adapterview<?>parent, view view, int position, long ID) { 3. system.out.println ("Item longclicked. Position: "+ Position); 4. return false; 5. } 6. }); If the return is False then click will still be called. and call Longclick first, and then call click. If you return TRUE then click will be eaten, click will not be called again. 5. Listener Click and long click Affect pop-up menu? click does not affect; Longclick if it returns true then it eats the click event, causing the menu not to pop up. Appendix: Android Using the ListView should be aware of the place in the ListView setting selector to NULL to report null pointers? mlistview.setselector (null);//null pointer Try this: mlistview.setselector (newcolordrawable ( color.transparent); How do I select an item when the ListView is initialized? listview needs to be selected when the data is initialized. The so-called "selected state" is that the color is different from other items, setselection (position) can only navigate to an item, but can not change the background is highlighted. SetSelection (position) can only have an item displayed at the top of the visible item (if item exceeds one screen)! is the so-called Firstvisibleitem! If you want to achieve the effect, you can do some specific work in the GetView function in the adapter that the ListView binds to. You can write down the index of the item you want to highlight, and in the GetView function, Judge Index (that is, position) and load a different background if the condition is met. listview How to enable the right-hand scroll slider? Many developers do not know how the quick scroll slider for the ListView list control is enabled, in fact, the auxiliary scroll slider only needs a single line of code to do, if you use XML layout only need to add in the ListView node The android:fastscrollenabled= "true" property is available, while for Java code it can be controlled by mylistview.setfastscrollenabled (true), and the parameter false is hidden. Another point is that when you scroll less than the current ListView 3 screen height, this quick scroll slider will not appear, the method is the basic method of Abslistview, you can LISTVIFast scrolling assist is used in subclasses such as EW or GridView. 1. Updating the figures in the ListView by using the Notifydatasetchanged () method of the Baseadapter image: Madapter.notifydatasetchanged (); 2. Each listview has an inefficient position, such as the first line in the previous row, the last line in the last row, and this inefficient position is a constant . listview.invalid_position 3. Sometimes we need to use the Click button in the program to control the selection of the ListView row, which uses the code in the program to select the ListView . mlistview.requestfocusfromtouch (); Mlistview.setselection (intindex); The first sentence is not required, but if you have Button,radiobutton in the ListView, When a checkbox is better than a ListView control, then the first sentence is the same as the . 4. you must add, if you have Button,radiobutton in the ListView, When a checkbox is better than a ListView control, the ListView Setonitemclicklistener is not executed, and you need to add android to these controls in your XML file: Focusable= "false" note that this sentence is to be modified in the XML file and is not valid for use in the code. 5. How to keep the scroll bars of the ListView always showing, not hidden: The xml file is modified as follows android:fadescrollbars= "false" 6. The ListView itself has its own key event, that is, you do not need to set the orientation key, and the ListView will have an implicit motion, how to control it, and write your own onKey, you need to re-write Dispatchkeyevent (keyeventevent) in the activity, and define your own motion here. listview Custom ScrollBar Style: < Listviewandroid:id= "@android: Id/list" android:layout_width= "Match_parent" android:layout_height= "0dip" android:layout_weight= "1" android:stackfrombottom= "true"//show entries from the beginning android: Transcriptmode= "Normal" android:fastscrollenabled= "true" android:focusable= "true" android:scrollbartrackvertical= "@drawable/ Scrollbar_vertical_track " android:scrollbarthumbvertical=" @drawable/scrollbar_ Vertical_thumb " /> //scrollbar_vertical_track,crollbar_vertical_thumb The custom XML file, placed in drawable, Track refers to the strip, thumb refers to the short strip Remove the ListView selector selection when the yellow shading Effect: xml code <?xml version= "1.0" Encoding= "Utf-8"?> <shapexmlns:android= "Http://schemas.android.com/apk/res/android" > < Solidandroid:color= "@android: Color/transparent"/> <cornersandroid:radius= "0dip"/> </shape> //listview.setselector (r.drawable.thisshape); or another way: Rewrite the public boolean isenabled (intposition) method in adapter, return it false, and recommend this approach, see http://gundumw100.iteye.com/admin/ Blogs/850654 java Code public boolean isenabled (int position) { //TODO Auto-generatedmethod stub return false; } listview several more special properties The first is the Stackfrombottom property, which is only after the list that you do well displays the bottom of your list, with a value of true and false android:stackfrombottom= "true" The second is the Transciptmode attribute, which requires real-time tracking or viewing of information in a ListView or other control that displays a large number of items And you want the latest entries to automatically scroll to the viewable range. By setting the Control Transcriptmode property, you can automatically slide the controls on the Android platform (support ScrollBar) to the bottom. android:transcriptmode= "Alwaysscroll" Third Cachecolorhint attribute, many people want to be able to change its background, so that he can conform to the overall UI design, change the background is very simple only need to prepare a picture and then specify the property android:background= "@drawable/BG "But don't be happy too early, when you do this, you find that the background is changed, but when you drag, or click on the list blank position to find that the ListItem has become black, destroying the overall effect. If you just change the color of the background, you can specify Android:cachecolorhint as the color you want, if you are using a picture to do the background, then just specify the android:cachecolorhint as transparent (# 00000000) on the fourth divider property, the function is to set a picture between each item as an interval, or to remove the split line between item android:divider= "@drawable/list_ Driver " where @drawable/list_driver is a picture resource, if you do not want to display the split line as long as it is set to android:divider=" @drawable/@null "is OK Fifth Fadingedge property, top and bottom have black shadow android:fadingedge= "none" Setting no shadow ~ fifth ScrollBars property, function is to hide ListView scroll bar , android:scrollbars= "None" with setverticalscrollbarenabled (true); the effect is the same, not active when hidden, when the activity is also hidden Sixth Fadescrollbars property, android:fadescrollbars= "True" when configuring the ListView layout, setting this property to True enables the auto-hide and display of scroll bars. How do I use gallery to drag in flinging without a selection? This time need to be aware of using gallery.setcallbackduringfling (false) How to let the ListView automatically scroll? Note Both the Stackfrombottom and Transcriptmode properties. Similar to the market clientThe low end keeps rolling. <listview android:id= "LISTCWJ" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:stackfrombottom= "true" android:transcriptmode= "Alwaysscroll" /> how do I traverse the Radio box of a ListView? java Code listview ListView = (ListView) Findviewbyid (r.id. config file in the ListView ID); // Select the option to iterate through the ListView, each option equivalent to relativelayout for in the layout profile (int i = 0; i < Listview.getchildcount (); i++) { Viewview = Listview.getchildat (i); checkbox cb = (CheckBox) View.findviewbyid (R.ID.CHECKBOXID); Cb.setchecked (TRUE); &NBSP: How do i make the font color of TextView in the ListView follow the change in focus? We usually need to select one of the items in the ListView, and his font color is different from the original one. How do I set the color of a font? TextColor an item in the layout file to set the color, but instead of just setting a color, you can set different colors under different conditions: Here's an example: xml code <?xml version= "1.0" encoding= "Utf-8"?> <selectorxmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:state_enabled= "false" android:color= "@ Color/orange "></item> <item android:state_window_focused=" false "android:color=" @color/orange " ></item> <item android:state_pressed= "true" android:color= "@color/white" ></item> <item android:state_selected= "true" android:color= "@color/white" ></item> <itemandroid:color= "@color/orange" ></item> </selector> is set to white when the focus is taken or selected, and other settings are orange. How do I customize split lines between the rows of a ListView? All widget controls that are based on the ListView or Abslistview implementation can set the line spacing split line by using the following method, and the split line can customize the color, or picture. In the ListView we use the attribute android:divider= "#FF0000" to define the delimiter to be red, of course here the value can point to a drawable picture object, if the use of the picture may be higher than the system default pixels, You can set your own height such as 6 pixels android:dividerheight= "6px", of course, in Java, the ListView also has the relevant method can be set. listview the specified Item listview not through notifydatasetchanged () is typically updated by notifydatasetchanged () to the ListView, But by notifydatasetchanged () the actual item on the interface is redrawn once, which affects UI performance. can be updated by updating the specified ITEM increase efficiency, pseudocode as follows: java code private void Updateview (int itemIndex) { int Visibleposition =yourlistview.getfirstvisibleposition (); View v = yourlistview.getchildat (itemindex-visibleposition),//do something fancy with your ListItem V iew TextView TV = (TextView) V.findviewbyid (R.id.sometextview); tv.settext ("hi! I updated you manually "); }
When a checkbox, WebView, and other high-priority controls are embedded in the ListView item, the Onitemclick event of the ListView is masked as unresponsive ...
At this point we can fix the problem by setting the focuse of the control in item to False.
Use static method: Add Android:focusable= "false" to the layout file
Using dynamic methods: Calling Xxxview.setfocusable (false) in code
Cause: In Android controls, interactive events are propagated from top to bottom along the control tree . When the parent control on the upper level of the control receives an interaction event, the target control object of the event is judged, and if the event is exactly what it needs, the event is intercepted for processing, otherwise an attempt is made to distribute the event down to the corresponding child control and recursively propagate the event downward until the event is processed or slightly
Transferred from: http://blog.sina.com.cn/s/blog_783ede030101bnm4.html
http://my.oschina.net/u/249000/blog/109616
Android ListView Onitemlongclick and Onitemclick event internal details sharing