Android ListView Common Tips Summary _android

Source: Internet
Author: User

ListView's position in our Android program is obvious to all, and I believe almost every app has its shadow.
ListView mainly uses the list form to load the data, needs to realize some special functions in the specific situation: for example refreshes the data, the load data, realizes the animation effect and so on.
What do we need to be aware of as our common control?
* * Set divider line for each item of ListView

The first method: also the simplest way to set the ListView in the layout file
Divider Property
such as: android:divider= "@color/black"
The second method: set the android:divider= "@null" to indicate that you don't want to divide the line, and then add the separator line yourself in the item layout.

 <ListView>
    android:id= "@+id/test_lv"
    android:layout_width= "match_parent"
    android:layout_ height= "Wrap_content"
    android:divider= "#d1d1d1" <!--set separator line color-->
    android:dividerheight= "1px" <!-- Set divider height-->
    >
 </ListView>

* * By default, listview item has click Effect, how to change this effect?
Set the Android:listselector property of the LISTVIEWR, such as setting the transparency to cancel the effect android:listselector= "@android: Color/transparent"
You can also set the effect you want, add the corresponding color resources or drawble resources.
* * When the data is more, ListView will display a default scroll bar when scrolling, to cancel the scroll bar, you can set the Android:scrollbars property
such as: android:scrollbars= "None"
* * ListView Performance optimization, must use Viewholder to give full play to the ListView recycle mechanism
* * ListView Data dynamic changes, add or delete data operations, the effect is obvious.

