告别Spinner下拉列表,用popupWindow 更美好
来源:互联网 发布:淘宝质量问题退货 编辑:程序博客网 时间:2024/04/28 13:42
转自:http://bbs.csdn.net/topics/390889924
第一次发帖,主要是我今天为了实现一个好看点的下拉列表,在网上找了一天关于Spinner的资料,没有看到好的帖子。尝试了各种方法,始终找不到如何去设置下拉框的背景的方法,如果有请给我留言,谢谢。
最后在伟大的钟老师的建议下,我决定换个方向去考虑使用他说的PopupWindow,所以才找到一把写下拉列表的利剑,所以迫不及待的想把这个分享给那些和我一样迷失过小伙伴们。
该事例,实现了popupWindow 可以点击外部区域,取消视图的功能。
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
先给大家看看效果图,很简单
用到的图片:
此处按钮图片里面的箭头我做在一起了,如果你不喜欢,想箭头按钮单独成为按钮,可以更改主页面里面的布局文件,TextView 后面加个Button,再设置背景就好了。想想Spinner能这样么?简直不想吐槽了。
就好了。
代码:Activity部分
PopWindowSpinnerActivity.java
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
package
com.example.spinnertest;
import
java.util.ArrayList;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.ListView;
import
android.widget.PopupWindow;
import
android.widget.TextView;
/**
* 这是一个用PopWindow 实现的下拉列表,对比spinner,我发现spinner,简直没用。
* @author lishuai
*
*/
public
class
PopWindowSpinnerActivity
extends
Activity {
//下拉按钮
TextView MyButton;
//PopupWindow对象声明
PopupWindow pw;
ArrayList<String> list;
//当前选中的列表项位置
int
clickPsition = -
1
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_window);
MyButton = (TextView) findViewById(R.id.myButton);
//获得要显示的数据
list = getList();
//设置默认显示的Text
MyButton.setText(list.get(
0
));
MyButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
//通过布局注入器,注入布局给View对象
View myView = getLayoutInflater().inflate(R.layout.pop,
null
);
//通过view 和宽·高,构造PopopWindow
pw =
new
PopupWindow(myView,
240
,
300
,
true
);
pw.setBackgroundDrawable(getResources().getDrawable(
//此处为popwindow 设置背景,同事做到点击外部区域,popwindow消失
R.drawable.diaolog_bg));
//设置焦点为可点击
pw.setFocusable(
true
);
//可以试试设为false的结果
//将window视图显示在myButton下面
pw.showAsDropDown(MyButton);
ListView lv = (ListView) myView.findViewById(R.id.lv_pop);
lv.setAdapter(
new
ListViewAdapter(PopWindowSpinnerActivity.
this
, list));
lv.setOnItemClickListener(
new
AdapterView.OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
MyButton.setText(list.get(position));
if
(clickPsition != position) {
clickPsition = position;
}
pw.dismiss();
}
});
}
});
}
/**
* 得到list集合的方法
* @return
*/
public
ArrayList<String> getList() {
ArrayList<String> list =
new
ArrayList<String>();
list.add(
"每日吐槽"
);
list.add(
"灵感笔记"
);
list.add(
"爆笑王文"
);
list.add(
"内涵段子"
);
list.add(
"每日吐槽"
);
list.add(
"灵感笔记"
);
list.add(
"爆笑王文"
);
list.add(
"内涵段子"
);
list.add(
"每日吐槽"
);
list.add(
"灵感笔记"
);
list.add(
"爆笑王文"
);
list.add(
"内涵段子"
);
return
list;
}
}
接下来是适配器:这个也算是小小优化了下。
ListViewAdapter.java
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
package
com.example.spinnertest;
import
java.util.ArrayList;
import
android.content.Context;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.TextView;
/**
* 适配器
* @author Administrator
*
*/
public
class
ListViewAdapter
extends
BaseAdapter{
private
LayoutInflater inflater;
private
ArrayList<String> list;
public
ListViewAdapter(Context context, ArrayList<String> list) {
super
();
this
.inflater = LayoutInflater.from(context);
this
.list = list;
}
@Override
public
int
getCount() {
return
list.size();
}
@Override
public
Object getItem(
int
position) {
// TODO Auto-generated method stub
return
null
;
}
@Override
public
long
getItemId(
int
position) {
// TODO Auto-generated method stub
return
0
;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
if
(convertView ==
null
) {
convertView = inflater.inflate(R.layout.lv_items,
null
);
}
TextView tv = (TextView)convertView.findViewById(R.id.text);
tv.setText(list.get(position));
return
convertView;
}
}
接下来是drawable文件夹里面的点击效果selector:selector_sinner.xml
1
2
3
4
5
6
7
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:drawable
=
"@drawable/spinner_bg_press"
android:state_pressed
=
"true"
/>
<
item
android:drawable
=
"@drawable/spinner_bg"
android:state_pressed
=
"false"
/>
</
selector
>
然后是几个xml文件:
文件名:activity_pop_window.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:background
=
"#fff"
android:orientation
=
"vertical"
>
<
TextView
android:paddingLeft
=
"5dp"
android:id
=
"@+id/myButton"
android:gravity
=
"center_vertical"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center_horizontal"
android:background
=
"@drawable/selector_sinner"
android:textColor
=
"#000"
/>
</
LinearLayout
>
文件名:lv_items.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?
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
=
"match_parent"
android:orientation
=
"vertical"
>
<
TextView
android:layout_marginLeft
=
"20dp"
android:id
=
"@+id/text"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textColor
=
"#000"
/>
</
LinearLayout
>
文件名:pop.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?
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
=
"match_parent"
android:orientation
=
"vertical"
android:paddingTop
=
"60dp"
>
<
ListView
android:id
=
"@+id/lv_pop"
android:divider
=
"@color/line"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
/>
</
LinearLayout
>
学了一年Android了,今天第一次使用popupwindow,在发现原来它这么强大。实在惭愧......不废话了。
大家加油。
0 0
- 告别Spinner下拉列表,用popupWindow 更美好
- 告别Spinner下拉列表,用popupWindow 更美好
- PopupWindow制作Spinner下拉列表
- 使用PopupWindow实现Spinner的下拉列表
- 关于下拉列表的实现(spinner和PopupWindow+listview)
- 用PopupWindow实现下拉列表
- 下拉菜单 spinner ,popupwindow, 自定义popupwindow总结
- 下拉列表框spinner
- 下拉列表 Spinner
- Android 下拉列表 Spinner
- spinner下拉列表
- Android Spinner 下拉列表
- 下拉列表 Spinner
- android spinner下拉列表
- 自定义Spinner下拉列表
- spinner下拉列表
- android Spinner 下拉列表
- android Spinner下拉列表
- SQLite2MySQL 转换
- 小白学c++之 unix共享内存
- [500lines]500行代码写web server
- Java代理模式 静态代理 动态代理
- 给UITextView增加了链接
- 告别Spinner下拉列表,用popupWindow 更美好
- 黑马程序员——java基础之薄弱点总结(二)
- handler与Message使用
- Linux的telnet安装
- A1 = ?
- 常见的6种传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
- 定制keil开发环境
- maven常见问题问答
- 如何查找生成的dll出错原因