最近在项目中使用android原生的Spinner发现其显示效果没有想象中的好,于是就动手写了一个popuwindow的弹出框下拉列表,在popuwindow中主要使用了ListView自带的单选和多选模式。
listview中使用自带的选择框有两种方式,第一种方式:
ListView lv = (ListView) findViewById(R.id.list_view);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
第二中方式:就是在listview布局中直接加,建议使用第二种
android:choiceMode="singleChoice"
先书写一个带listview的布局,popuwindow_spinner布局:
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/spinner_pop_bg">
android:id="@+id/pop_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:gravity="center"
android:textSize="@dimen/title_size"/>
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/grey_line"/>
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#00000000"
android:choiceMode="singleChoice"
>
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/grey_line"/>
android:id="@+id/pop_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="@string/cancel"
android:textColor="@color/orange"
android:gravity="center"
android:textSize="@dimen/title_size"/>
其实在popuwindow中使用listview和在Activity中使用相同,具体实现代码如下(具体使用看注释):
public class SpinnerPopuwindow extends PopupWindow{
private View conentView;
private ListView listView;
private SpinnerPopAdapter adapter;
private Activity context;
private TextView pop_title;
private TextView pop_cancel;
/**
* @param context 上下文
* @param string 获取到未打开列表时显示的值
* @param list 需要显示的列表的集合
* @param itemsOnClick listview在activity中的点击监听事件
*/
@SuppressLint("InflateParams")
public SpinnerPopuwindow(final Activity context, final String string, final List list, AdapterView.OnItemClickListener itemsOnClick) {
LayoutInflater inf