效果图:
实现以上圆角ListView有两种方法:
一、我们先来看第一种通过配置文件来生成:
1.设置默认ListView组件背景样式:bg_list_view.xml 文件内容
<?xml version="1.0" encoding="UTF-8"?>
2.设置ListView组件第一项被点击后的样式:/res/drawable文件夹下list_top_round.xml 文件内容:
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<shape xmlns:android=
"[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]"
>
<stroke
android:width=
"1dp"
android:color=
"#c0c0c0"
/>
<solid android:color=
"#c0c0c0"
/>
<!--设置左上角 右上角 弧度为8dip-->
<corners
android:topLeftRadius=
"8dp"
android:topRightRadius=
"8dp"
/>
</shape>
接下来设置中间的列表项,模仿以上书写即可!!!
二、第二种方法:我们用.9.png完全代替上面那些配置,(因为.9.png可以拉伸不毁容)
3.复写ListView父组件中的一些方法
package com.jun.mylistview;
import com.jun.activity.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ListView;
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/****
* 拦截触摸事件
*/
@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)) {
// 只有一项
setSelector(R.drawable.list_round);
} else {
// 第一项
setSelector(R.drawable.list_top_round);
}
} else if (itemnum == (getAdapter().getCount() - 1))
// 最后一项
setSelector(R.drawable.list_bottom_round);
else {
// 中间项
setSelector(R.drawable.list_center_round);
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onInterceptTouchEvent(ev);
}
}
4.我们接着来看一下主Activity ListViewActivity.java文件的内容:
package com.jun.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListViewActivity extends Activity {
/*
* 实现圆角ListView有两种方法,下面来看通过配置文件实现圆角ListView
* */
private ListView listView=null,listView1=null,listView2=null;
private String[] data={"选项1","选项2","选项3","选项4"};
private String[] data1={"选项5","选项6"};
private int[] resId={R.drawable.img_info,R.drawable.img_info,R.drawable.img_info,R.drawable.img_info};
private int[] resId1={R.drawable.img_info,R.drawable.img_info};
private int[] resId2={R.drawable.img_info};
private List<HashMap<String,Object>> list=null;
private List<HashMap<String,Object>> list1=null;
private List<HashMap<String,Object>> list2=null;
private HashMap<String,Object> map=null,map1=null,map2=null;
private SimpleAdapter adapter=null,adapter1,adapter2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
init();
setDtata();
}
private void setDtata() {
// TODO Auto-generated method
// 使用SimpleAdapter适配器进行数据添加
for(int i=0;i<data.length;i++){
map=new HashMap<String, Object>();
map.put("text", data[i]);
map.put("img", resId[i]);
list.add(map);
}
for(int j=0;j<data1.length;j++){
map1=new HashMap<String, Object>();
map1.put("text", data1[j]);
map1.put("img", resId1[j]);
list1.add(map1);
}
adapter=new SimpleAdapter(ListViewActivity.this, list, R.layout.list_view_item, new String[]{"text","img"}, new int[]{R.id.list_view_text,R.id.list_view_img});
adapter1=new SimpleAdapter(ListViewActivity.this, list1, R.layout.list_view_item, new String[]{"text","img"}, new int[]{R.id.list_view_text,R.id.list_view_img});
listView.setAdapter(adapter);
listView1.setAdapter(adapter1);
}
private void init(){
listView=(ListView) findViewById(R.id.my_list_view);
listView1=(ListView) findViewById(R.id.my_list_view1);
list=new ArrayList<HashMap<String,Object>>();
list1=new ArrayList<HashMap<String,Object>>();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
5.主Activity 布局文件 activity_list_view.xml文件内容:
<RelativeLayout 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:background="@drawable/activity_background" >
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:background="@drawable/theme_title_red"
android:gravity="center_vertical|center_horizontal"
android:text="@string/title_listview_activity"
android:textColor="#fff"
android:textSize="20dp"
tools:context=".ListViewActivity" />
<com.jun.mylistview.MyListView
android:id="@+id/my_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_list_view"
android:layout_below="@+id/title_text"
android:cacheColorHint="#00000000"
android:layout_marginTop="30dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:divider="#ff0000"
android:dividerHeight="1.0dp"
/>
<com.jun.mylistview.MyListView
android:id="@+id/my_list_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_list_view"
android:layout_below="@+id/my_list_view"
android:cacheColorHint="#00000000"
android:layout_marginTop="30dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:divider="#ff0000"
android:dividerHeight="1.0dp"
/>
</RelativeLayout>
到此介绍完毕!以上圆角ListView就是如此实现!!!!