MultiSelectPopupWindows 弹出多选列表

MultiSelectPopupWindows 弹出多选列表

某些时候需要用到多选列表。

MultiSelectPopupWindows 是用 PopupWindow 和 ListView 封装的,主要封装 ui 层和设置逻辑。

MultiSelectPopupWindows 多选列表


public class MultiSelectPopupWindows extends PopupWindow {
    private Context context;
    private View parent;
    private List data;
    private int yStart;
    private SearchPopupWindowsAdapter adapter;

    private ListView mListView;

    public MultiSelectPopupWindows(Context context, View parent, int yStart, List data) {
        this.context = context; // 当前的 context
        this.parent = parent;   // parent ui
        this.yStart = yStart;   // top 位置
        this.data = data;       // 多选列表数据
        initView();
    }

    private void initView() {
        View view = View.inflate(this.context, R.layout.popupwindows_multiselect, null);
        view.startAnimation(AnimationUtils.loadAnimation(this.context, R.anim.fade_in_slow));
        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout_selector);
        linearLayout.startAnimation(AnimationUtils.loadAnimation(this.context, R.anim.list_top_in));
        mListView = (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(this.context, yStart));
        update();

        initListView(mListView, data);
    }

    private void initListView(ListView listView, List data) {
        adapter = new SearchPopupWindowsAdapter(context);
        adapter.setItems(data);
        listView.setAdapter(adapter);
    }

    public List getItemList() {
        return adapter.getItemList();
    }
}

初始化 MultiSelectPopupWindows 时传入多选列表数据项 data 。

SearchPopupWindowsAdapter 多选列表适配器

设置数据适配器内容,提供 MultiSelectPopupWindows 数据更新的接口,以及多选逻辑。


abstract class CommonBaseAdapter extends BaseAdapter {
    protected Context context;
    protected LayoutInflater inflater;
    protected List itemList = new ArrayList<>();

    public CommonBaseAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public boolean isEmpty() {
        return itemList.isEmpty();
    }

    public void addItems(List itemList) {
        this.itemList.addAll(itemList);
        notifyDataSetChanged();
    }

    public void setItems(List itemList) {
        this.itemList.clear();
        this.itemList = itemList;
        notifyDataSetChanged();
    }

    public void clearItems() {
        itemList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public List getItemList() {
        return itemList;
    }

    @Override
    abstract public View getView(int i, View view, ViewGroup viewGroup);
}

class SearchPopupWindowsAdapter extends CommonBaseAdapter {
    public SearchPopupWindowsAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        view = inflater.inflate(R.layout.item_list_selector, viewGroup, false);
        if (view != null) {
            RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout_search);
            TextView textView = (TextView) view.findViewById(R.id.textView_listView_selector);
            final ImageView imageView = (ImageView) view.findViewById(R.id.image_search_check);

            Search search = itemList.get(i);

            imageView.setImageResource(search.isChecked() ? R.drawable.icon_selected : R.drawable.icon_unselected);
            textView.setText(search.getKeyWord());

            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Search search = itemList.get(i);
                    search.setChecked(! search.isChecked());
                    imageView.setImageResource(search.isChecked() ? R.drawable.icon_selected : R.drawable.icon_unselected);
                }
            });
        }
        return view;
    }
}

Search 多选列表数据项

Search 用于记录多选列表每一项的数据。


public class Search {
    private String keyWord;
    private boolean isChecked;
    private String no;

    public Search(String keyWord, boolean isChecked) {
        this.keyWord = keyWord;
        this.isChecked = isChecked;
    }

    public Search(String keyWord, boolean isChecked, String no) {
        this.keyWord = keyWord;
        this.isChecked = isChecked;
        this.no = no;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }
}

调用方式


protected void onCreate(Bundle savedInstanceState) {

    // 加载数据过程忽略( List mSearchList; )

    // 用于绑定多选列表 UI
    LinearLayout linearLayoutMultiSelectPopup = (LinearLayout) findViewById(R.id.linearlayout_multi_select_popup);

    MultiSelectPopupWindows multiSelectPopupWindows = new MultiSelectPopupWindows(this,
            linearLayoutMultiSelectPopup,
            200,
            searchList);

    multiSelectPopupWindows.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            String text = "";
            for (int i = 0, len = groupNames.length; i < len; ++ i)
                text += " " + groupNames[i] + (i + 1 < len ? "," : "");
            Toast.makeText(MainActivity.this, "["+text+"]", Toast.LENGTH_SHORT).show();
        }
    });
}

private String[] getSearchDataResult() {
    List groupNameList = new ArrayList<>();
    for (Search search : mSearchList) {
        if (search.isChecked()) {
            groupNameList.add(search.getKeyWord());
        }
    }
    return (String[]) groupNameList.toArray(new String[groupNameList.size()]);
}

主界面

多选列表

多选列表 2

多选结果

你可能感兴趣的:(MultiSelectPopupWindows 弹出多选列表)