listview在我们android项目中的地位是有目共睹的,相信几乎每一个app中都有它的身影。
listview主要是用列表形式来加载数据,在特定情况下需要实现一些特殊功能:如刷新数据,加载数据,实现动画效果等。
作为我们常用的控件,有哪些需要注意的呢?
**为listview的每一item设置分隔线
第一种方法:也是最简单地方法,在布局文件中设置listview的
divider属性
如:android:divider=”@color/black”
第二种方法:设置android:divider=”@null”表示不要分隔线,然后,自己在item布局中添加分隔线。
1
2
3
4
5
6
7
8
|
<listview>
android:id= "@+id/test_lv"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:divider= "#d1d1d1" <!--设置分隔线颜色-->
android:dividerheight= "1px" <!--设置分隔线高度-->
>
</listview>
|
** 默认情况下,listview的item有点击效果,怎么样改变这种效果呢?
设置listviewr的android:listselector属性,比如可以设置透明取消这种效果android:listselector=”@android:color/transparent”
也可以设置自己想要的效果,添加对应的color资源或drawble资源 就可以了。
** 当数据比较多的时候,listview在滚动时候会显示一个默认的滚动条,要取消这个滚动条的话,可以设置android:scrollbars属性
如:android:scrollbars=”none”
** listview性能优化,必用viewholder来充分发挥listview的recycle机制
** listview中数据动态变化,添加或删除数据操作,效果显而易见。
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
//实现listview数据添加删除
public class mainactivity extends activity implements view.onclicklistener {
private listview test_lv;
private list<testbean> datalist;
private testadapter adapter;
private button add_btn, del_btn;
private imageview emptyiv;
private linearlayout operator_ll;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
initdatas();
initviews();
}
private void initdatas() {
datalist = new arraylist<>();
for ( int i = 0 ; i < 5 ; i++) {
testbean bean = new testbean();
bean.settitle( "标题_" + i);
bean.setcontent( "这是内容_" + i);
bean.settype(i % 2 == 0 ? 1 : 2 );
datalist.add(bean);
}
}
private void initviews() {
this .test_lv = (listview) findviewbyid(r.id.test_lv);
this .add_btn = (button) findviewbyid(r.id.add_btn);
this .del_btn = (button) findviewbyid(r.id.del_btn);
emptyiv = (imageview) findviewbyid(r.id.empty_iv);
operator_ll=(linearlayout)findviewbyid(r.id.operator_ll);
this .add_btn.setonclicklistener( this );
this .del_btn.setonclicklistener( this );
if (datalist.size() == 0 ) {
emptyiv.setvisibility(view.visible);
operator_ll.setvisibility(view.gone);
test_lv.setemptyview(emptyiv);
} else {
emptyiv.setvisibility(view.gone);
operator_ll.setvisibility(view.visible);
adapter = new testadapter( this , datalist);
test_lv.setadapter(adapter);
}
test_lv.setselection( 15 );
}
@override
public void onclick(view v) {
switch (v.getid()) {
case r.id.add_btn:
//默认增加在第一个位置
testbean bean = new testbean();
bean.settitle( "增加项" );
bean.setcontent( "这是增加内容" );
bean.settype( 1 );
datalist.add( 0 , bean);
break ;
case r.id.del_btn:
//默认删除第一条数据
datalist.remove( 0 );
break ;
}
adapter.notifydatasetchanged(); //刷新listview数据
}
}
//listview对应adapter
public class testadapter extends baseadapter {
private context mcontext;
private list<testbean> listdatas;
public testadapter(context mcontext, list<testbean> listdatas) {
this .mcontext = mcontext;
this .listdatas = listdatas;
}
@override
public int getcount() {
return listdatas.size();
}
@override
public object getitem( int position) {
return listdatas.get(position);
}
@override
public long getitemid( int position) {
return position;
}
@override
public view getview( int position, view convertview, viewgroup parent) {
viewholder holder = null ;
if (convertview == null ) {
holder = new viewholder();
convertview = layoutinflater.from(mcontext).inflate(r.layout.test_item, null );
holder.title = (textview) convertview.findviewbyid(r.id.item_title);
holder.content = (textview) convertview.findviewbyid(r.id.item_content);
convertview.settag(holder);
} else {
holder = (viewholder) convertview.gettag();
}
holder.content.settext(listdatas.get(position).getcontent());
holder.title.settext(listdatas.get(position).gettitle());
return convertview;
}
final class viewholder {
textview title;
textview content;
}
}
|
** 类似聊天界面,listview中有多种item样式效果
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
//实现listview中多种item样式
public class typeactivity extends activity {
private listview type_lv;
private list<testbean> datalist;
private typeadapter adapter;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_type);
initdatas();
initviews();
}
private void initdatas() {
datalist = new arraylist<>();
for ( int i = 0 ; i < 9 ; i++) {
testbean bean = new testbean();
bean.settitle( "标题_" + i);
bean.setcontent( "这是内容_" + i);
bean.settype(i % 2 == 0 ? 1 : 2 );
datalist.add(bean);
}
}
private void initviews() {
this .type_lv = (listview) findviewbyid(r.id.type_lv);
adapter = new typeadapter( this , datalist);
type_lv.setadapter(adapter);
}
}
//对应adapter
public class typeadapter extends baseadapter {
private context mcontext;
private list<testbean> listdatas;
public typeadapter(context mcontext, list<testbean> listdatas) {
this .mcontext = mcontext;
this .listdatas = listdatas;
}
@override
public int getcount() {
return listdatas.size();
}
@override
public object getitem( int position) {
return listdatas.get(position);
}
@override
public long getitemid( int position) {
return position;
}
@override
public view getview( int position, view convertview, viewgroup parent) {
viewholder holder = null ;
//根据样式设置不同的布局及数据展示
int type = getitemviewtype(position);
if (convertview == null ) {
holder = new viewholder();
if (type == 1 ) {
convertview = layoutinflater.from(mcontext).inflate(r.layout.test_item, null );
holder.title = (textview) convertview.findviewbyid(r.id.item_title);
holder.content = (textview) convertview.findviewbyid(r.id.item_content);
} else {
convertview = layoutinflater.from(mcontext).inflate(r.layout.type_item, null );
holder.title = (textview) convertview.findviewbyid(r.id.type_title);
holder.content = (textview) convertview.findviewbyid(r.id.type_title);
}
convertview.settag(holder);
} else {
holder = (viewholder) convertview.gettag();
}
holder.content.settext(listdatas.get(position).getcontent());
holder.title.settext(listdatas.get(position).gettitle());
return convertview;
}
//关键方法之getviewtypecount:获取有多少种样式
@override
public int getviewtypecount() {
return 2 ;
}
//关键方法之getitemviewtype:获取item类型
@override
public int getitemviewtype( int position) {
return listdatas.get(position).gettype();
}
final class viewholder {
textview title;
textview content;
}
}
|
相关布局
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
|
activity_main.xml
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<linearlayout
android:id= "@+id/operator_ll"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:orientation= "horizontal" >
<button
android:id= "@+id/add_btn"
android:layout_width= "0dp"
android:layout_height= "wrap_content"
android:layout_weight= "1"
android:gravity= "center"
android:text= "添加" />
<button
android:id= "@+id/del_btn"
android:layout_width= "0dp"
android:layout_height= "wrap_content"
android:layout_weight= "1"
android:gravity= "center"
android:text= "删除" />
</linearlayout>
<listview
android:id= "@+id/test_lv"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:divider= "#d1d1d1" <!--分隔线颜色-->
android:dividerheight= "1px" <!--分隔线高度-->
android:listselector= "@android:color/transparent" <!--取消默认点击效果-->
android:scrollbars= "none" ><!--隐藏滚动条-->
</listview>
<imageview
android:id= "@+id/empty_iv"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"
android:layout_margintop= "100dp"
android:src= "@mipmap/empty"
android:visibility= "gone" />
</linearlayout>
|
activity_type.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<listview
android:id= "@+id/type_lv"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
></listview>
</linearlayout>
|
test_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:orientation= "vertical"
android:paddingleft= "15dp"
android:layout_margintop= "5dp"
android:layout_marginbottom= "5dp" >
<textview
android:id= "@+id/item_title"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "hello world!" />
<textview
android:id= "@+id/item_content"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_margintop= "5dp"
android:text= "hello world!" />
</linearlayout>
|
以上就是搜集的android listview常用小技巧全部内容,希望对大家的学习有所帮助。