在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过ArrayAdapter来使ListView显示和刷新内容。
假定现在有一String类型的ArrayList,叫myArrayList,建立ArrayAdapter并将其与myArrayList绑定的代码如下:
1
2
|
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>( this , android.layout.simple_list_item_1, myArrayList); |
其中android.layout.simple_list_item_1是android本身的一个基本listview,在实际中也可以自建一个listview。
当有新的内容时,先将String添加到myArrayList,然后通过以下代码完成ListView的刷新显示:
1
2
|
myArrayList.add( 0 , myString); myArrayAdapter.notifyDataSetChanged(); |
上面add方法的第一个参数是新String要添加的位置,从0开始一次递增。notifyDataSetChanged()的作用是告知ListView刷新内容。
在实际中,经常需要定制ListView,先要为所需的页面、边缘等需要的颜色在colors.xml文件中进行设置。并为页面宽度和页面边缘在dimens.xml中添加所需要的值。
然后需要扩展一个新的TextView类,用作ListView中每一行的显示,在init方法中创建获取前面创立的资源文件,并建立Paint对象,然后重写onDraw方法,利用Paint对象来重写图像。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import android.content.Context; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.widget.TextView; public class WordItemView extends TextView{ private Paint marginPaint; private Paint linePaint; private int paperColor; private float margin; //WordItemView的构造函数 public WordItemView(Context context, AttributeSet ats, int ds){ super (context, ats, ds); init(); } public WordItemView(Context context){ super (context); init(); } public WordItemView(Context context, AttributeSet ats){ super (context, ats); init(); } private void init(){ Resources myResources = getResources(); //创建画刷 marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG); marginPaint.setColor(myResources.getColor(R.color.margin)); linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); linePaint.setColor(myResources.getColor(R.color.lines)); //获得页面背景色和边缘宽度 paperColor = myResources.getColor(R.color.paper); margin = myResources.getDimension(R.dimen.margin); } @Override public void onDraw(Canvas canvas){ //绘制页面颜色 canvas.drawColor(paperColor); //绘制边缘 //canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint); canvas.drawLine( 0 , getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint); canvas.drawLine(margin, 0 , margin, getMeasuredHeight(), marginPaint); //移动文本 canvas.save(); canvas.translate(margin, 0 ); //渲染文本 super .onDraw(canvas); canvas.restore(); } } |
接下来在res/layout中新建一个xml文件来指定每一个条目在视图列表中的排列方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:id = "@+id/itemMean" android:layout_height = "match_parent" android:layout_width = "wrap_content" android:layout_alignParentRight = "true" android:textColor = "@color/text" android:padding = "10dp" android:scrollbars = "vertical" android:fadingEdge = "vertical" /> < com.qingshuimonk.words.WordItemView android:id = "@+id/itemWord" android:layout_width = "match_parent" android:layout_height = "match_parent" android:padding = "10dp" android:scrollbars = "vertical" android:textColor = "@color/text" android:textStyle = "italic" android:fadingEdge = "vertical" /> </ RelativeLayout > |
重写ArrayAdapter方法使其适应现有的空间,在这个例子(一个能显示单词和释义的应用)里,有两个TextView需要显示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; public class MyAdapter extends ArrayAdapter<WordItem>{ int resource; public MyAdapter(Context context, int _resource, List<WordItem> items){ super (context, _resource, items); resource = _resource; } @Override public View getView( int position, View convertView, ViewGroup parent){ LinearLayout newView; WordItem item = getItem(position); String word = item.getWord(); String mean = item.getMean(); if (convertView == null ){ newView = new LinearLayout(getContext()); String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater li; li = (LayoutInflater)getContext().getSystemService(inflater); li.inflate(resource, newView, true ); } else { newView = (LinearLayout)convertView; } TextView wordView = (TextView)newView.findViewById(R.id.itemWord); TextView meanView = (TextView)newView.findViewById(R.id.itemMean); wordView.setText(word); meanView.setText(mean); return newView; } } |
最后在MainActivity里面对ArrayList和ArrayAdapter的绑定代码进行修改。
1
2
3
4
|
final ArrayList<WordItem> worditem = new ArrayList<WordItem>(); final MyAdapter adapter = new MyAdapter( this , R.layout.worditem, worditem); wordsList.setAdapter(adapter); |
这样定制的ListView就大功告成了。