Android UI控件之ListView實(shí)現(xiàn)圓角效果
今天在Android群里面有人再求圓角ListView的實(shí)現(xiàn)方式,正好自己以前實(shí)現(xiàn)過。因此就共享了現(xiàn)在將其實(shí)現(xiàn)方式寫在博客中共他人學(xué)習(xí)。給出實(shí)現(xiàn)方式之前順帶加點(diǎn)自己的想法,感覺上android中方形的ListView還是太“硬性”,沒有圓角的有親和力。連Apple也為了“圓角”這個(gè)設(shè)計(jì)去申請(qǐng)專利。
看來圓角確實(shí)比較適合現(xiàn)在人們的喜好吧。
照老規(guī)矩先上兩張效果圖吧:
第一張:

第二張:

該方式主要就是需要重新去實(shí)現(xiàn)自己的ListView代碼如下:
package com.kiritor.corner_listview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ListView;
/***
* 自定義listview
*
* @author Kiritor
*
*/
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/****
* 攔截觸摸事件
*/
@Override
public boolean onTouchEvent(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)) {
// 只有一項(xiàng)數(shù)據(jù),設(shè)置背景設(shè)置為圓角的
setSelector(R.drawable.list_round);
} else {
// 第一項(xiàng),設(shè)置為上面為圓角的
setSelector(R.drawable.list_top_round);
}
} else if (itemnum == (getAdapter().getCount() - 1))
// 最后一項(xiàng),設(shè)置為下面為圓角的
setSelector(R.drawable.list_bottom_round);
else {
// 中間項(xiàng),不用設(shè)置為圓角
setSelector(R.drawable.list_center_round);
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(ev);
}
}
MainActivity
package com.kiritor.corner_listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private MyListView listView_1, listView_2, listView_3;
private ArrayList<Map<String, String>> listData, listData2, listData3;
private SimpleAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
listView_1 = (MyListView) findViewById(R.id.mylistview_1);
listView_2 = (MyListView) findViewById(R.id.mylistview_2);
listView_3 = (MyListView) findViewById(R.id.mylistview_3);
listView_1.setAdapter(getSimpleAdapter_1());
listView_2.setAdapter(getSimpleAdapter_2());
listView_3.setAdapter(getSimpleAdapter_3());
listView_1.setOnItemClickListener(this);
listView_2.setOnItemClickListener(this);
listView_3.setOnItemClickListener(this);
setListViewHeightBasedOnChildren(listView_1);
setListViewHeightBasedOnChildren(listView_2);
setListViewHeightBasedOnChildren(listView_3);
}
/**
* 設(shè)置第一列數(shù)據(jù)
*/
private SimpleAdapter getSimpleAdapter_1() {
listData = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("text", "天氣動(dòng)畫");
listData.add(map);
map = new HashMap<String, String>();
map.put("text", "通知欄天氣");
listData.add(map);
return new SimpleAdapter(MainActivity.this, listData,
R.layout.list_item, new String[] { "text" },
new int[] { R.id.tv_list_item });
}
/**
* 設(shè)置第二列數(shù)據(jù)
*/
private SimpleAdapter getSimpleAdapter_2() {
listData2 = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("text", "桌面插件");
listData2.add(map);
map = new HashMap<String, String>();
map.put("text", "綁定微博");
listData2.add(map);
map = new HashMap<String, String>();
map.put("text", "天氣分享");
listData2.add(map);
map = new HashMap<String, String>();
map.put("text", "通知與提示");
listData2.add(map);
map = new HashMap<String, String>();
map.put("text", "定時(shí)播報(bào)");
listData2.add(map);
return new SimpleAdapter(MainActivity.this, listData2,
R.layout.list_item, new String[] { "text" },
new int[] { R.id.tv_list_item });
}
/**
* 設(shè)置第三列數(shù)據(jù)
*/
private SimpleAdapter getSimpleAdapter_3() {
listData3 = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("text", "檢查新版本");
listData3.add(map);
map = new HashMap<String, String>();
map.put("text", "發(fā)送建議");
listData3.add(map);
map = new HashMap<String, String>();
map.put("text", "幫助");
listData3.add(map);
map = new HashMap<String, String>();
map.put("text", "關(guān)于");
listData3.add(map);
return new SimpleAdapter(MainActivity.this, listData3,
R.layout.list_item, new String[] { "text" },
new int[] { R.id.tv_list_item });
}
/***
* 動(dòng)態(tài)設(shè)置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
// a
// little short
// listView.getDividerHeight()獲取子項(xiàng)間分隔符占用的高度
// params.height最后得到整個(gè)ListView完整顯示需要的高度
listView.setLayoutParams(params);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (parent == listView_1) {
Map<String, String> map = listData.get(position);
Toast.makeText(MainActivity.this, map.get("text"), 1).show();
} else if (parent == listView_2) {
Map<String, String> map = listData2.get(position);
Toast.makeText(MainActivity.this, map.get("text"), 1).show();
} else if (parent == listView_3) {
Map<String, String> map = listData3.get(position);
Toast.makeText(MainActivity.this, map.get("text"), 1).show();
}
}
}
差不多也就是這樣了,還有相關(guān)資源文件。

這里就不一一給出了。
完整源碼部分:ListView實(shí)現(xiàn)圓角效果
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter?將Dio請(qǐng)求轉(zhuǎn)發(fā)原生網(wǎng)絡(luò)庫的實(shí)現(xiàn)方案
這篇文章主要介紹了Flutter?將Dio請(qǐng)求轉(zhuǎn)發(fā)原生網(wǎng)絡(luò)庫,需要注意添加NativeNetInterceptor,如果有多個(gè)攔截器,例如LogInterceptors等等,需要將NativeNetInterceptor放到最后,需要的朋友可以參考下2022-05-05
Android編程之動(dòng)態(tài)壁紙實(shí)例分析
這篇文章主要介紹了Android編程之動(dòng)態(tài)壁紙實(shí)現(xiàn)方法,以實(shí)例形式分析了Android動(dòng)態(tài)壁紙的原理與實(shí)現(xiàn)步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法
這篇文章主要介紹了Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01
Android 文件分段上傳和下載實(shí)現(xiàn)方案
文章介紹了Android大文件下載和上傳的分段處理方法,在下載時(shí),使用多線程并發(fā)請(qǐng)求文件的多個(gè)段,并將這些段寫入同一個(gè)文件,在上傳時(shí),根據(jù)文件大小分段上傳,每段文件的大小由預(yù)設(shè)閾值決定,感興趣的朋友跟隨小編一起看看吧2024-11-11

