您的位置:首页 > 移动开发 > Android开发

Android ListView圆角列表完整(附源码)

2012-05-25 18:12 441 查看
自己很少写文章,今天突然想做个记录,也是给自己做个笔记.

为应用项目需求,需要做图下效果,找了一下资料,自己在重新写一下,变Ok了,看效果图片再上代码:

主布局文件more_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/setting_container"
style="@style/Global_Bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:id="@+id/_top_box"
android:layout_width="fill_parent"
android:layout_height="@dimen/top_box"
android:layout_alignParentTop="true" >

<include
android:id="@+id/inc_top_box"
layout="@layout/top_box" />
</LinearLayout>

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_below="@+id/_top_box" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:id="@+id/tv_state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="15dp"
android:text="状态设置"
android:textColor="@color/black"/>
<cn.fengwoo.core.CornerListView
android:id="@+id/lv_state"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/shape_bg_listview"
android:scrollbars="none"
android:cacheColorHint="@null"/>

<TextView
android:id="@+id/tv_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="15dp"
android:text="帐号管理"
android:textColor="@color/black"/>
<cn.fengwoo.core.CornerListView
android:id="@+id/lv_account"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/shape_bg_listview"
android:scrollbars="none"
android:cacheColorHint="@null"/>

<TextView
android:id="@+id/tv_system"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="15dp"
android:text="系统帮助"
android:textColor="@color/black"/>
<cn.fengwoo.core.CornerListView
android:id="@+id/lv_system"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/shape_bg_listview"
android:scrollbars="none"
android:cacheColorHint="@null"/>

<cn.fengwoo.core.CornerListView
android:id="@+id/lv_exit"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="20dp"
android:background="@drawable/shape_bg_listview"
android:scrollbars="none"
android:cacheColorHint="@null"/>

</LinearLayout>
</ScrollView>

</RelativeLayout>


副布局文件(也就上面四个listview的item)

state_item.xml

<?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="match_parent">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_horizontal">

<TextView
android:id="@+id/tv_state_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="隐身模式"
android:textColor="@color/black"/>

<TextView
android:id="@+id/tv_state_context"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/iv_state_right"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="对所有人可见" />

<ImageView
android:id="@+id/iv_state_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/arrows_right" />
</RelativeLayout>

</LinearLayout>


account_item.xml

<?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="match_parent">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_horizontal">

<TextView
android:id="@+id/tv_account_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="修改密码"
android:textColor="@color/black"/>

<ImageView
android:id="@+id/iv_account_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/arrows_right" />
</RelativeLayout>

</LinearLayout>


system_item.xml

<?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="match_parent">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_horizontal">

<TextView
android:id="@+id/tv_system_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="分享给好友"
android:textColor="@color/black"/>

<ImageView
android:id="@+id/iv_system_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/arrows_right" />
</RelativeLayout>

</LinearLayout>


exit_item.xml

<?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="match_parent">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_horizontal">

<TextView
android:id="@+id/tv_exit_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="注销"
android:textColor="@color/red"
android:textStyle="bold"/>

<ImageView
android:id="@+id/iv_exit_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/arrows_right" />
</RelativeLayout>

</LinearLayout>


布局基本就这些,下面看绘制圆角布局文件,总共有四个

上面左右圆角

app_list_corner_round_top.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
<corners android:topLeftRadius="6dip"
android:topRightRadius="6dip"/>
</shape>


中部平角

app_list_corner_round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
<corners android:topLeftRadius="6dip"
android:topRightRadius="6dip"
android:bottomLeftRadius="6dip"
android:bottomRightRadius="6dip"/>
</shape>


下面左右圆角

app_list_corner_round_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
<corners android:bottomLeftRadius="6dip"
android:bottomRightRadius="6dip" />
</shape>


上面是控制listview中item效果

下面是要改变listview中item的背景或边框颜色

shape_bg_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
android:angle="180"
android:endColor="#FFCCCCCC"
android:startColor="@android:color/white" />

<stroke
android:width="1px"
android:color="@android:color/transparent" />

<solid android:color="@android:color/background_light" />

<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />

</shape>


再就自定义listview圆角的类

CornerListView.java

/**
* 圆角ListView示例
* @Description: 圆角ListView
* @FileName: CornerListView.java
* @author XIAJUN
* @Date 2012-5-25 下午18:05:19
* @Version V1.0
*/
public class CornerListView extends ListView {
public CornerListView(Context context) {
super(context);
}

public CornerListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public CornerListView(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.app_list_corner_round);
}else{
setSelector(R.drawable.app_list_corner_round_top);
}
}else if(itemnum==(getAdapter().getCount()-1))
setSelector(R.drawable.app_list_corner_round_bottom);
else{
setSelector(R.drawable.app_list_corner_shape);
}
}

