How to use the ListView using the Android

Source: Internet
Author: User

Setting selector to NULL in the ListView will report a null pointer?
Mlistview.setselector (null);//null pointer
Try the following:
Mlistview.setselector (New colordrawable (color.transparent));

How do I select an item when the ListView is initialized?
The ListView requires that one of the items be selected after the good data has been 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 one item displayed on 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.

Right scroll slider on the ListView How to enable?
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 android:fastscrollenabled= to the ListView node " True "This property is available, and for Java code can be passed mylistview.setfastscrollenabled (true); To control enable, the parameter false is hidden.
Another thing is that when your scrolling content is small, it does not appear on the 3 screen height of the current ListView, which is the basic method of Abslistview and can be used for fast scrolling assistance in subclasses such as ListView or GridView.



1. Update the numbers 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 row in the line, 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 item.
Mlistview.requestfocusfromtouch ();
mlistview.setselection (int index);

The first sentence is not required, but if you have a ListView that contains Button,radiobutton,checkbox and other controls that have a higher priority than the ListView, then the first sentence is what you must add.

4. In the same way, if your ListView contains Button,radiobutton,checkbox and other controls that have a higher priority than the ListView, the Setonitemclicklistener of the ListView is not executed. At this point you need to add android:focusable= "false" to these controls in your XML file. Note that this language is modified in an XML file and is not valid for use in the code.

5. How to keep the scroll bars of the ListView always displayed, 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 direction key of the consciousness, press the arrow key The ListView will have the action, how to control, to write their own onkey, you need to re-write in the activity dispatchkeyevent ( KeyEvent event); method, define your own motion in this area.

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"//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 a custom XML file, placed in drawable, track refers to a strip, thumb is a short bar
Remove the yellow shading effect of the ListView selector selection:

  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's another way:
Rewrite the public boolean isenabled (int position) method in adapter, return it false, and recommend this approach, see http://gundumw100.iteye.com/admin/ blogs/850654

    1. Public boolean isenabled (int position) {
    2. //TODO auto-generated method stub
    3. return false;
    4. }



Several more special properties of the ListView
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 property, which needs to be tracked or viewed in real time with 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"

The 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 do not 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 when the ListItem has become black, destroying the overall effect.
If you just change the color of the background, you can directly specify Android:cachecolorhint as the color you want, if you are using a picture to do the background, it will be as long as the android:cachecolorhint is specified as transparent (#00000000) can be

The Divider property, the function of which is to set a picture between each item as an interval, or to remove the split line between items android:divider= "@drawable/list_driver" where @drawable/list_ Driver is a picture resource, if you do not want to display the split line, just set it to android:divider= "@drawable/@null".

Fifth Fadingedge property, there is a black shadow on top and bottom android:fadingedge= no shadow after setting "None" ~

The fifth ScrollBars property, which is to hide the scroll bar of the ListView, android:scrollbars= "None" with setverticalscrollbarenabled (true); the effect is the same, not active when hidden, It's also hidden during the event.

The sixth Fadescrollbars property, android:fadescrollbars= "True" when configuring the ListView layout, sets this property to True to enable the automatic hiding and display of the scroll bar.

How do I use gallery to drag the flinging without a selection?
You need to pay attention to using
Gallery.setcallbackduringfling (False)

How do I get the ListView to scroll automatically?
Note Both the Stackfrombottom and Transcriptmode properties. The low end of a market-like client 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?

    1. ListView ListView = (ListView) Findviewbyid (r.id. config file in the ListView ID);
    2. Select the option to iterate through the ListView, with each option being equivalent to the 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 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 a different color under different conditions: Here's an example:

  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 to white when the focus is taken or selected, and other cases are set to orange.



How do I customize split lines between rows in 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 picture used 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 does not update the specified item  through notifydatasetchanged ();
ListView is mostly through Notifydatasetchanged () To update the ListView, but by Notifydatasetchanged () the actual item on the interface is redrawn once, which affects UI performance.  
can improve efficiency by updating the specified item, pseudocode as follows:  

    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 (
    6. }  



allows the listview to eject ContextMenu when it is pressed by some item, some cannot  
defines a ListView, and set up a Setoncreatecontextmenulistener listener for him, but this only makes all the items in the ListView pop up ContextMenu when long-pressed. &NBSP,
Sometimes hope is some long on time to pop ContextMenu, some can not. The solution to this problem is to set the Setonitemlongclicklistener listener for this listview and then implement the  

    1. Public Boolean Onitemlongclick (adapterview<?> parent, view view,
    2. int position, long id) {
    3. if (id = = 1) {
    4. return true;
    5. }
    6. return false;
    7. }


If this item is id=1, it cannot be long-pressed. That's it.

Problem with ListView Bottom Divider
In the work encountered a problem, is a ListView in the bottom of the item without a split line, the requirement is to have this split line. This strange phenomenon has been discovered through a study:
1. Listactivity has this bottom split line.
2. There is only a ListView in the activity, and there are no other controls.
In fact, Listactivity is also an activity, but in which the Setcontentview (ListView) method is used to set a ListView as the view it displays. So the conclusion is that as long as the activity calls the Setcontentview (ListView), it will have this bottom split line.
So what's not going to happen with this split floss? In activity, if you call Setcontentview (view) and the ListView is only embedded inside the view, there may not be a split line.
Analyze the reason: By adding breakpoints debugging found in the ListView, all the split lines are drawn by a very narrow rectangle to achieve, but before the drawing of the line will be judged the current position a and the length of the ListView B, if the a=b, then will not draw this split line. However, the ListView is embedded in a view and is generally set to a height of wrap_content, in which case the last split line is exactly equal to the height of the ListView, so the system will not draw this split line. What would it be like to draw it? It is simple to set the height of the ListView to Fill_partent.
Of course the above is the case of item very few, if the item is so much that it must show the scroll bar, then the last item below is sure there will be no split line. <!--[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.