Controls related to adapters in android ---- ListView and androidadapter controls

Source: Internet
Author: User

Controls related to adapters in android ---- ListView and androidadapter controls

ListView explanation:

1. ListView is a widely used widget, which deserves further study and research. The basic usage has been used in the Adapter.

Ii. common attributes and Methods

FooterDividersEnabled: whether to draw a split line before footerView (Table end). The default value is true.

HeaderDividersEnabled: whether to draw a split line after headerView (header). The default value is true.

Divider: sets the split bar, which can be a color split or a drawable resource split.

DividerHeight: sets the height of the separator bar.

StackFromBottom: The list is displayed from the bottom and set to true. It seems useless.

CacheColorHint: If you set an image as the Background for the ListView, when you drag or click the blank position of the listView, the item turns black, this is the time when we use the ColorHint to set the color to transparent: #000000

Scrollbars = "none" or setVerticalBarEnabled (true) Hide the slider

Adding the header or end of a listView seems to be only available in Java.

AddHeaderView: adds a HeaderView (header). It contains a View object and can be obtained through the context object.

AddFooterView (View view): adds a foot two View (end of the table), which contains a view object and can also be obtained through the context object.

AddHeaderView (headerView, null, false): difference from the above: Set whether the Header can be selected

AddFooterView (footerView, null, false): different from the preceding one. Set whether footer can be selected.

Iii. listView focus

If you add buttons, checkboxes, and other controls to an item, you need to consider the focus issue.

When writing a simple listView with a Button or checkBox contained above, clicking the item does not work, that is, the onItemClick event cannot be triggered, that is, the listView focus is preemptible by other controls.

Solution:

Method 1: Set android: focusable = "false" for the preemptible Control"

Method 2: Set android: descendantFocusability = "blocksDescendants" for the root node of item. There are three options for attribute values:

BeforeDescendants: This value indicates that ViewGroup takes precedence over its subclass control and obtains the focus.

AfterDescendants: This value indicates that ViewGroup obtains the focus only when the control of the subclass does not need to obtain the focus.

BlocksDescendants: This value indicates that ViewGroup will overwrite the subclass control and directly obtain the focus.

Iv. Incorrect choice of CheckBox in listView.

Method 1: Put it in a HashMap <Integer, Boolean>. During each initialization, the corresponding boolean value is retrieved Based on postion, and then the checkbox status is set. That is

HashMap <Integer, Boolean> state = new HashMap <Integer, Boolean> ();

Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
// TODO Auto-generated method stub
If (isChecked)
{
State. put (position, isChecked );
} Else {
State. remove (position );
}
}

In the getView () method: holder. cbox. setChecked (state. get (position) = null? False: true );

Method 2: Add a boolean value to the entity class to determine

5. Data Update of listView (add, delete, modify, and query)

Call the yydatasetchanged () method as an example.

Step 1: Add a data update method to the custom BaseAdapter:

Step 2: Set the button listening event in the activity to add a piece of data

Every time you call yydatasetchanged () to update data, this method will actually re-draw the items on the interface, which will affect the UI performance. If the data volume is large, it is obviously not cost-effective to re-plot all the items as long as one of the items is changed. At this time, we customize the following method.

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.