Android interface programming--android Advanced UI components (III)

Source: Internet
Author: User

Android Interface Programming Android Advanced UI Components 2.4.1 Adapter Components the role of the adapter

The adapter acts as a bridge between the adapter control and the view data . the adapter provides access to the data item and is responsible for generating a view of each item in the Data group .


commonly used adapters

Baseadapter: Abstract class, with a high degree of flexibility.

Arrayadapter: The simplest, Smart display of a line of text.

Simpleadapter: Has a good extensibility, can customize a variety of effects.

Simplecursoradapter: primarily for manipulating databases.

Common Adapter Controls

The adapter controls extend from Viewadapter, which is the UIforloading data through the adapter and producing each item , and thecommon adapter controls are as follows:

ListView List Display control

GridView Grid Control

Spinner drop-down list control

Viewpager Horizontal Paging control

The adapter control uses the Setadapter (Adapter Adapter) method to mount the adapter

2.4.2 ListView list Display control

Is the list graph group that displays scrollable items, as shown in the News list


A list item is a Viewgenerated by an adapterthat can use arrayadapter,simpleadapter, Baseadapter to design a ListView list item

The following example explains the use of the ListView control and the adapter

Example 2.4-1: Implementation with Arrayadapter





Arrayadapter is a relatively simple adapter that can only be used to display a single line of text data. The constructor functions are as follows:

Arrayadapter (context context, int resource, t[] objects)

Arrayadapter (context context, int resource, list<t> objects)

Parameter description:

Resource: Layout files, using system-defined layout resources

Objects: List item data

Example 2.4-2: Implementation with Simpleadapter





Simpleadapter is more flexible than arrayadapter, and can list items to display a variety of data. The constructor functions are as follows:

Simpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[] to)

Context:simpleadapter the running environment of the associated view

Data: A list consisting of a map. Each entry in the list corresponds to a row in the list, and each map should contain all the keys specified in the FROM parameter

Resource: A resource ID for the layout file that defines the list item. The layout file should contain at least those IDs defined in to

From: A key name that will be added to the map map

To: The ID of the view that binds the data, corresponding to the from parameter




Example 2.4-3: implementation with Baseadapter







public class Newslistadapter extends Baseadapter {private context context;    Private list<news> newslist;        Public Newslistadapter (Context context,list<news> newslsit) {this.context=context;    This.newslist=newslsit;        } @Override public int getcount () {log.d ("Jereh", "Newslistadapter-->getcount");    Return newslist!=null?newslist.size (): 0;        } @Override public Object getItem (int position) {LOG.D ("Jereh", "Newslistadapter-->getitem");    return Newslist.get (position);        } @Override public long getitemid (int position) {LOG.D ("Jereh", "Newslistadapter-->getitemid");    return position;        } Private class viewholder{public TextView tvtitle;        Public TextView Tvauthor;        Public TextView Tvtimer;    Public ImageView ivimg; } @Override public View getView (int position, view Convertview, ViewGroup parent) {News News=newslist.get (p        Osition); ViewholderHolder=null;            if (convertview==null) {log.d ("Jereh", "newslistadapter--> constructs a view for list items");                    Convertview=layoutinflater.from (context).            Inflate (r.layout.ch04_news_item_layout,null);            Holder=new Viewholder ();            Holder.tvtitle= (TextView) Convertview.findviewbyid (r.id.tvtitle);            Holder.tvauthor= (TextView) Convertview.findviewbyid (R.id.tvauthor);            Holder.tvtimer= (TextView) Convertview.findviewbyid (r.id.tvtime);            Holder.ivimg= (ImageView) Convertview.findviewbyid (r.id.ivimg);        Convertview.settag (holder);        }else{holder= (Viewholder) Convertview.gettag ();        } holder.tvTitle.setText (News.gettitle ());        Holder.tvAuthor.setText (News.getauthor ());        Holder.ivImg.setImageResource (News.getimageres ());        Long Timer=news.gettime ();        String timeflag= "";        if (timer<1000*60*5) {timeflag= "just";            }else if (timer<1000*60*60) {Timeflag= "an hour ago";            }else {Date Date=new date (new Date (). GetTime ()-timer);            SimpleDateFormat sdf=new SimpleDateFormat ("MM month DD day");        Timeflag=sdf.format (date);        } holder.tvTimer.setText (Timeflag);    return convertview; }}

Implementation of a custom adaptation class