break;
case MotionEvent.ACTION_UP:
break;
}

return super.onInterceptTouchEvent(ev);
}
}


最后看一下Activity的代码:

SettingActivity.java

public class SettingActivity extends Activity{

private CornerListView lvState,lvAccount,lvSystem,lvExit;
private SimpleAdapter stateAdapter,accountAdapter,systemAdapter,exitAdapter;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getStateData();
getAccountData();
getSystemData();
getExitData();
initView();
initAdapter();

}

private void initView(){
setContentView(R.layout.more_settings);
lvState=(CornerListView)findViewById(R.id.lv_state);
lvAccount=(CornerListView)findViewById(R.id.lv_account);
lvSystem=(CornerListView)findViewById(R.id.lv_system);
lvExit=(CornerListView)findViewById(R.id.lv_exit);
lvState.setOnItemClickListener(new StateOnItemListSelectedListener());
lvAccount.setOnItemClickListener(new AccountOnItemListSelectedListener());
lvSystem.setOnItemClickListener(new SystemOnItemListSelectedListener());
lvExit.setOnItemClickListener(new ExitOnItemListSelectedListener());
}

private void initAdapter(){
stateAdapter = new SimpleAdapter(this,getStateData(),R.layout.state_item,
new String[]{"state_title","state_context","state_right"},
new int[]{R.id.tv_state_title,R.id.tv_state_context,R.id.iv_state_right});

lvState.setAdapter(stateAdapter);

accountAdapter = new SimpleAdapter(this,getAccountData(),R.layout.account_item,
new String[]{"account_title","account_right"},
new int[]{R.id.tv_account_title,R.id.iv_account_right});

lvAccount.setAdapter(accountAdapter);

systemAdapter = new SimpleAdapter(this,getSystemData(),R.layout.system_item,
new String[]{"system_title","system_right"},
new int[]{R.id.tv_system_title,R.id.iv_system_right});

lvSystem.setAdapter(systemAdapter);

exitAdapter = new SimpleAdapter(this,getExitData(),R.layout.exit_item,
new String[]{"exit_title","exit_right"},
new int[]{R.id.tv_exit_title,R.id.iv_exit_right});

lvExit.setAdapter(exitAdapter);
}

private class StateOnItemListSelectedListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:

break;
case 1:

break;
case 2:

break;
case 3:

break;
default:
break;
}
}
}

private class AccountOnItemListSelectedListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:

break;
case 1:

break;
default:
break;
}

}
}

private class SystemOnItemListSelectedListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:

break;
case 1:

break;
case 2:

break;
case 3:

break;
case 4:

break;
case 5:

break;
case 6:

break;
default:
break;
}
}
}

private class ExitOnItemListSelectedListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:

break;
case 1:

break;
default:
break;
}
}
}

private List<Map<String, Object>> getStateData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

Map<String, Object> map = new HashMap<String, Object>();
map.put("state_title", "隐身模式");
map.put("state_context", "对所有人可见");
map.put("state_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("state_title", "消息提醒");
map.put("state_context", "");
map.put("state_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("state_title", "静音时段");
map.put("state_context", "关闭");
map.put("state_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("state_title", "定位服务");
map.put("state_context", "");
map.put("state_right", R.drawable.arrows_right);
list.add(map);
return list;
}

private List<Map<String, Object>> getAccountData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

Map<String, Object> map = new HashMap<String, Object>();
map.put("account_title", "修改密码");
map.put("account_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("account_title", "黑名单");
map.put("account_right", R.drawable.arrows_right);
list.add(map);
return list;
}

private List<Map<String, Object>> getSystemData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

Map<String, Object> map = new HashMap<String, Object>();
map.put("system_title", "分享给好友");
map.put("system_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("system_title", "意见反馈");
map.put("system_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("system_title", "版本信息");
map.put("system_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("system_title", "系统设置");
map.put("system_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("system_title", "检查更新");
map.put("system_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("system_title", "给xx评分");
map.put("system_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("system_title", "关于xx");
map.put("system_right", R.drawable.arrows_right);
list.add(map);
return list;
}

private List<Map<String, Object>> getExitData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

Map<String, Object> map = new HashMap<String, Object>();
map.put("exit_title", "注销");
map.put("exit_right", R.drawable.arrows_right);
list.add(map);

map = new HashMap<String, Object>();
map.put("exit_title", "退出");
map.put("exit_right", R.drawable.arrows_right);
list.add(map);
return list;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Intent intent=new Intent(SettingActivity.this, IndexActivity.class);
startActivity(intent);
return super.onKeyDown(keyCode, event);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