Android drop-down selection box custom view

Let's first look at the effect achieved

Basic idea: Inherit the custom View of PopupWindow

Explanation: This part of the figure below has its own layout. The main explanation in this article is to click on a category to display the drop-down implementation

Step 1: Customize SpinnerPopuwindow to inherit PopupWindow


/**
 * Created by sws on 2019-04-28.
 * from:
 * describe:
 */

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<String> list, AdapterView.OnItemClickListener itemsOnClick) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context =context;
        conentView = inflater.inflate(R.layout.popuwindow_spinner, null);
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //    this.setWidth(view.getWidth());
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 刷新状态
        this.update();
        this.setOutsideTouchable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
        //解决软键盘挡住弹窗问题
        this.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        // 设置SelectPicPopupWindow弹出窗体动画效果
        //  this.setAnimationStyle(R.style.AnimationPreview);

        adapter = new SpinnerPopAdapter(context,list);
        listView = (ListView) conentView.findViewById(R.id.listView);
        listView.setOnItemClickListener(itemsOnClick);
        listView.setAdapter(adapter);
        // setAdapter是异步进行的,为了使弹窗能即时刷新,所以使用post+Runnable
        listView.post(new Runnable() {
            @Override
            public void run() {
                //主要是为了比对未打开列表时显示的值和列表中的值进行默认选中
                for(int j = 0;j<list.size();j++){
                    if(string.equals(list.get(j).toString())){
                        listView.setItemChecked(j, true);//listview自带的方法
                    }else {
                        listView.setItemChecked(j, false);
                    }
                }
            }
        });

        pop_title = (TextView) conentView.findViewById(R.id.pop_title);
//        pop_cancel = (TextView) conentView.findViewById(R.id.pop_cancel);
//
//        pop_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dismiss();
//                darkenBackground(1f);
//            }
//        });
    }

    //给下拉列表的设置标题,增加复用性
    public void setTitleText(String str){
        pop_title.setText(str);
    }
    //获取选中列表中的数据所对应的position
    public int getText(){
        return listView.getCheckedItemPosition();
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
              this.showAsDropDown(parent);
//             this.showAsDropDown(parent,0,10);
//            this.showAtLocation(parent, Gravity.BOTTOM|Gravity.CENTER, 0, 0);
            darkenBackground(0.9f);//弹出时让页面背景回复给原来的颜色降低透明度,让背景看起来变成灰色
        }
    }
    /**
     * 关闭popupWindow
     */
    public void dismissPopupWindow() {
        this.dismiss();
        darkenBackground(1f);//关闭时让页面背景回复为原来的颜色

    }
    /**
     * 改变背景颜色,主要是在PopupWindow弹出时背景变化,通过透明度设置
     */
    private void darkenBackground(Float bgcolor){
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgcolor;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
}

 This class is mainly to create a PopupWindow, and write its display, close, click monitor, set title and other methods. You can change the background color, width and height of the popup window according to your needs.

The list layout that needs to be used is as follows: a simple listview and a title TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <TextView
        android:id="@+id/pop_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="6dp"
        android:gravity="center"
        android:textSize="@dimen/text14"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/hui"/>
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#00000000"
        android:choiceMode="singleChoice"
        >
    </ListView>
</LinearLayout>

 Step 2: Create in the Activity that will use the drop-down list

 

/** 项目类型数据*/
private List<String> lxData;

private SpinnerPopuwindow lxSpinnerPopuwindow;


//在页面上项目类型按钮的点击监听中加一下代码

type_lx = tvlx.getText().toString();
                lxSpinnerPopuwindow = new SpinnerPopuwindow( StreetDeviceListActivity.this,type_lx,lxData,lxitemsOnClick);
//type_lx:选择某一项后显示选中内容的TextView
                lxSpinnerPopuwindow.showPopupWindow(llSX);//llSX是指此下拉列表要显示在那个view下边
                lxSpinnerPopuwindow.setTitleText("项目类型");//给下拉列表设置标题

Step 3: Set up the listener for the drop-down list

 private AdapterView.OnItemClickListener lxitemsOnClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String value = lxData.get(lxSpinnerPopuwindow.getText());
            tvlx.setText(value);
            lxSpinnerPopuwindow.dismissPopupWindow();

           //TODO 这儿写点击某一项之后你要执行的代码

        }
    };

 

Guess you like

Origin blog.csdn.net/u010855711/article/details/90205672