Precautions for using listview in Android

Source: Internet
Author: User

If selector is set to null in listview, a null pointer is returned?
Mlistview. setselector (null); // NULL pointer
Try the following:
Mlistview. setselector (New colordrawable (color. Transparent ));

How can I select an item during listview initialization?
After the data is initialized, one of the items needs to be selected. The so-called "selected State" means that the background color is different from other items. setselection (position) can only locate an item, but cannot change the background color to highlight. Setselection (position) can only display an item at the top of the visible item (if the item exceeds one screen )! It's the so-called firstvisibleitem!
To achieve the effect, you can complete some specific work in the getview function of the adapter bound to the listview. You can write down the index of the item you want to highlight and judge the index (that is, position) in the getview function. If the condition is met, different backgrounds are loaded.

How does one enable the right-side Slider of listview?
Many developers do not know how to enable the quick scroll slider of the listview list control. In fact, the secondary scroll slider only needs one line of code, if you use XML layout, you only need to add the Android: fastscrollenabled = "true" attribute to the listview node. For Java code, you can use mylistview. setfastscrollenabled (true); to control enabling. The false parameter is hidden.
Another point is that when the scrolling content is small and the screen height of the current listview is less than the three screens, the quick scroll slider will not appear. This method is the basic method of the abslistview, you can use Quick scrolling assistance in listview, gridview, and other subclasses.

 

1. Update the data records in the listview by using the notifydatasetchanged () method of the baseadapter object:
Madapter. notifydatasetchanged ();

2. Each listview has an invalid position, such as the first row of the first row and the last row of the last row. This invalid position is a constant.
Listview. invalid_position

3. Sometimes we need to control the selection of the listview row by clicking Browse in the program, so we can use the code in the program to select the listview layout.
Mlistview. requestfocusfromtouch ();
Mlistview. setselection (INT index );

The first sentence is not required, but if your listview handler contains buttons, radiobutton, and checkbox controls that are higher than the top priority of listview, the first sentence must be added.

4. in the same way, if the listview listener contains buttons, radiobutton, and checkbox controls that are higher than the top priority of the Focus cursor obtained by the listview, The setonitemclicklistener of the listview will not be merged, at this time, you need to add Android: focusable = "false" to these controls in your xml file. Note that this sentence should be modified in the XML file, it is ineffective to use in the Token Generation.

5. How can I keep the listview's dynamic motion prompt without hiding it? In the XML file, make the following changes: Android: fadescrollbars = "false"

6. the listview itself has its own tag event, that is, you do not need to set the orientation of the tag. If you press the direction to upload the listview, there will be an activity for silence. How can we further control the event, define your own onkey. You need to repeat the dispatchkeyevent (keyevent event) in the activity. The method defines your own action here.

Listview custom scroll bar style:
<Listview Android: Id = "@ Android: ID/List"
Android: layout_width = "match_parent"
Android: layout_height = "0dip"
Android: layout_weight = "1"
Android: stackfrombottom = "true" // display entries from the bottom
Android: transcriptmode = "normal"
Android: fastscrollenabled = "true"
Android: focusable = "true"
Android: scrollbartrackvertical = "@ drawable/scrollbar_vertical_track"
Android: scrollgateumbvertical = "@ drawable/scrollbar_vertical_thumb"
/>
// Scrollbar_vertical_track, crollbar_vertical_thumb custom XML file, put in drawable, track refers to the bar, thumb refers to the short bar
When selecting listview selector, the yellow background will flash:

XML Code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Shape xmlns: Android = "http://schemas.android.com/apk/res/android">
  3. <Solid Android: color = "@ Android: color/transparent"/>
  4. <Corners Android: radius = "0dip"/>
  5. </Shape>
  6. // Listview. setselector (R. drawable. thisshape );

Or there is another way:
In the adapter, override the public Boolean isenabled (INT position) method and return it to false. This method is recommended. For details, see the http://gundumw100.iteye.com/admin/blogs/850654.

Java code
  1. Public Boolean isenabled (INT position ){
  2. // Todo auto-generated method stub
  3. Return false;
  4. }

Listview has several special attributes.
The first is the stackfrombottom attribute. After this attribute is set, the list you prepared will display the bottom of your list, with values true and false.
Android: stackfrombottom = "true"

The second is the transciptmode attribute. You need to use listview or other controls that display a large number of items to track or view information in real time, and you want the latest entries to automatically scroll to the visible range. By setting the transcriptmode attribute, You can automatically slide the Android platform control (scrollbar supported) to the bottom.
Android: transcriptmode = "alwaysscroll"

