Explain how to write the ListView list options bar in Android applications _android

Source: Internet
Author: User
Tags static class

According to the list of adapter types, the list is divided into three kinds, arrayadapter,simpleadapter and simplecursoradapter, the use of these three kinds of adapters you can learn descend network above the use or own Baidu Google, a bunch of demo!!! One of the most simple to arrayadapter, can only show a line of words. Simpleadapter has the best extensibility and can be customized to a variety of effects. Simplecursoradapter can be considered as a simple combination of the Simpleadapter database, you can easily display the contents of the database as a list of the form.

The system to draw ListView, he first use the GetCount () function to draw the length of the list, and then start to draw the first line, how to draw it? Call the GetView () function. In this function you first get a view (this looks at the actual situation, if a simple display is view, if it is a custom contains a lot of controls when it is actually a viewgroup), and then instantiate and set the components and their data content and display it. All right, I've finished drawing this line. Then draw the next line, until the painting is finished, the front of these things to pave, continue ...

Now we come to understand the principle of ListView loading data, with this understanding of the optimization after the line, the following first with you to see the basic principles of ListView Loading data directly written:

The working principle of ListView is as follows:

ListView for each item, the adapter "return a View" (GetView) is required, which means that the system first invokes the GetCount () function, based on the length of his return, which is worth the ListView. Then, based on this length, the GetView () line is invoked to draw each item of the ListView. If your GetCount () returns a value of 0, the list line does not appear, and if you return 1, only one row is displayed. Several rows are displayed by returning a few. What if we have thousands of tens of thousands of or more item to show? Create a new view for each item? No way!!! Actually Android has already cached these views, you can see the following screenshot to understand, this diagram is to explain the ListView work principle of the most classic figure you can collect, do not understand when take to see, deepen understanding, In fact, there is a component called recycler in Android, incidentally listed with recycler related to Google has been done by the N more optimized things such as: Abslistview.recyclerlistener, Viewdebug.recyclertracetype and so on, to understand the friends themselves check, not difficult to understand, the following figure is ListView load data working principle

public class Multipleitemslist extends Listactivity {private Mycustomadapter madapter;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Madapter = new Mycustomadapter ();
    for (int i = 0; i < i++) {Madapter.additem ("item" + i);
  } setlistadapter (Madapter);
    Private class Mycustomadapter extends Baseadapter {private ArrayList mdata = new ArrayList ();
 
    Private Layoutinflater Minflater;
    Public Mycustomadapter () {minflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
      public void AddItem (final String Item) {Mdata.add (item);
    Notifydatasetchanged ();
    @Override public int GetCount () {return mdata.size ();
    @Override public String getitem (int position) {return mdata.get (position);
    @Override public long getitemid (int position) {return position; } @OverriDe public View getview (int position, View Convertview, ViewGroup parent) {System.out.println ("GetView" + posit
      Ion + "" + Convertview);
      Viewholder holder = null;
        if (Convertview = = null) {Convertview = minflater.inflate (r.layout.item1, NULL);
        Holder = new Viewholder ();
        Holder.textview = (TextView) Convertview.findviewbyid (R.id.text);
      Convertview.settag (holder);
      else {holder = (Viewholder) convertview.gettag ();
      } holder.textView.setText (Mdata.get (position));
    return convertview;
  } public static class Viewholder {public TextView TextView;
 }
}

Execute program to view log:

GetView is called 9 times, Convertview is null for all visible items (as follows):

Then scroll down the list a little, until ITEM10 appears:

Convertview is still a null value because there is no view in recycler (the edge of the item1 is still visible, at the top) to scroll through the list and continue scrolling:

At this point the Convertview is not empty, after Item11 left the screen, its view (...). 0F8) as Convertview accommodates the ITEM12.

Custom Multiple ListView

However, the implementation of the Android multiple ListView is implemented through Checkedtextview, but the control is ugly and a row of content layouts are fixed.
Here we customize one:

Mlistview = (ListView) Findviewbyid (R.id.listview); 
Mlistview.setcachecolorhint (0); 
Mlistview.setitemscanfocus (false); 
Mlistview.setchoicemode (listview.choice_mode_multiple); 

Adapter Definition:

@Override public 
View getview (int position, View Convertview, ViewGroup parent) { 
  Data data = Mdatalist.get (posit ION); 
 
  if (Convertview = = null) { 
    Convertview = minflater.inflate (r.layout.xxxx, null) 
  ; 
 
  TextView Nameview = (TextView) Convertview.findviewbyid (r.id.name); 
  Nameview.settext (data. FullName); 
 
  ImageView Checkview = (imageview) Convertview.findviewbyid (R.id.check_view); 
  if (mlistview.getadapter ()!= null) { 
    Sparsebooleanarray Checkedarray = Mlistview.getcheckeditempositions (); 
    if (0 <= position && position < Checkedarray.size ()) { 
      if (checkedarray.valueat (position)) { 
        Checkview.setimageresource (r.drawable.checkbox_selected); 
      } 
      else{ 
        Checkview.setimageresource (r.drawable.checkbox_unselected); 
 
  }} return convertview; 
} 

Xxxx.xml Layout File Implementation

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= 
"http://schemas.android.com/apk" /res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" wrap_content " 
  > 
  <textview  
    android:id= "@+id/name" 
    android:layout_width= "Wrap_content" 
    Wrap_content " 
    android:textcolor=" @color/black " 
    android:layout_marginleft=" 10dip " 
    android:gravity = "Center_vertical" 
    /> 
  <imageview  
    android:id= "@+id/check_view" 
    Wrap_content " 
    android:layout_height=" wrap_content " 
    android:layout_alignparentright=" true " 
    android:layout_marginright= "10dip" 
    android:layout_centervertical= "true" 
    android:background= "@ drawable/checkbox_unselected " 
    /> 

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.