Always wanted to summarize this knowledge, after all kinds of busy, do not want to write. It's better to summarize it yourself. There are many things to note about the ListView, and one of the most common controls. We can automatically calculate the height of the ListView when used with the scroll bar. The ListView divider line can also be removed, or changed. There is also a listener that is used with various controls. Next look at what I've described and.
This project demo address: Click here to download
First, look at the main categories of projects
Package Com.example.customlistviewdemo;import Java.util.arraylist;import Java.util.list;import android.os.Bundle; Import Android.app.activity;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemlongclicklistener;import Android.widget.baseadapter;import Android.widget.button;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.linearlayout;import Android.widget.listadapter;import Android.widget.listview;import Android.widget.textview;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.toast;public Class Customlistviewactivity extends Activity Implementsonclicklistener, Onitemlongclicklistener {private ListView Mylistview;private mycustomlistviewadapter mcustomlistviewadapter; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_custom_listview); Mylistview = (ListView) Findviewbyid (r.id.listview1); mcustomlistviewadapter = new Mycustomlistviewadapter (Getapplicationcontext ()); Mylistview.setonitemlongclicklistener (this); Long press the item to monitor Mylistview.setadapter (mcustomlistviewadapter); Setlistviewheightbasedonchildren (MyListView); The ListView automatically calculates the height button button = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (this);} ListView automatically calculates height private void Setlistviewheightbasedonchildren (ListView listview) {// Gets the listView corresponding to the adapterlistadapter ListAdapter = Listview.getadapter (); if (listadapter = = null) {return;} int totalheight = 0;for (int i = 0, Len = listadapter.getcount (); i < Len; i++) {View ListItem = Listadapter.getview (i, NULL, ListView); listitem.measure (0, 0); Calculates the width of the child view Totalheight + = Listitem.getmeasuredheight (); Counts the total height of all children}viewgroup.layoutparams params = listview.getlayoutparams ();p arams.height = totalheight+ ( Listview.getdividerheight() * (Listadapter.getcount ()-1)); Listview.setlayoutparams (params);} Public list<string> Getdatasource () {arraylist<string> List = new arraylist<string> (); List.add ("Beijing" ), List.add ("Shanghai"), List.add ("Guangzhou"), List.add ("Nanjing"), List.add ("Suzhou"), List.add ("Jiangsu"), List.add ("Hangzhou"), List.add ("Wuzhen"); List.add ("Lijiang"), List.add ("Dali"), List.add ("Kunming"); List.add ("Harbin"); return list;} public class Mycustomlistviewadapter extends Baseadapter Implementsoncheckedchangelistener, Onclicklistener {private Context Mcontext;private Layoutinflater Mlayoutinflater;public Mycustomlistviewadapter (context context) {MContext = Context;mlayoutinflater = (layoutinflater) mcontext.getsystemservice (context.layout_inflater_service);} @Overridepublic int GetCount () {return Getdatasource (). Size (); @Overridepublic Object getItem (int position) {return position;} @Overridepublic long getitemid (int arg0) {return 0;} @Overridepublic view GetView (int position, view arg1, ViewGroup arg2) {linearlayout linearlayout = (linearlayout) mlayoUtinflater.inflate (R.layout.layout_custom_checkbox_item, null); Linearlayout.setonclicklistener (this); Linearlayout.settag (position); TextView Titletextview = (TextView) Linearlayout.findviewbyid (r.id.textview1); if (Titletextview! = null) Titletextview.settext (Getdatasource (). get (position));//control check checkbox Selectedcheckbox = (checkbox) Linearlayout.findviewbyid (R.id.checkbox1); Selectedcheckbox.setoncheckedchangelistener (this); return linearLayout ;} @Overridepublic void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {integer nposition = (integer) (Arg0.gettag ()); Toast.maketext (Getapplication (), "checkbox Check Event:", Toast.length_short). Show (); @Overridepublic void OnClick (View arg0) {//click event Response int position = ((Integer) Arg0.gettag ()). Intvalue (); Toast.maketext (Getapplication (), "ListView Click event:" + Getdatasource (). get (position), Toast.length_short). Show ();}} @Overridepublic boolean Onitemlongclick (adapterview<?> arg0, View arg1, int arg2,long arg3) {Toast.maketext ( Getapplicationcontext (), "LiStview Long Press event: ", Toast.length_short). Show (); return true;} @Overridepublic void OnClick (View arg0) {if (Arg0.getid () = = R.id.button1) {Toast.maketext (Getapplicationcontext (), " Button event: ", Toast.length_short). Show ();}}}As we all know, the ListView can be self-scrolling, but it can also disable scrolling. Maybe you don't need it to scroll. Only scroll bars are required to scroll. That's what I've come across. The ListView is used with the scrollbar. The code is also explained.
Second, look at the XML
<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:orien tation= "vertical" > <framelayout android:layout_width= "match_parent" android:layout_height= "match_p Arent "> <scrollview android:id=" @+id/scrollview1 "android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:layout_weight= "0.96" > <linearlayout Android:layout_width= "Match_parent" android:layout_height= "Match_parent" Andro id:orientation= "vertical" > <linearlayout android:layout_width= "match_parent" android:layout_height= "60DP" android:orientation= "vertical" > < TextView android:id= "@+iD/textview1 "android:layout_width=" match_parent "android:layout_height=" MATC H_parent "android:gravity=" center "android:text=" Custom ListView Style " Android:textsize= "18sp"/> </LinearLayout> <listview Android:id= "@+id/listview1" android:layout_width= "Match_parent" android:layout_he ight= "400DP" > </ListView> </LinearLayout> </ScrollView> <l Inearlayout android:layout_width= "match_parent" android:layout_height= "60DP" Android:layo ut_gravity= "Bottom" android:gravity= "center" android:orientation= "vertical" > <butt On android:id= "@+id/button1" android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:text= "button" android:textsize= "18sp"/> </LinearLayout> </fr Amelayout></linearlayout>
The XML here also uses a floating framelayout to display the button key.
Third, look at the XML of the ListView item
<?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=" Match_parent "android:longclickable=" true " Android:clickable= "true" android:orientation= "vertical" > <linearlayout android:layout_width= " Match_parent "android:layout_height=" 50DP "android:gravity=" center "> <textview and Roid:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "50DP" android:layout_weight= "1.02" android:gravity= "Center_vertical|left" android:paddingleft= "10DP" Android:textcolor= "#000" android:text= "TextView" android:textsize= "18sp"/> <che Ckbox android:id= "@+id/checkbox1" android:layout_width= "Wrap_content" Android:layout_heig ht= "Match_parent" Android:paddingright= "10DP" android:textcolor= "#000" android:layout_gravity= "right"/> </linearla Yout></linearlayout>
Here is a description, linearlayout need these two sentences. The settings for the ListView Click and long Press, respectively.
Android:longclickable= "true" android:clickable= "true"
Four, next look:
Five, finally add, remove the ListView divider line or settings.
There are three ways to remove the divider line:
1, mylistview.setdividerheight (0);
2, Mylistview.setdivider (null);
- 3. Set the properties of the ListView in the XML layout file android:divider= "#FFCC00" or android:divider= "@null"
Set or change a nice, automatic divider line.
android:divider= "@drawable/skin_line"
Here is the end, this article is about listening and layout, about the ListView and scroll bar, Checkbox,buuton with.
This project demo address: Click here to download
Android ListView Long Press, click on various event snaps