The third cachecolorhint attribute, many people want to change its background so that it can adapt to the overall uidesign. To change the background, it is easy to prepare an image and specify the attribute Android: background = "@ drawable/BG", but don't be too happy. When you do this, you will find that the background has changed, but when you drag, you can also click the blank position in the list to find that all listitems are black, damaging the overall effect.
If you only change the background color, you can directly specify the Android: cachecolorhint as the color you want. If you use an image as the background, you only need to change the Android: cachecolorhint is specified as transparent (#00000000 ).

The fourth divider attribute is used to set an image between each item as an interval or remove the split line between items. Android: divider = "@ drawable/list_driver" where @ drawable/list_driver is an image resource. If you do not want to display the split line, set it to Android: divider = "@ drawable/@ null ".

The fifth fadingedge attribute has a black shadow on the top and bottom. Android: fadingedge = "NONE" has no shadow ~

The fifth scrollbars attribute is used to hide the listview scroll bar. Android: scrollbars = "NONE" and setverticalscrollbarenabled (true); have the same effect, which is hidden when no activity occurs, hidden during activities

Sixth, the fadescrollbars attribute. When Android: fadescrollbars = "true" is used to configure the listview layout, setting this attribute to true can automatically hide and display the scroll bar.

How can I choose not to use gallery when dragging flinging?
Pay attention to the usage at this time.
Gallery. setcallbackduringfling (false)

How can I make listview automatically scroll?
Note the stackfrombottom and transcriptmode attributes. Similar to the low-end rolling of the market client.
<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 single rows of listview?

Java code
  1. Listview = (listview) findviewbyid (R. Id. the ID of listview in the configuration file );
  2. // Select all options to traverse the listview. Each option is equivalent to relativelayout in the layout configuration file.
  3. For (INT I = 0; I <listview. getchildcount (); I ++ ){
  4. View view = listview. getchildat (I );
  5. Checkbox cb = (checkbox) view. findviewbyid (R. Id. checkboxid );
  6. CB. setchecked (true );
  7. }

How can I change the font color of textview in listview to focus?
The font color of a selected item in listview is different from that of the original one. How do I set the font color? In the layout file, textcolor is used to set the color. Instead of setting only one color, different colors are set under different conditions. The following is an example:

XML Code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
  3. <Item Android: state_enabled = "false" Android: color = "@ color/orange"> </item>
  4. <Item Android: state_window_focused = "false" Android: color = "@ color/orange"> </item>
  5. <Item Android: state_pressed = "true" Android: color = "@ color/White"> </item>
  6. <Item Android: state_selected = "true" Android: color = "@ color/White"> </item>
  7. <Item Android: color = "@ color/orange"> </item>
  8. </Selector>
  9. Set it to white when the focus is obtained or selected, and orange in other cases.

How can I customize the split line between the lines of a listview?
All widgets Based on listview or abslistview can use the following method to set the line spacing, and the line can be customized.
In listview, we use the property Android: divider = "# ff0000" to define the separator as red. Of course, the value here can point to a drawable image object, if the image height is greater than the system's default pixel, you can set the height by yourself, for example, 6 pixels Android: dividerheight = "6px". Of course, you can set listview in Java.

Listview does not update the specified item through policydatasetchanged ()
In general, listview is updated through yydatasetchanged (), but the actual items on the interface will be re-painted Once Through yydatasetchanged (), which will affect the UI performance.
You can improve efficiency by updating the specified item. The pseudocode is as follows:

Java code
  1. Private void updateview (INT itemindex ){
  2. Int visibleposition = yourlistview. getfirstvisibleposition ();
  3. View v = yourlistview. getchildat (itemindex-visibleposition); // do something fancy with your listitem View
  4. Textview TV = (textview) v. findviewbyid (R. Id. sometextview );
  5. TV. settext ("Hi! I updated you manually ");
  6. }

The contextmenu can be displayed when you press certain items in the listview.
A listview is defined and the listener of setoncreatecontextmenulistener is set for it. However, in this way, only the contextmenu is displayed when all the items in the listview are long pressed.
Sometimes it is expected that the contextmenu can be displayed on time, and some cannot. To solve this problem, set the setonitemlongclicklistener listener for the listview and then implement

Java code
  1. Public Boolean onitemlongclick (adapterview <?> Parent, view,
  2. Int position, long ID ){
  3. If (ID = 1 ){
  4. Return true;
  5. }
  6. Return false;
  7. }

If the ID of this item is 1, you cannot press it. In this way, you can

Question about the separation line at the bottom of listview
A problem encountered in the work is that a listview has no split line under the bottom item, and the requirement is that there must be a split line. After a study, we found this strange phenomenon:
1. listactivity has a split line at the bottom.
2. There is only listview in the activity, and there will be no other controls.
In fact, listactivity is also an activity, but it uses the setcontentview (listview) method to set a listview as its display view. So the conclusion is that as long as this activity calls setcontentview (listview), there will be a split line at the bottom.
So under what circumstances will there be no split line? In the activity, if setcontentview (View) is called and listview is embedded in this view, this split line may not exist.
Analysis of the Cause: Through breakpoint debugging, it is found that in listview, all split lines are implemented by drawing a very narrow rectangle, however, the current position a and the length B of the listview will be determined before the split line is drawn. If a = B, this split line will not be drawn. However, if you embed a listview into a view, the height is generally set to wrap_content. In this case, the position of the split line is exactly the height of the listview, so the system will not draw this split line. So how can we draw on it? You can set the height of the listview to fill_partent.
Of course, if there are so many items that the scroll bar must be displayed, there will be no split lines under the last item. <! -- [Endif] --> <! -- [Endif] -->

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.