Android ListView for Rounded corners

Source: Internet
Author: User

First of all, let's look at a few charts:

This fillet of the ListView ' looks great, it is true, actually can not say so, the main square too much, stone see used to be worthless, "things to dilute for you." Like learning Java are androd, it is obvious, in order to earn more money, but oversupply of words, it is not so optimistic, like now this fillet, if too much, I think some time period, will certainly be replaced by the new view. So it's important to "follow the trend and relax the eyeliner." (Online implementation of a lot of examples) below a brief introduction to the implementation method: (two methods)

The first: we use the configuration file, shape itself, to achieve the background of the fillet, and selector Select the background of an item.

First we will implement a full rounded background for the default background with the ListView.

<?xml version="1.0"encoding="Utf-8"? ><shape xmlns:android="http://schemas.android.com/apk/res/android"> <Stroke Android:width="1DP"Android:color="@color/gray"/> <solid android:color="@color/white"/> <corners android:radius="8DP"/> </shape>

Next we want to implement shape that is the background of the first selector of the ListView.

<?xml version="1.0"encoding="Utf-8"? ><shape xmlns:android="http://schemas.android.com/apk/res/android"> <Stroke Android:width="1DP"Android:color="@color/gray"/> <solid android:color="@color/gray"/> <Corners Android:topleftradius="8DP"Android:toprightradius="8DP"/> </shape>

Below, we also want to implement the middle shape, as well as the shape at the end of the configuration, and so on. This is not the case. Follow the above imitation on the OK.
The second method: we use the. 9.png completely instead of those configurations above, (because. 9.png can be stretched without disfigurement )

So the first item in the ListView, the middle item, the last item, and on the one, we can all get through the picture.

Here I use the first method: (also solidify under shape)

Create a custom ListView that implements Setselector and selected effects.

Code snippet:

Package Com.jj.listview; import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.widget.adapterview;import Android.widget.ListView; /** * Custom ListView * * @author Administrator **/ Public classMylistview extends ListView { PublicMylistview (Context context) {super (context); }      PublicMylistview (Context context, AttributeSet Attrs) {Super (context, attrs); }     /** * * intercept Touch Events*/@Override Publicboolean onintercepttouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionevent.action_down:intx = (int) Ev.getx (); inty = (int) ev.gety (); intItemnum =pointtoposition (x, y); if(Itemnum = =adapterview.invalid_position) Break; Else {                if(Itemnum = =0) {                    if(Itemnum = = (Getadapter (). GetCount ()-1)) {                        //There is only one itemSetselector (R.drawable.list_round); } Else {                        //First ItemSetselector (R.drawable.list_top_round); }                } Else if(Itemnum = = (Getadapter (). GetCount ()-1))                    //the last oneSetselector (R.drawable.list_bottom_round); Else {                    //Middle ItemSetselector (R.drawable.list_center_round); }            }             Break;  Casemotionevent.action_up: Break; }        returnsuper.onintercepttouchevent (EV); }}

This code a lot of online, almost all of this is achieved, here I briefly introduce, if wrong, please point out,

First we have implemented the Onintercepttouchevent method, where we can also use the Ontouchevent event, can achieve the effect we want.

The difference between onintercepttouchevent and ontouchevent: In simple terms, the former can intercept the latter.

Detailed please refer to http://blog.csdn.net/ddna/article/details/5473293 strongly recommend everyone to see.

Some of the following logic, although not seen, I think we can understand, (is to get coordinates (x, y), and then according to the coordinates to get the corresponding position value of the ListView, no Return-1, and then according to the corresponding position set corresponding setselector ). Have time to study the ListView. It is very helpful to use it flexibly .

Here I would like to say the name, the graph above is obviously not a ListView, is three ListView, the other important is that we have a screen display is not complete, then we use the ScrollView, a mention of this I think we all know ScrollView and ListView is a friend, can not exist at the same time, but there is a solution on the Internet, the principle is that we show our ListView dynamic,

Implementation method:

***     *dynamically set the height of the ListView*     *@param listView*/ Public voidSetlistviewheightbasedonchildren (ListView listview) {ListAdapter ListAdapter=Listview.getadapter (); if(ListAdapter = =NULL) {            return; }        intTotalheight =0;  for(inti =0; I < Listadapter.getcount (); i++) {View ListItem= Listadapter.getview (I,NULL, ListView); Listitem.measure (0,0); Totalheight+=listitem.getmeasuredheight (); } viewgroup.layoutparamsparams=Listview.getlayoutparams (); params. Height =Totalheight+ (Listview.getdividerheight () * (Listadapter.getcount ()-1)); //Params.height + = 5;//if without this statement,the listview would be//a//Little Short//Listview.getdividerheight () Gets the height of the delimiter between children//Params.height finally get the entire ListView full display required heightListview.setlayoutparams (params); }

We in the ListView after the Setadapter, in the call this method is OK, the code content, I think we all see understand, not more introduced.

Looks ugly, but the effect is OK, if the project is another matter

Android ListView for Rounded corners

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.