Android ListView rounded corners

Source: Internet
Author: User

First of all, let's take a look at several diagrams: (this is a weather forecast APP (moji) launched by 360, which is a good one. Here we will provide them with a free advertisement, haha .)
[Java]


This kind of listview with rounded corners looks great. It does. In fact, it cannot be said that, because there are too many square shapes, It's not worth it. "It's just a rare thing ". it's just like learning java to build androd. Obviously, in order to make more money, if the supply is oversupply, it's not so optimistic. It's like the rounded corner. If there are too many, I think it will be replaced by a new view for several time periods. therefore, it is important to "follow the trend and relax the eyeliner. no, (there are many online implementation examples) The following describes the implementation methods: (two methods)
First, we use the configuration file, that is, shape, to achieve the background of the rounded corner and selector to select a background.
First, we need to implement a complete rounded corner background, which is used by default with the background of listview.
[Java]
<? 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 will implement the shape of the background after the first selector of the listview.
[Java]
<? 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>
Next, we also need to implement the middle shape, and the end shape and so on. We will not write it here. It will be OK if we follow the above imitation.

Method 2: We use .9.png to completely replace the remaining configurations (because .9.png can be stretched without dismounting)
So the first item, intermediate item, last item, and one item of listview can all be implemented through images.
Here I use the first method: (also consolidate the shape)
Create a custom listview for setSelector and the selected result.
Code snippet:

[Java]
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 class MyListView extends ListView {
Public MyListView (Context context ){
Super (context );
}
 
Public MyListView (Context context, AttributeSet attrs ){
Super (context, attrs );
}
 
/****
* Interception of touch events
*/
@ Override
Public boolean onInterceptTouchEvent (MotionEvent ev ){
Switch (ev. getAction ()){
Case MotionEvent. ACTION_DOWN:
Int x = (int) ev. getX ();
Int y = (int) ev. getY ();
Int itemnum = pointToPosition (x, y );
If (itemnum = AdapterView. INVALID_POSITION)
Break;
Else {
If (itemnum = 0 ){
If (itemnum = (getAdapter (). getCount ()-1 )){
// Only one
SetSelector (R. drawable. list_round );
} Else {
// Item 1
SetSelector (R. drawable. list_top_round );
}
} Else if (itemnum = (getAdapter (). getCount ()-1 ))
// Last item
SetSelector (R. drawable. list_bottom_round );
Else {
// Intermediate item
SetSelector (R. drawable. list_center_round );
}
}
Break;
Case MotionEvent. ACTION_UP:
Break;
}
Return super. onInterceptTouchEvent (ev );
}
}
This code is implemented almost all over the Internet. I will briefly introduce it here. If it is wrong, please point it out,
First, we implemented the onInterceptTouchEvent method. Here we can also use the onTouchEvent event to achieve the desired effect.
The difference between onInterceptTouchEvent and onTouchEvent: In simple terms, the former can intercept the latter.
Although I have not seen the following logic, I think everyone can understand it (that is, get the coordinates (x, y), and then get the corresponding position value of listview Based on the coordinates, without returning-1, set the corresponding setSelector according to the position ). have time to study listview. it is helpful for flexible use.

Here I want to add a name. The figure above is obviously not a listview, but three listview. What's important is that one screen is not completely displayed, now we use ScrollView. When we mention this, I think everyone knows that ScrollView and listview are friends and cannot exist at the same time. However, there is a solution on the Internet. The principle is that we dynamically show our listview,
Implementation Method:
[Java]
/***
* Dynamically set the height of the listview
*
* @ Param listView
*/
Public void setListViewHeightBasedOnChildren (ListView listView ){
ListAdapter listAdapter = listView. getAdapter ();
If (listAdapter = null ){
Return;
}
Int totalHeight = 0;
For (int I = 0; I <listAdapter. getCount (); I ++ ){
View listItem = listAdapter. getView (I, null, listView );
ListItem. measure (0, 0 );
TotalHeight + = listItem. getMeasuredHeight ();
}
ViewGroup. LayoutParams params = listView. getLayoutParams ();
Params. height = totalHeight
+ (ListView. getDividerHeight () * (listAdapter. getCount ()-1 ));
// Params. height + = 5; // if without this statement, the listview will be
//
// Little short
// ListView. getDividerHeight () obtains the height occupied by separators between subitems.
// Params. height the final height required for the full display of the entire ListView
ListView. setLayoutParams (params );
}
After we call the setAdapter of Listview, this method will be okay. The Code content will not be described much if you can understand it.
:

Although it looks ugly, it can achieve the effect. If it is in the project, it will be another matter.
Haha, it's easy to implement. Go to bed.

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.