【android开发记录片】android下实现圆角列表布局控件
2013-05-24 15:08
561 查看
引子
今天闲来做了一个类似iphone的圆角列表,先看效果。图片中绿色线条的是列表头文字,红色的是列表落款文字。此两处都可以显示/隐藏及动态改变值。对于列表头还可以设置文字的位置(靠左,靠右,居中)。点击图片中的地区一行,转到下面省份选择:
关于列表行
列表中的一行默认的定义为:左边的标题(title)
右边的内容(value)
还有靠右的箭头
其中标题是一定会显示的,而“内容”如果为null,则不会显示,箭头是一个显示与否的boolean。则 CornerCell定义如下:
public class CornerCell { private String title; private String value; private boolean isArrow; private View view; public CornerCell(String title){ this(title, null, false); } public CornerCell(String title, boolean isArrow){ this(title, null, isArrow); } public CornerCell(String title, String value, boolean isArrow){ this.title = title; this.value = value; this.isArrow = isArrow; } //getter and setter @Override public String toString() { return String.format( "[CornerCell: title=%1$s, value=%2$s, isArrow=%3$s]", title, value, isArrow ); } }
圆角列表容器
CornerRowLayout 继承于 LinearLayout,并实现了OnClickListener接口。其构造方法如下:
public CornerRowLayout(Context context, AttributeSet attrs) { super(context, attrs); this.isShowValue = true; contentLy = new LinearLayout(context, attrs); contentLy.setBackgroundResource(R.drawable.shape_corner_list_background); contentLy.setOrientation(LinearLayout.VERTICAL); LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); headerTX = new TextView(getContext()); headerTX.setLayoutParams(lp); footerTX = new TextView(getContext()); footerTX.setLayoutParams(lp); footerTX.setGravity(Gravity.RIGHT); footerTX.setTextSize(13); //设置为垂直布局 this.setOrientation(LinearLayout.VERTICAL); this.addView(headerTX); this.addView(contentLy); this.addView(footerTX); }
设置列表内容
/** * 设置这个表格的数据,会直接重新渲染整个表格 * @param cells */ public void setCellList(List<CornerCell> cells){ contentLy.removeAllViews(); for(int i=0;i<cells.size();i++){ CornerCell cell = cells.get(i); //如果 CornerCell 已经有自定义的视图,就用自定义的视图 View cellView = cell.getView() == null ? View.inflate(getContext(), R.layout.nerve_corner_cell, null) : cell.getView(); if(cellView == null) continue; System.out.println(cell); /* * 对头,中,尾进行分组 */ if(i == 0) cellView.setBackgroundResource(R.drawable.shape_corner_list_top); else{ //设置顶部的margin为1,就会出现一条细线 LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); lp.setMargins(0, 1, 0, 0); cellView.setLayoutParams(lp); if(i == cells.size() - 1) cellView.setBackgroundResource(R.drawable.shape_corner_list_bottom); else cellView.setBackgroundResource(R.drawable.shape_corner_list_middle); } //设置可以点击,不然按住时不会有效果 //cellView.setClickable(true); //cellView.setPadding(5, 8, 5, 8); ((TextView)cellView.findViewById(R.id.cell_title)).setText(cell.getTitle()); if(isShowValue) ((TextView)cellView.findViewById(R.id.cell_value)).setText(cell.getValue()); cellView.findViewById(R.id.cell_arrow) .setVisibility(cell.isArrow() ? View.VISIBLE : View.GONE); cellView.setOnClickListener(this); cellView.setTag(i); //将这个view添加到本地容器 contentLy.addView(cellView); } resetAll(); }
如何使用
1.先将相关的java类导入项目,还有相关的layout,drawable,style文件2.在想加入圆角列表的页面加入以下内容:
<org.nerve.ui.corner.CornerRowLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myCornerLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" android:background="#DCDDDB" > </org.nerve.ui.corner.CornerRowLayout>
这个根据实际情况而定,如果列表内容太多,需要嵌套在一个SrollView内。
3.在Activity中:
cornerL = (CornerRowLayout)findViewById(R.id.myCornerLayout); List<CornerCell> cells = new ArrayList<CornerCell>(); cells.add(new CornerCell("姓名", "集成显卡", true)); cells.add(new CornerCell("年龄", "18岁", true)); cells.add(new CornerCell("地区", "广西壮族自治区", true)); cornerL.setCellList(cells); cornerL.setOnRowClickListener(this); cornerL.setHeader("以下信息我们会绝对保密"); cornerL.setFooter("2013-5-24");
效果就出来了。
4.Activity实现OnRowClickListenrr接口:
@Override public void onRowClick(View v, int index) { if(index == 2){ Intent intent = new Intent(ConrnerActivity.this, SelectProvinceActitivy.class); startActivityForResult(intent, PROVINCE); } }
源代码下载:http://download.csdn.net/detail/ssrc0604hx/5442505
感谢阅读
相关文章推荐
- android列表【android开发记录片】android下实现圆角列表布局控件
- 【android开发记录片】android下实现圆角列表布局控件
- Android开发之玩转FlexboxLayout布局(可用于普通控件实现流式布局,也可结合RecycleView实现流式布局)
- android开发实现列表控件滚动位置精确保存和恢复的方法(推荐)
- android 开发-ListView列表显示控件的实现
- android应用开发-从设计到实现 4-9 天气指数列表的布局
- Android开发实现布局中为控件添加选择器的方法
- Android开发实现自定义日历、日期选择控件效果
- Android开发&可实现信息滚动的控件——RollTextView
- Android开发实现Launcher3应用列表修改透明背景的方法
- Android开发实现二级联动下拉列表
- Android 组合控件实现布局的复用的方法
- Android组件式开发(2)——实现网格布局的RadioButton矩阵
- iPad开发--QQ空间,处理横竖屏布局,实现子控件中的代理
- Android开发之ListView列表刷新和加载更多实现方法
- Android开发之日历控件实现
- Android开发(三)——Android布局中实现圆角边框
- Android开发之多级下拉列表菜单实现(仿美团,淘宝等)
- Android布局实现圆角边框
- Android突击:实现屏幕四角和居中摆放控件的布局