Android自定义单选多选下拉列表的实例代码

2020-10-22 10:56:00 浏览数 (1)

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

直接上效果:

实现方案:

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

核心代码:

代码语言:javascript复制
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

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