Tag Archives: android listview

Android listview entry button click state chaos solution

Problems:

Click the button button on listview item, click to change the display state of button, slide the button on listview item is reused, the corresponding button state on item is not corresponding, and the button state on item is chaotic.

Analysis of the reasons:

Listview item caching mechanism: for better performance, listview will cache line items (the view corresponding to a line). Listview gets the items of each line through the getview function of the adapter. During sliding,

A. if a line item has already been shown on the screen, if the modified item is not in the cache, put it into the cache, otherwise update the cache;

B. before obtaining the line items that slide into the screen, it will first determine whether there are available items in the cache. If there are, it will be passed to the getview of the adapter as the convertview parameter.

In this way, getview writing can make full use of the cache and greatly improve the performance of listview. Even if there are tens of thousands of line items, the maximum number of inflates is n, and N is the maximum number of listview line items displayed on a screen.

According to the above caching principle, the reuse of listview will cause the chaos of the control state on the item.

For detailed analysis, please refer to http://www.trinea.cn/android/android-listview-display-error-image-when-scroll/

Solution:

The idea of Java programming is that everything is an object. If the status of an item is recorded in the corresponding object of each item, through the field of the object, each time you click to change the field value of the object, each time you display the status of the control on the item, you can judge the field value of the object. The problem of chaos has to be solved.

Add a field to the entry object without reason, but the server does not need this field. In order to unify the front and back ends, this will cause trouble. Therefore, when we add the Boolean value of state judgment to the object, we add the field modifier keyword transient before the field in the bean. For example, if the judgment Boolean value added in the bean is ischeck, then it is private transient Boolean ischeck in the bean;

Transient as the name suggests: (from Youdao dict)

Transient phenomena; passing passengers; migratory birds

In Java, “transient” modifier is a special annotation to indicate that the modified field is ignored in serialization.

For detailed analysis, please refer to http://www.cnblogs.com/gaojing/archive/2011/04/14/2844973.html

Conclusion:

Therefore, adding a field to the entry object (in order to record the state of the object) and adding the keyword “transient” in front of the field can solve the problem of confusion when clicking the listview entry button~

Several properties of Android listview

First is the stackFromBottom property, after this property your finished list will be displayed at the bottom of your list, with the values true and false

android:stackFromBottom="true"             

The second is the transciptMode property, you need to track or view information in real time with a ListView or other control that displays a large number of Items, and you want the newest items to be automatically scrolled to the visible range. The control transcriptMode property can be set to automatically scroll to the bottom of the Android platform control (which supports ScrollBar) by setting
 android:transcriptMode="alwaysScroll"    

The third cacheColorHint property is that many people want to change the background to match the overall UI design, it's easy to change the background by preparing an image and specifying the property android:background="@drawable/bg", but don't be too happy, after you do that, you will find that the background is but when you drag or click on the blank position of the list, you will find that the ListItem becomes black, which ruins 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 pictures as the background, then also just specify android:cacheColorHint as transparent (#00000000) on it!

The fourth divider attribute, the role of this attribute is to set a picture as a spacer between each item, or to remove the divider line between the items

 android:divider="@drawable/list_driver" where @drawable/list_driver is an image resource, if you don't want to show the divider, just set it to android:divider="@drawable/@null" and you're done.

The fifth fadingEdge property, with black shadows on the top and bottom edges

android:fadingEdge="none" set to no shadow~

 The fifth scrollbars property, which hides the scrollbars of the listView, is

android:scrollbars="none" and setVerticalScrollBarEnabled(true); the effect is the same, hidden when inactive and hidden when active

The sixth fadeScrollbars property, android:fadeScrollbars="true", can be set to true when configuring the ListView layout to achieve automatic hiding and displaying of scrollbars.