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" would be OK.
2, remove the middle of the list of cutting lines: android:divider= "#00000000", where the value can also point to a drawable picture object (android:divider= "@drawable/list_line"). Assuming that the image height is greater than the system's pixels, it is possible to set a height yourself.

android:dividerhight= "10px"
3, the ListView when 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. Change the distance between the slider on the right of the LISTVEW and the list item.

Use this to solve: android:paddingright= "10dip". Can be changed on the basis of need.
7. Change the color of the right slider bar display. Use this to solve:
android:scrollbartrackvertical= "@drawable/scrollbar_vertical_track"
android:scrollbarthumbvertical= "@drawable/scrollbar_vertical_thumb"
among them, Scrollbar_vertical_track and Scrollbar_vertical_thumb is the XML configuration file of the slider bar, in the API, you can change the start color and end color according to your own needs.

Please refer to this article for more details on Android ListView property

To take a look at the ListView fillet implementation, the main highlight of this article is that the data source can be flexibly set. Don't 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>

Create a new activity_main.xml to serve as the main page of our program. That is, we want to show the ListView, the layout is very easy

For example, the following:

<?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:layout_height=" Wrap_content "android:b ackground= "@drawable/round_bg" android:cachecolorhint= "#00000000" android:divider= "@color/list_divid Er "android:dividerheight=" 0.1DP "android:listselector=" #00000000 "> </ListView> <listview android:id= "@+id/list2" android:layout_width= "Fill_parent" android:layou t_height= "Wrap_content" android:layout_margintop= "15DP" android:background= "@drawable/round_bg" Android:cachecolorhint= "#00000000 "android:divider=" @color/list_divider "android:dividerheight=" 0.1DP "Android:li Stselector= "#00000000" > </ListView> <listview android:id= "@+id/list3" and Roid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "15DP" android:background= "@drawable/round_bg" android:cachecolorhint= "#00000000" Android:divid er= "@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 define 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));}}


:


Android ListView Fillet

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.