先看一下效果:
效果就是这样,看一下实现,其实也没多难,就是想开源出来供小伙伴们使用,如有不合理地方,希望大家多多指正。
1.自定义popwindow
首先我们分析一下,这样的效果肯定是一个popwindow嵌套着listview,而上面的title、和下面的cancel是两个文本框,实现起来也比较简单。
然后我们在popwindow中声明两个接口,用来回调cancel和item的点击事件
1
2
3
4
5
6
7
|
public interface onpopitemclicklistener{
void onpopitemclick(view view, int position);
}
public interface onbottomtextviewclicklistener{
void onbottomclick();
}
|
然后再设置一些popwindow的一些属性
1
2
3
4
5
6
7
8
9
10
11
12
|
parentview = layoutinflater.from(context).inflate(r.layout.list_popwindow, null );
setcontentview(parentview);
lv = (listview) parentview.findviewbyid(r.id.lv_popwindow);
//设置弹出窗体的高
this .setwidth(viewgroup.layoutparams.match_parent);
this .setheight(viewgroup.layoutparams.match_parent);
//设置弹出窗体可点击
this .setfocusable( true );
//实例化一个colordrawable颜色为半透明
colordrawable dw = new colordrawable( 0xb0000000 );
//设置selectpicpopupwindow弹出窗体的背景
this .setbackgrounddrawable(dw);
|
看一下整体的代码吧:
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
|
public class listpopwindow extends popupwindow{
private context context; //上下文
private view parentview; //父视图
private list<popbean> datalist; //item数据源
private onpopitemclicklistener listener; //item点击接口
private listview lv; //item列表视图
private view viewtop; //title视图
private string toptext,bottomtext; //title文字,bottom文字
private textview tvtop,tvbottom; //title文本,bottom文本
private popwindowadapter adapter; //适配器
private onbottomtextviewclicklistener bottomlistener; //底部点击接口
public interface onpopitemclicklistener{
void onpopitemclick(view view, int position);
}
public interface onbottomtextviewclicklistener{
void onbottomclick();
}
public listpopwindow(context context,onpopitemclicklistener listener,onbottomtextviewclicklistener bottomlistener,
view parentview,list<popbean> datalist,string bottomtext,string toptext){
this .context = context;
this .listener = listener;
this .parentview = parentview;
this .datalist = datalist;
this .bottomlistener = bottomlistener;
this .toptext = toptext;
this .bottomtext = bottomtext;
initviews();
}
private void initviews(){
parentview = layoutinflater.from(context).inflate(r.layout.list_popwindow, null );
setcontentview(parentview);
lv = (listview) parentview.findviewbyid(r.id.lv_popwindow);
//设置弹出窗体的高
this .setwidth(viewgroup.layoutparams.match_parent);
this .setheight(viewgroup.layoutparams.match_parent);
//设置弹出窗体可点击
this .setfocusable( true );
//实例化一个colordrawable颜色为半透明
colordrawable dw = new colordrawable( 0xb0000000 );
//设置selectpicpopupwindow弹出窗体的背景
this .setbackgrounddrawable(dw);
//view添加ontouchlistener监听判断获取触屏位置如果在布局外面则销毁弹出框
parentview.setontouchlistener( new view.ontouchlistener() {
public boolean ontouch(view v, motionevent event) {
int height = parentview.findviewbyid(r.id.ll_bottom).gettop();
int y = ( int ) event.gety();
if (event.getaction() == motionevent.action_up) {
if (y > height) {
dismiss();
}
}
return true ;
}
});
update();
viewtop = parentview.findviewbyid(r.id.view_line1);
tvbottom = (textview) parentview.findviewbyid(r.id.tv_popwindow_bottom);
tvtop = (textview) parentview.findviewbyid(r.id.tv_popwindow_first);
adapter = new popwindowadapter(context,datalist, false );
lv.setadapter(adapter);
if (!textutils.isempty(toptext)){
tvtop.setvisibility(view.visible);
tvtop.settext(toptext);
viewtop.setvisibility(view.visible);
}
else {
tvtop.setvisibility(view.gone);
viewtop.setvisibility(view.gone);
}
if (!textutils.isempty(bottomtext)){
tvbottom.setvisibility(view.visible);
tvbottom.settext(bottomtext);
}
else {
tvbottom.setvisibility(view.gone);
}
lv.setonitemclicklistener( new adapterview.onitemclicklistener() {
@override
public void onitemclick(adapterview<?> adapterview, view view, int i, long l) {
listener.onpopitemclick(view, i);
}
});
tvbottom.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view view) {
bottomlistener.onbottomclick();
}
});
}
}
|
2.看一些item的bean
这里我就声明了title和图片的id
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
|
package com.hankkin.library;
public class popbean {
private string title;
private int icon_res;
public string gettitle() {
return title;
}
public void settitle(string title) {
this .title = title;
}
public int geticon_res() {
return icon_res;
}
public void seticon_res( int icon_res) {
this .icon_res = icon_res;
}
public popbean(string title, int icon_res) {
this .title = title;
this .icon_res = icon_res;
}
}
|
3.自定义adapter适配器
这里面可能要注意的就是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
|
@override
public view getview( int i, view view, viewgroup viewgroup) {
viewholder holder;
if (view == null ) {
view = inflater.inflate(r.layout.listview_popwindow_item, null );
holder = new viewholder();
holder.tv_name = (textview) view.findviewbyid(r.id.tv_title);
holder.v_line = (view) view.findviewbyid(r.id.v_line);
view.settag(holder);
} else {
holder = (viewholder) view.gettag();
}
holder.tv_name.settext(datalist.get(i).gettitle());
if (datalist.size() - 1 == i) {
holder.v_line.setvisibility(view.invisible);
holder.tv_name.setbackground(context.getresources().getdrawable(r.drawable.selector_bottom_half));
} else {
holder.v_line.setvisibility(view.visible);
holder.tv_name.setbackground(context.getresources().getdrawable(r.drawable.list_gray_item));
}
return view;
}
|
最后看一下调用
activity需要实现item接口(onpopitemclicklistener)和底部按钮接口(onbottomtextviewclicklistener)
1
2
3
4
5
6
7
8
9
|
public void show(view view){
list<popbean> pops = new arraylist<>();
for ( int i= 0 ;i< 5 ;i++){
popbean pop = new popbean( "item" +i, 0 );
pops.add(pop);
}
popwindow = new listpopwindow(mainactivity. this , this , this ,rl,pops, "cancel" , "title" );
popwindow.showatlocation(rl, gravity.center| gravity.bottom, 0 , 0 );
}
|
以上就是本文的全部内容,希望能够对大家的学习有所帮助。