1, write the adaptation class, expand Baseadapter

public class Newslistadapter extends Baseadapter

2, write the constructor, the data required for the incoming adapter and the context

Public Newslistadapter (Context context,list<news> newslsit)

3, rewrite the method of Baseadapter

? int GetCount (): Returns the total amount of data

? Object getItem (int position): Gets a data based on position

? Long Getitemid (): Gets the ID of a row

? View getView (int postion,view convertview,viewgroup parent)

Generates a view of a list item and populates the data, returning the resulting views

Viewholder mechanism

In order to improve the efficiency of listvew view, reuse existing view, reduce the number of UI creation, and adopt the optimization scheme

1. Write the Viewholder class to define the UI elements required for list items

Private Class viewholder{

Public TextView Tvtitle;

Public TextView Tvauthor;

Public TextView Tvtimer;

Public ImageView ivimg;

}

2. If Convertview is empty, create UI and use Viewholder entity to refer to UI generated in list item, if Converview is not empty, you do not need to create, use the UI saved in Viewholder entity. The code snippet is as follows:

if (convertview==null) {

Convertview=layoutinflater.from (context).

Inflate (r.layout.ch04_news_item_layout,null);

Holder=new Viewholder ();

Holder.tvtitle= (TextView) Convertview.findviewbyid (r.id.tvtitle);

Holder.tvauthor= (TextView) Convertview.findviewbyid (R.id.tvauthor);

Holder.tvtimer= (TextView) Convertview.findviewbyid (r.id.tvtime);

Holder.ivimg= (ImageView) Convertview.findviewbyid (r.id.ivimg);

Convertview.settag (holder);

}else{

Holder= (Viewholder) Convertview.gettag ();

}






2.4.3 GridView Control

The scrollable grid control is displayed in rows and columns, and each item in the grid is generated by the association between ListAdapter and the view. Typical application of the "nine Gongge" effect:


Table 2.4.3-1 lists the properties and methods commonly used by the GridView.

Table 2.4.3-1gridviewl Properties and Methods

XML properties

Value

Note

Android:numcolumns

Num,auto_fit

Specifies the number of columns in the GridView that can be set to Automatic

Android:columnwidth

Dp

The width of each column, which is the width of item

Android:stretchmode

ColumnWidth

Set zoom mode, synchronize with column width size

Android:verticalspacing

Dp

The margin between two lines

Android:horizontalspacing

Dp

The margin between two columns

Android:cachecolorhint

#00000000

Remove the default black background when dragging

Android:scrollbars

None,vertical,Horizontal

Set how the GridView scroll bar is displayed

The use of the GridView is explained in the following example

Instance 2.4.3-1, using the GridView to implement a picture wall effect like









2.4.4 Spinner Controls

The drop-down options control gives the user a quick way to select a set of values, 2.4.4-1 the selected spinner will be displayed with all other available values and the user can select a new drop-down option.

Figure 2.4.4-1

The Spinner control also belongs to the adapter control, which is very similar to the previous ListView,GridView control, and a key point is writing Adapter, the following examples to explain The use of Spinner





2.4.5 Viewpager Controls

Cross-slide pagination control is one of a wide range of components, common applications have different channels of the left and right slide display, can flip the picture and so on. Viewpager Exclusive Adapter Pageradapter, by providing an implementation pageradapter the adapter to generate each display page that the view displays

Viewpager is most often used in conjunction with fragments, which is a convenient way to provide and manage the life cycle of each fragment. The standard adaptation fragmentpageradapter and fragmentstatepageradapter for implementing Viewpager fragments are provided.

Viewpager implementation steps:

1. Add the Viewpager control to the layout file

2, write the adapter--Realize Pageradapter

3. Load the adapter with Setadapter ()

Table 2.4.5-1The method of Pageradapter adapter and its effect.

Table 2.4.5-1pageradapter Methods and functions

Method

Role

int GetCount ()

Returns the number of flipped pages

Boolean Isviewfromobject (view view, Object obj)

Used to determine if an object is a view component for rendering in Viewpager

Object Instantiateitem (View container, int position)

Returns a page of Viewpager

void Destroyitem (ViewGroup container, int position, object object)

Remove a view from the ViewGroup

The use of Viewpager is gradually mastered by examples below.

Example 2.4.5-1, using Viewpager to implement the news image browsing interface.












20160707 of the peak of the cupola

Android interface programming--android Advanced UI components (III)

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.