android自定义下拉筛选,Android自定义下拉列表——PopupWindow+ListView(单选模式)...

这篇博客介绍了如何在Android中使用PopupWindow和ListView实现自定义的下拉筛选功能。通过设置ListView的单选模式,并结合PopupWindow,创建了一个具有选择框的下拉列表。博客内容包括ListView的单选模式设置,PopupWindow的使用,以及自定义适配器的编写。示例代码展示了如何在Activity中实例化和展示下拉列表,以及监听列表项的点击事件。
摘要由CSDN通过智能技术生成

最近在项目中使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值