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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。