Android 中怎么使用ListView控件实现圆角效果,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
成都创新互联公司是一家专业提供泾源企业网站建设,专注与成都网站建设、网站制作、H5高端网站建设、小程序制作等业务。10年已为泾源众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
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控件实现圆角效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
文章标题:Android中怎么使用ListView控件实现圆角效果
标题网址:https://www.cdcxhl.com/article48/gghhhp.html
成都网站建设公司_创新互联,为您提供微信公众号、网站设计、网站导航、网站建设、小程序开发、网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联