Implement ListView Data add delete public class Mainactivity extends activity implements View.onclicklistener {private ListView test_l
  V
  Private list<testbean> dataList;
  Private Testadapter adapter;
  Private Button add_btn, del_btn;
  Private ImageView Emptyiv;

  Private LinearLayout Operator_ll;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Initdatas ();

  Initviews ();
    private void Initdatas () {dataList = new arraylist<> ();
      for (int i = 0; i < 5; i++) {Testbean bean = new Testbean ();
      Bean.settitle ("title _" + i);
      Bean.setcontent ("This is content _" + i);
      Bean.settype (i% 2 = 0? 1:2);
    Datalist.add (Bean);
    } private void Initviews () {this.test_lv = (ListView) Findviewbyid (R.ID.TEST_LV);
    THIS.ADD_BTN = (Button) Findviewbyid (R.ID.ADD_BTN);
    THIS.DEL_BTN = (Button) Findviewbyid (R.ID.DEL_BTN); Emptyiv = (ImagevieW) Findviewbyid (R.ID.EMPTY_IV);
    Operator_ll= (LinearLayout) Findviewbyid (R.ID.OPERATOR_LL);
    This.add_btn.setOnClickListener (this);
    This.del_btn.setOnClickListener (this);
      if (datalist.size () = = 0) {emptyiv.setvisibility (view.visible);
      Operator_ll.setvisibility (View.gone);
    Test_lv.setemptyview (Emptyiv);
      else {emptyiv.setvisibility (view.gone);
      Operator_ll.setvisibility (view.visible);
      adapter = new Testadapter (this, dataList);
    Test_lv.setadapter (adapter);
  } test_lv.setselection (15);
        @Override public void OnClick (View v) {switch (V.getid ()) {case R.ID.ADD_BTN://default add in first position
        Testbean bean = new Testbean ();
        Bean.settitle ("Add Item");
        Bean.setcontent ("This is added content");
        Bean.settype (1);
        Datalist.add (0, Bean);
      Break
        Case R.ID.DEL_BTN://default Delete First Data datalist.remove (0);
    Break
Adapter.notifydatasetchanged ()//Refresh ListView Data}//listview corresponds to adapter public class Testadapter extends Baseadapter {private context mcontext;

  Private list<testbean> Listdatas;
    Public Testadapter (Context Mcontext, list<testbean> listdatas) {this.mcontext = Mcontext;
  This.listdatas = Listdatas;
  @Override public int GetCount () {return listdatas.size ();
  @Override public Object getitem (int position) {return listdatas.get (position);
  @Override public long getitemid (int position) {return position;
    @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder holder = null;
      if (Convertview = = null) {holder = new Viewholder ();
      Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.test_item, NULL);
      Holder.title = (TextView) Convertview.findviewbyid (r.id.item_title);
      Holder.content = (TextView) Convertview.findviewbyid (r.id.item_content);
    Convertview.settag (holder); else {HoldeR = (viewholder) convertview.gettag ();
    } holder.content.setText (Listdatas.get (position). GetContent ());
    Holder.title.setText (Listdatas.get (position). GetTitle ());
  return convertview;
    Final class Viewholder {TextView title;
  TextView content;

 }
}

* * Similar to the chat interface, ListView has a variety of item style effects

Implement the various item styles in ListView public class Typeactivity extends activity {private ListView type_lv;
  Private list<testbean> dataList;

  Private Typeadapter adapter;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_type);
    Initdatas ();

  Initviews ();
    private void Initdatas () {dataList = new arraylist<> ();
      for (int i = 0; i < 9; i++) {Testbean bean = new Testbean ();
      Bean.settitle ("title _" + i);
      Bean.setcontent ("This is content _" + i);
      Bean.settype (i% 2 = 0? 1:2);
    Datalist.add (Bean);
    } private void Initviews () {this.type_lv = (ListView) Findviewbyid (R.ID.TYPE_LV);
    adapter = new Typeadapter (this, dataList);

  Type_lv.setadapter (adapter);
  }//corresponds to adapter public class Typeadapter extends Baseadapter {private context mcontext;

  Private list<testbean> Listdatas; Public Typeadapter (Context Mcontext, list<Testbean> listdatas) {this.mcontext = Mcontext;
  This.listdatas = Listdatas;
  @Override public int GetCount () {return listdatas.size ();
  @Override public Object getitem (int position) {return listdatas.get (position);
  @Override public long getitemid (int position) {return position;
    @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder holder = null;
    Different layouts and data display according to styles the int type = Getitemviewtype (position);
      if (Convertview = = null) {holder = new Viewholder ();
        if (type = = 1) {Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.test_item, NULL);
        Holder.title = (TextView) Convertview.findviewbyid (r.id.item_title);
      Holder.content = (TextView) Convertview.findviewbyid (r.id.item_content);
        else {Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.type_item, NULL); Holder.title = (TextView) convertview.finDviewbyid (R.id.type_title);
      Holder.content = (TextView) Convertview.findviewbyid (r.id.type_title);
    } convertview.settag (holder);
    else {holder = (Viewholder) convertview.gettag ();
    } holder.content.setText (Listdatas.get (position). GetContent ());
    Holder.title.setText (Listdatas.get (position). GetTitle ());
  return convertview;
  }//Key method Getviewtypecount: Get how many styles @Override public int getviewtypecount () {return 2; }//Key method Getitemviewtype: Get item type @Override public int getitemviewtype (int position) {return Listdatas.get (Positi
  ON). GetType ();
    Final class Viewholder {TextView title;
  TextView content;

 }
}

Related layouts

Activity_main.xml <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" http:// Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:orientation=" vertical "> <linearlayout android:id=" @+ Id/operator_ll "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:orientation=" Horizontal "> <button android:id=" @+id/add_btn "android:layout_width=" 0DP "Android:layout_heig ht= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "add"/> <bu Tton android:id= "@+id/del_btn" android:layout_width= "0DP" android:layout_height= "Wrap_content" and roid:layout_weight= "1" android:gravity= "center" android:text= "delete"/> </LinearLayout> <listvie W android:id= "@+id/test_lv" Android:layoUt_width= "Match_parent" android:layout_height= "wrap_content" android:divider= "#d1d1d1" <!--separator Line Color-->
    id:dividerheight= "1px" <!--divider height--> android:listselector= "@android: Color/transparent" <!--cancel default Click Effect-->
    Android:scrollbars= "None" ><!--hidden scroll bars--> </ListView> <imageview android:id= "@+id/empty_iv" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" Andro

 id:layout_margintop= "100DP" android:src= "@mipmap/empty" android:visibility= "Gone"/> </LinearLayout>

Activity_type.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  xmlns:tools=" Http://schemas.android.com/tools "
  android:layout_width=" Match_parent
  " android:layout_height= "Match_parent"
  android:orientation= "vertical" >


  <listview android:id=
    "@ +id/type_lv "
    android:layout_width=" match_parent "
    android:layout_height=" wrap_content "
    ></" Listview>


</LinearLayout>

Test_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" wrap_content "
 android:o" rientation= "vertical"
 android:paddingleft= "15DP"
 android:layout_margintop= "5DP"
 android:layout_ marginbottom= "5DP" >

 <textview
  android:id= "@+id/item_title"
  android:layout_width= "WRAP_" Content "
  android:layout_height=" wrap_content "
  android:text=" Hello world!/>

 <textview
  android:id= "@+id/item_content"
  android:layout_width= "wrap_content"
  android:layout_height= "wrap" _content "
  android:layout_margintop=" 5DP "
  android:text=" Hello world!/>
</LinearLayout>

The above is the collection of Android ListView common tips for all the content, I hope to help you learn.

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.