您好,登錄后才能下訂單哦!
Android 中怎么使用ListView控件實現圓角效果,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
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)) { // 只有一項數據,設置背景設置為圓角的 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.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); } /** * 設置第一列數據 */ private SimpleAdapter getSimpleAdapter_1() { listData = new ArrayList<Map<String, String>>(); Map<String, String> map = new HashMap<String, String>(); map.put("text", "天氣動畫"); 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 }); } /** * 設置第二列數據 */ 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", "定時播報"); listData2.add(map); return new SimpleAdapter(MainActivity.this, listData2, R.layout.list_item, new String[] { "text" }, new int[] { R.id.tv_list_item }); } /** * 設置第三列數據 */ 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", "發送建議"); listData3.add(map); map = new HashMap<String, String>(); map.put("text", "幫助"); listData3.add(map); map = new HashMap<String, String>(); map.put("text", "關于"); listData3.add(map); return new SimpleAdapter(MainActivity.this, listData3, R.layout.list_item, new String[] { "text" }, new int[] { R.id.tv_list_item }); } /*** * 動態設置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()獲取子項間分隔符占用的高度 // params.height最后得到整個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(); } } }
差不多也就是這樣了,還有相關資源文件。
看完上述內容,你們掌握Android 中怎么使用ListView控件實現圓角效果的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。