android列表【android开发记录片】android下实现圆角列表布局控件
每日一貼,明天的內(nèi)容關(guān)鍵字為android列表
????
引子
????明天閑來做了一個(gè)類似iphone的圓角列表,先看效果。
????
????圖片中綠色線條的是列表頭文字,紅色的是列表題名文字。此兩處都可以顯示/隱藏及動(dòng)態(tài)改變值。對(duì)于列表頭還可以設(shè)置文字的位置(靠左,靠右,居中)。點(diǎn)擊圖片中的地域一行,轉(zhuǎn)到下面省分選擇:
????
????
????
關(guān)于列表行
????列表中的一行默許的定義為:
? ? 左邊的標(biāo)題(title)
? ? 右側(cè)的內(nèi)容(value)
? ? 還有靠右的箭頭
????其中標(biāo)題是一定會(huì)顯示的,而“內(nèi)容”如果為null,則不會(huì)顯示,箭頭是一個(gè)顯示與否的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@Overridepublic String toString() {return String.format("[CornerCell: title=%1$s, value=%2$s, isArrow=%3$s]", title, value, isArrow);} }????
圓角列表容器
????CornerRowLayout 繼承于 LinearLayout,并實(shí)現(xiàn)了OnClickListener接口。
????其構(gòu)造方法如下:
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);//設(shè)置為垂直布局this.setOrientation(LinearLayout.VERTICAL);this.addView(headerTX);this.addView(contentLy);this.addView(footerTX); }????
????
設(shè)置列表內(nèi)容
每日一道理微笑,是春天里的一絲新綠,是秋日里的一縷陽光,是驕陽下的一片濃蔭,是冬雪中的一株梅紅……微笑著去面對(duì)吧,你會(huì)感到人生是那樣的溫馨與甜蜜!
/*** 設(shè)置這個(gè)表格的數(shù)據(jù),會(huì)直接重新渲染全部表格* @param cells*/ public void setCellList(List<CornerCell> cells){contentLy.removeAllViews();for(int i=0;i<cells.size();i++){CornerCell cell = cells.get(i);//如果 CornerCell 已經(jīng)有自定義的視圖,就用自定義的視圖View cellView = cell.getView() == null ?View.inflate(getContext(), R.layout.nerve_corner_cell, null):cell.getView();if(cellView == null)continue;System.out.println(cell);/** 對(duì)頭,中,尾進(jìn)行分組*/if(i == 0)cellView.setBackgroundResource(R.drawable.shape_corner_list_top);else{//設(shè)置頂部的margin為1,就會(huì)涌現(xiàn)一條細(xì)線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);elsecellView.setBackgroundResource(R.drawable.shape_corner_list_middle);}//設(shè)置可以點(diǎn)擊,不然按住時(shí)不會(huì)有效果//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);//將這個(gè)view添加到本地容器contentLy.addView(cellView);}resetAll(); }
????
如何使用
????1.先將相關(guān)的java類導(dǎo)入項(xiàng)目,還有相關(guān)的layout,drawable,style文件
????
????2.在想?yún)⒓訄A角列表的頁面參加以下內(nèi)容:
<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>????這個(gè)根據(jù)實(shí)際情況而定,如果列表內(nèi)容太多,需要嵌套在一個(gè)SrollView內(nèi)。
????
????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("地域", "廣西壯族自治區(qū)", true));cornerL.setCellList(cells); cornerL.setOnRowClickListener(this);cornerL.setHeader("以下信息我們會(huì)絕對(duì)保密"); cornerL.setFooter("2013-5-24");????效果就出來了。
????
????4.Activity實(shí)現(xiàn)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
????
????感激閱讀
????
????
????
????
????
文章結(jié)束給大家分享下程序員的一些笑話語錄: 問答
Q:你是怎么區(qū)分一個(gè)內(nèi)向的程序員和一個(gè)外向的程序員的? A:外向的程序員會(huì)看著你的鞋和你說話時(shí)。
Q:為什么程序員不能區(qū)分萬圣節(jié)和圣誕節(jié)? A:這是因?yàn)?Oct 31 == Dec 25!(八進(jìn)制的 31==十進(jìn)制的 25)
--------------------------------- 原創(chuàng)文章 By
android和列表
---------------------------------
總結(jié)
以上是生活随笔為你收集整理的android列表【android开发记录片】android下实现圆角列表布局控件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3月1日发布!vivo V27系列实拍曝
- 下一篇: 又要说离开了!