Android Learning notes-fourth Android development component 2

Source: Internet
Author: User

Fourth Android Development component 2

  1. List class components

    (1) ListView component: Lists items that need to be displayed in a vertical list

    Related properties:

    A. Android:divider used to set the separator bar for a list view, you can use color or picture resources

    B. Android:dividerheight setting the height of the separator bar

    C. android:entries specifying list items for a ListView through array resources

    D. android:footerdividersenabled sets whether the separator bar is drawn before Footerview, which defaults to true.

    E. android:headerdividersenabled sets whether the separator bar is drawn after Headerview, which defaults to true.

    (2) ListView Use steps:

    A. Drag into a ListView control and change the height to match_parent

    B. Create a new layout XML file that sets the style for each line display

    C. Create a class myadapter inherit Baseadapter

    GetView () Method: Sets the display of each row of data and the display style of each row

    GetCount () Method: Displays the number of rows, when the return value is 0 o'clock, the GetView () method is not called, and is displayed as blank

    D. Creating an Adapter object

    Myadapter adapter = new Myadapter ();

    E. Setting up an adapter for a ListView

    Listview.setadapter (adapter);

    (3) Optimization of the ListView

    Swipe up and down will constantly trigger the GetView () method

    Workaround: Reuse row layouts to reduce control lookups

    Create a class that declares several variables (the variable type and quantity are consistent with the controls contained in the layout XML file)

    Class viewholder{

    View View1;

    View View2;

    ......

    }

    Declaring member variables

    View inflate;

    Viewholder Holder;

    Optimization

    if (convertview==null) {

    Create a Viewholder object

    Get the Layout Converter object

    Layoutinflater inflater = Getlayoutinflate ();

    To convert a layout XML file to a layout object

    Inflate = Inflater.inflate (r.layout.listitem,null);

    Find controls

    Holder.view1 = Inflate.findviewbyid (r.id.view1);

    Holder.view2 = Inflate.findviewbyid (R.ID.VIEW2);

    Set a label

    Infalte.settag (holder);

    }else{

    Replace, re-use row layouts

    Inflate=convertview;

    Holder = (viewholder) inflate.gettag ();

    }

    To set the contents of a control based on control type

    ......

    (3) Data update

    When data changes, the adapter notifies the ListView to update the data

    Adapter.notifydatasetchanged ();

    (4) Click events for ListView

    A. Setting up tap-listening

    Listview.setonitemclicklistener (new Onitemclicklistener) {

    @Override

    public void Onitemclick (adapterview<?> parent,view view,int position,long ID) {

    ......

    }

    }

    Note: When a control with a preemption focus exists in the row layout (such as a button), the click of the ListView is invalidated

    How to resolve:

    Set properties on the control that preempted the focus in the row layout: Android:focusable = "false"

    Set in row layout so that all the controls that can preempt the focus do not preempt the focus

    android:descendantfocusability = "Blocksdescendants"

    B. Set long press Tap to listen

    Listview.setonitemlongclicklistener (new Onitemlongclicklistener) {

    @Override

    public void Onitemlongclick (adapterview<?> parent,view view,int position,long ID) {

    ......

    }

    }

  2. Spinner Components: list selection box is equivalent to a common drop-down list box in a Web page

    GetSelectedItem (): Gets the selected value of the list selection box

    Onitemselectedlistener: Sets the selected event listener for the list item.

    Setdropdownviewresource (): Sets the option style for the adapter when the list box is pulled down

  3. Gallery Components: Gallery View, you can display content horizontally, and you can use your finger to drag the picture to move, generally used to browse pictures. The Baseaadapter adapter is typically used to provide the Grallery component with a picture to display.

  4. Imageswitcher components: picture Switcher for implementing features like "previous", "Next" in Windows Photo Viewer under Windows.                      The Viewswitcher.viewfactory interface must be implemented using Imageswitcher, and the Makeview () method is used to create a The ImageView object. For example:

    Imageswitcher.setinanimation (XXX);//Set Fade animation

    Imageswitcher.setoutanimation (XXX);//Set Fade animation

    Imageswitcher.setfactory (New Viewfactoty () {

    @Override

    Public View Makeview () {

    ImageView ImageView = new ImageView (mainactivity.this);

    return imageView;

    }

    });

    Imageswitcher.setimageresource (XXX); Set the displayed picture

Android Learning notes-fourth Android development component 2

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.