android列表【android开发记录片】android下实现圆角列表布局控件
每日一貼,明天的內容關鍵字為android列表
????
引子
????明天閑來做了一個類似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@Overridepublic 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);elsecellView.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
????
????感激閱讀
????
????
????
????
????
文章結束給大家分享下程序員的一些笑話語錄: 問答
Q:你是怎么區分一個內向的程序員和一個外向的程序員的? A:外向的程序員會看著你的鞋和你說話時。
Q:為什么程序員不能區分萬圣節和圣誕節? A:這是因為 Oct 31 == Dec 25!(八進制的 31==十進制的 25)
--------------------------------- 原創文章 By
android和列表
---------------------------------
總結
以上是生活随笔為你收集整理的android列表【android开发记录片】android下实现圆角列表布局控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3月1日发布!vivo V27系列实拍曝
- 下一篇: 又要说离开了!