android 简单的下拉选择框实现(ListPopupWindow)

使用系统自带布局的ListPopupWindow: 

第一步 Activity布局: 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gang.app.myceshi.Main2Activity">
 
    <Button
        android:background="@drawable/spinner"
        android:layout_gravity="center"
        android:id="@+id/mButton"
        android:text="数据一"
        android:layout_width="180dp"
        android:layout_height="30dp" />
 
</LinearLayout>

 第二步 选择框背景,在drawable下新建select_box_bg.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <item>
        <shape>
            <!--边框线宽度  边框线颜色-->
            <stroke
                android:width="1dp"
                android:color="#D3D3D3" />
 
            <!--圆角度数-->
            <corners android:radius="3dp" />
 
            <!--背景颜色-->
            <solid android:color="#ffffff" />
 
            <!--边距-->
            <padding android:right="5dp" />
 
 
        </shape>
 
 
    </item>
    <!--//第二组item 插入图片(替换默认箭头)-->
    <item>
        <bitmap
            android:gravity="end"
            android:src="@drawable/triangle" />
    </item>
</layer-list>

 //箭头图片 triangle.png:

 
第三步 我的Activity代码实现: 

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
 
    private Button mButton;
    private ListPopupWindow listPopupWindow;
    private List<String> mList = new ArrayList<>();
    ;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        selectBox();//添加数据,初始化ListPopupWindow
        listPopupWindowListener();//ListPopupWindow监听
    }
 
    private void initView() {
        mButton = (Button) findViewById(R.id.mButton);
 
        mButton.setOnClickListener(this);
    }
 
    /*
     * 方法名:selectBox()
     * 功    能:添加数据,初始化ListPopupWindow
     * 参    数:无
     * 返回值:无
     */
    private void selectBox() {
 
        mList.add("数据一");
        mList.add("数据二");
        mList.add("数据三");
 
        //初始化ListPopupWindow,适配
        listPopupWindow = new ListPopupWindow(this);
        //系统布局
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        //自定义适配器,布局
//        MyApader adapter = new MyApader(this,mList);
        listPopupWindow.setAdapter(adapter);
        listPopupWindow.setAnchorView(mButton);//设置ListPopupWindow的锚点,关联mButton位置
        listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setModal(true);
 
    }
 
    /*
     * 方法名:listPopupWindowListener()
     * 功    能:ListPopupWindow监听
     * 参    数:无
     * 返回值:无
     */
    private void listPopupWindowListener() {
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mButton.setText(mList.get(position));
 
                mList.add(0, mList.remove(position)); //list集合中的某个值放到第一的位置
 
//                Collections.swap(mList,0,position);//互换位置
 
                listPopupWindow.dismiss(); //关闭listPopupWindow
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mButton:
                listPopupWindow.show();//显示listPopupWindow
                break;
        }
    }
}

以下使用自定义布局 ListPopupWindow: 

 

//只有适配器变了,使用自定义适配器,自定义布局。

第一步 我的Activity代码实现:

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
 
    private Button mButton;
    private ListPopupWindow listPopupWindow;
    private List<String> mList = new ArrayList<>();
    ;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        selectBox();//添加数据,初始化ListPopupWindow
        listPopupWindowListener();//ListPopupWindow监听
    }
 
    private void initView() {
        mButton = (Button) findViewById(R.id.mButton);
 
        mButton.setOnClickListener(this);
    }
 
    /*
     * 方法名:selectBox()
     * 功    能:添加数据,初始化ListPopupWindow
     * 参    数:无
     * 返回值:无
     */
    private void selectBox() {
 
        mList.add("数据一");
        mList.add("数据二");
        mList.add("数据三");
 
        //初始化ListPopupWindow,适配
        listPopupWindow = new ListPopupWindow(this);
        //系统布局
//        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        //自定义适配器,布局
        MyApader adapter = new MyApader(this,mList);
        listPopupWindow.setAdapter(adapter);
        listPopupWindow.setAnchorView(mButton);//设置ListPopupWindow的锚点,关联mButton位置
        listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setModal(true);
 
    }
 
    /*
     * 方法名:listPopupWindowListener()
     * 功    能:ListPopupWindow监听
     * 参    数:无
     * 返回值:无
     */
    private void listPopupWindowListener() {
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mButton.setText(mList.get(position));
 
                mList.add(0, mList.remove(position)); //list集合中的某个值放到第一的位置
 
//                Collections.swap(mList,0,position);//互换位置
 
                listPopupWindow.dismiss(); //关闭listPopupWindow
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mButton:
                listPopupWindow.show();//显示listPopupWindow
                break;
        }
    }
}

第二步 自定义适配器 MyApader :

class MyApader extends BaseAdapter{
    private Context context;
    private List<String>mList;
 
    public MyApader(Context context, List<String> mList) {
        this.context = context;
        this.mList = mList;
    }
 
    @Override
    public int getCount() {
        return mList.size();
    }
 
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.litem_layout, null);
        }
 
        TextView textView = (TextView) convertView.findViewById(R.id.mText);
        textView.setText(mList.get(position));
//        if (position==0){
//            textView.setBackgroundColor(Color.BLUE);
//        }
        return convertView;
    }
}


第三步 适配器Item布局  litem_layout.xml :

<?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:id="@+id/mText"
        android:textStyle="bold"
        android:padding="5dp"
        android:textSize="15sp"
        android:textColor="@color/colorAccent"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <View
        android:background="#bebebe"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
 
</LinearLayout>
解决使用ListPopupWindow时发现宽度不适应,显示不全问题,使用View.inflate获取到spinner_item.xml中的TextView对象,赋值后获取宽度,比较出最大值maxWidth即可,调用ListPopupWindowsetWidth(maxWidth),注意:TextView赋值后,获取宽度前,要调用measure方法,否则获取宽度为0。
popup = new ListPopupWindow(getContext());
 
            int maxWidth = 0;
            for (String car : cars) {
                TextView textView = (TextView) View.inflate(getContext(), R.layout.spinner_item, null);
                textView.setText(car);
                textView.measure(0, 0);
                maxWidth = maxWidth > textView.getMeasuredWidth() ? maxWidth : textView.getMeasuredWidth();
            }
            popup.setAdapter(adapter);
            popup.setWidth(maxWidth);
转载于:https://blog.csdn.net/weixin_42061754/article/details/90110857
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值