美文网首页程序员Android知识
Android自定义单选多选下拉列表

Android自定义单选多选下拉列表

作者: Funzion | 来源:发表于2017-07-29 13:02 被阅读0次

    发疯的产品要做一个可以单选和多选的下拉列表,陪你玩玩吧。

    直接上效果:

    实现方案:

    我的思路是自定义一个类继承PopupWindow,里面的选项采用Listview,再结合一些动画来实现。

    核心代码:

    public class MultiSelectPopupWindows extends PopupWindow {
    
        private Context context;
        private View parent;
        private List<Search> data;
        private int yStart;
        private SearchPopupWindowsAdapter adapter;
    
        public MultiSelectPopupWindows(Context context, View parent, int yStart, List<Search> data) {
            this.context = context;
            this.parent = parent;
            this.yStart = yStart;
            this.data = data;
            initView();
        }
    
        private void initView() {
            View view = View.inflate(context, R.layout.popupwindows_multiselect, null);
            view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in_slow));
            LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout_selector);
            linearLayout.startAnimation(AnimationUtils.loadAnimation(context, R.anim.list_top_in));
            ListView listView = (ListView) view.findViewById(R.id.listView_selector);
    
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.TOP, 0, DensityUtil.dip2px(context, yStart));
            update();
    
            initListView(listView, data);
        }
    
        private void initListView(ListView listView, List<Search> data) {
            adapter = new SearchPopupWindowsAdapter(context);
            adapter.setItems(data);
            listView.setAdapter(adapter);
        }
    
        public List getItemList() {
            return adapter.getItemList();
        }
    
    }
    

    项目源码:

    https://github.com/hfrommane/MultiSelectPopupWindows

    相关文章

      网友评论

        本文标题:Android自定义单选多选下拉列表

        本文链接:https://www.haomeiwen.com/subject/xuaqlxtx.html