Android ListView Fillet

Source: Internet
Author: User

First look at the basic properties of the ListView

1. After clicking on the list, the background of the list becomes black. You can change it by specifying the properties of the Android:cachecolorhint to specify it as transparent. Use the following property values:
android:cachecolorhint= "#000000".
2. Remove the split line in the middle of the list item: android:divider= "#00000000", where the value can also point to a drawable picture object (android:divider= "@drawable/list_line"), If you use a picture that is taller than the system's pixels, you can set a height yourself. android:dividerhight= "10px"
3. When the ListView is dragging, the background of the ListView turns black. Use this to solve: Android:scrollingcache= "false"
4, the top and bottom of the ListView has a black shadow. Use this to solve: android:fadingedge= "None"
5. The slider to the right of the ListView overrides the contents of the list item. Use this to solve: android:scrollbarstyle= "Outsideinset"
6. Modify the distance between the slider bar on the right side of the Listvew and the list item. Use this to solve: android:paddingright= "10dip", can be modified as needed.
7. Modify the color of the slider bar display on the right. Use this to solve:
android:scrollbartrackvertical= "@drawable/scrollbar_vertical_track"
android:scrollbarthumbvertical= "@drawable/scrollbar_vertical_thumb"
where Scrollbar_vertical_track and Scrollbar_vertical_thumb are the XML configuration file for the slider bar, in the API, you can modify the start color and the end color to suit your needs.

Please refer to this article for specific Android ListView properties

The following is a look at the ListView implementation, the main highlight of this article is that the data source can be flexibly set, not to write dead on the XML.

1. Under the drawable file, create a new

List_bottom_selector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "><shape>            <solid android:color=" #ADFF2F "/ >            <corners android:bottomleftradius= "4DP" android:bottomrightradius= "4DP"/>        </shape></ item>    <item><shape>            <solid android:color= "@color/white"/>            <corners android: bottomleftradius= "4DP" android:bottomrightradius= "4DP"/>        </shape></item></selector>
Create a few new XML

List_rect_selector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "><shape>            <solid android:color=" #ADFF2F "/ >        </shape></item>    <item><shape>            <solid android:color= "@color/white"/ >        </shape></item></selector>

List_round_selector.xml

<?xml version= "1.0" encoding= "Utf-8"?> <selector    xmlns:android= "http://schemas.android.com/apk/res/ Android ">                <item android:state_pressed=" true "><shape>                  <solid android:color=" #ADFF2F "/ >                  <corners android:bottomleftradius= "4DP" android:bottomrightradius= "4DP"                     android:topleftradius= " 4DP "android:toprightradius=" 4DP "/>              </shape></item>          <item><shape>                  < Solid android:color= "@color/white"/>                        <corners android:bottomleftradius= "4DP" android:bottomrightradius= "4DP"                     android:topleftradius= "4DP" android:toprightradius= "4DP"/>              </shape></item>            </selector>  

List_top_selector.xml

<?xml version= "1.0" encoding= "Utf-8"?> <selector    xmlns:android= "http://schemas.android.com/apk/res/ Android ">                <item android:state_pressed=" true "><shape>                  <solid android:color=" #ADFF2F "/ >                        <corners android:topleftradius= "4DP" android:toprightradius= "4DP"/>              </shape></item >          <item><shape>                        <solid android:color= "@color/white"/>                        <corners android: topleftradius= "4DP" android:toprightradius= "4DP"/>              </shape></item>            </selector>  

Next we create a new item.xml in layout

<?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 "    android: paddingright= "5DP"    >    <textview         android:id= "@+id/text"        android:layout_width= "Fill_parent"        android:layout_height= "40DP"       android:textsize= "16sp"       android:paddingleft= "15DP"       android: Textcolor= "#000000"        android:gravity= "left|center_vertical"        />        <imageview         android:src = "@drawable/common_left"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:layout_centervertical= "true"        android:layout_alignparentright= "true"        /></ Relativelayout>

After creating a new activity_main.xml as our main page of the program, that is, we want to display the ListView, the layout is very simple

As follows:

<?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:orientation        = "vertical" android:paddingleft= "10DP" android:paddingright= "10DP" android:paddingtop= "15DP" > <listview android:id= "@+id/list1" android:layout_width= "Fill_parent" android:layou            t_height= "Wrap_content" android:background= "@drawable/round_bg" android:cachecolorhint= "#00000000" android:divider= "@color/list_divider" android:dividerheight= "0.1DP" android:listselector= " #00000000 "> </ListView> <listview android:id=" @+id/list2 "Android:layout_            Width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "15DP" android:background= "@drawable/round_BG "android:cachecolorhint=" #00000000 "android:divider=" @color/list_divider "Android:div            iderheight= "0.1DP" android:listselector= "#00000000" > </ListView> <listview            Android:id= "@+id/list3" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:layout_margintop= "15DP" android:background= "@drawable/round_bg" Android:cachecolorh Int= "#00000000" android:divider= "@color/list_divider" android:dividerheight= "1px" Android : listselector= "@color/transparent" > </ListView> </LinearLayout>


Then create a new mainactivity

public class Mainactivity extends Activity {private ListView list1;private ListView list2;private ListView List3; String array1[] = {"Set"}; String array2[] = {"Circle of Friends", "My Space"}; String array3[] = {"My wallet", "My Favorites", "My Albums", "Associations"}; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview ();}    private void Initview ()    {list1 = (ListView) Findviewbyid (r.id.list1); list2 = (ListView) Findviewbyid (R.ID.LIST2); List3 = (ListView) Findviewbyid (R.ID.LIST3); List1.setadapter (new Mylistadapter (Array1,this)); List2.setadapter (new Mylistadapter (Array2,this)); List3.setadapter (new Mylistadapter (Array3,this));}    }


Later we customize a mylistadapter inheritance Baseadapter

public class Mylistadapter extends Baseadapter {private layoutinflater inflater;private string[] array;private Context MC Ontext;public Mylistadapter (string[] Array,context context) {Inflater = Layoutinflater.from (context); This.array = Array;this.mcontext =context; } @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn array.length;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn array[position];} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubconvertv Iew = inflater.inflate (R.layout.item, NULL); TextView TV = (TextView) Convertview.findviewbyid (R.id.text); Tv.settext (array[position]); if (array.length = = 1) { Setbackgrounddrawable (convertview,r.drawable.list_round_selector);} else if (Array.Length = = 2) {if (position = = 0) {setbackgrounddrawable (convertview,r.drawable.list_Top_selector);} else if (position = = array.length-1) {setbackgrounddrawable (convertview,r.drawable.list_bottom_selector);}} else {if (position = = 0) {setbackgrounddrawable (convertview,r.drawable.list_top_selector);} else if (position = = array.length-1) {setbackgrounddrawable (convertview,r.drawable.list_bottom_selector);} else {setBackgroundDrawable (Convertview,r.drawable.list_rect_selector);}} return Convertview;} private void setbackgrounddrawable (view view, int ResID) {view.setbackgrounddrawable (Mcontext.getresources (). Getdrawable (ResID));}}



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.