Android RecyclerView实现下拉列表功能
程序员文章站
2024-03-01 19:42:16
现在市面上的很多的应用,都带有下拉列表的功能,将所有选项都放在下拉列表中,当用户点击选择的时候,弹出所有的选项,用户选择一项后,下拉列表自动隐藏,很多下拉列表都是用list...
现在市面上的很多的应用,都带有下拉列表的功能,将所有选项都放在下拉列表中,当用户点击选择的时候,弹出所有的选项,用户选择一项后,下拉列表自动隐藏,很多下拉列表都是用listview + popupwindow来实现的,由于google推出了替代listview的recyclerview,所以简单实现一下:
mainactivity.java
package com.jackie.countdowntimer; import android.graphics.drawable.bitmapdrawable; import android.os.bundle; import android.os.countdowntimer; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.edittext; import android.widget.imagebutton; import android.widget.popupwindow; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity implements view.onclicklistener { private edittext mnumberedittext; private imagebutton mselectimagebutton; private popupwindow mpopupwindow; private recyclerview mrecyclerview; private recyclerviewadapter mrecyclerviewadapter; private list<string> mnumberlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace with your own action", snackbar.length_long) .setaction("action", null).show(); } }); //初始化数据 mnumberlist = new arraylist<>(); for (int i = 0; i < 30; i++) { mnumberlist.add("1000000" + i); } mnumberedittext = (edittext) findviewbyid(r.id.number); mselectimagebutton = (imagebutton) findviewbyid(r.id.select_number); mselectimagebutton.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { case r.id.select_number: showselectnumberpopupwindow(); break; } } /** * 弹出选择号码的对话框 */ private void showselectnumberpopupwindow() { initrecyclerview(); mpopupwindow = new popupwindow(mrecyclerview, mnumberedittext.getwidth() - 4, 200); mpopupwindow.setoutsidetouchable(true); // 设置外部可以被点击 mpopupwindow.setbackgrounddrawable(new bitmapdrawable()); mpopupwindow.setfocusable(true); // 使popupwindow可以获得焦点 // 显示在输入框的左下角 mpopupwindow.showasdropdown(mnumberedittext, 2, -5); } /** * 初始化recyclerview,模仿listview下拉列表的效果 */ private void initrecyclerview(){ mrecyclerview = new recyclerview(this); //设置布局管理器 mrecyclerview.setlayoutmanager(new linearlayoutmanager(this)); mrecyclerview.setbackgroundresource(r.drawable.background); //设置adapter mrecyclerviewadapter = new recyclerviewadapter(this, mnumberlist); mrecyclerview.setadapter(mrecyclerviewadapter); //设置点击事件 mrecyclerviewadapter.setonitemclicklistener(new recyclerviewadapter.onitemclicklistener() { @override public void onitemclick(view view, int position) { mnumberedittext.settext(mnumberlist.get(position)); mnumberedittext.setselection(mnumberedittext.gettext().tostring().length()); mpopupwindow.dismiss(); } }); //添加分割线 mrecyclerview.additemdecoration(new divideritemdecoration(this, divideritemdecoration.vertical_list)); } }
recyclerviewadapter.java
package com.jackie.countdowntimer; import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imagebutton; import android.widget.textview; import java.util.list; /** * created by jackie on 2015/12/18. * recyclerview adapter */ public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.recyclerviewholder> { private onitemclicklistener monitemclicklistener; private context mcontext; private list<string> mnumberlist; public recyclerviewadapter(context context, list<string> numberlist) { this.mcontext = context; this.mnumberlist = numberlist; } public interface onitemclicklistener { void onitemclick(view view, int position); } public void setonitemclicklistener(onitemclicklistener onitemclicklistener) { this.monitemclicklistener = onitemclicklistener; } @override public recyclerviewadapter.recyclerviewholder oncreateviewholder(viewgroup parent, int viewtype) { recyclerviewholder holder = new recyclerviewholder(layoutinflater.from(mcontext).inflate(r.layout.number_item, parent, false)); return holder; } @override public void onbindviewholder(final recyclerviewadapter.recyclerviewholder holder, final int position) { holder.numbertextview.settext(mnumberlist.get(position)); // 如果设置了回调,则设置点击事件 if (monitemclicklistener != null) { holder.numbertextview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { monitemclicklistener.onitemclick(holder.itemview, holder.getlayoutposition()); } }); } holder.deleteimagebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mnumberlist.remove(position); //通知刷新 notifyitemremoved(position); } }); } @override public int getitemcount() { return mnumberlist.size(); } public class recyclerviewholder extends recyclerview.viewholder { textview numbertextview; imagebutton deleteimagebutton; public recyclerviewholder(view itemview) { super(itemview); numbertextview = (textview) itemview.findviewbyid(r.id.number_item); deleteimagebutton = (imagebutton) itemview.findviewbyid(r.id.number_item_delete); } } }
divideritemdecoration.java
package com.jackie.countdowntimer; /* * copyright (c) 2014 the android open source project * * licensed under the apache license, version 2.0 (the "license"); * limitations under the license. */ import android.content.context; import android.content.res.typedarray; import android.graphics.canvas; import android.graphics.rect; import android.graphics.drawable.drawable; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.view; /** * this class is from the v7 samples of the android sdk. it's not by me! * <p/> * see the license above for details. */ public class divideritemdecoration extends recyclerview.itemdecoration { private static final int[] attrs = new int[]{ android.r.attr.listdivider }; public static final int horizontal_list = linearlayoutmanager.horizontal; public static final int vertical_list = linearlayoutmanager.vertical; private drawable mdivider; private int morientation; public divideritemdecoration(context context, int orientation) { final typedarray a = context.obtainstyledattributes(attrs); mdivider = a.getdrawable(0); a.recycle(); setorientation(orientation); } public void setorientation(int orientation) { if (orientation != horizontal_list && orientation != vertical_list) { throw new illegalargumentexception("invalid orientation"); } morientation = orientation; } @override public void ondraw(canvas c, recyclerview parent) { if (morientation == vertical_list) { drawvertical(c, parent); } else { drawhorizontal(c, parent); } } public void drawvertical(canvas c, recyclerview parent) { final int left = parent.getpaddingleft(); final int right = parent.getwidth() - parent.getpaddingright(); final int childcount = parent.getchildcount(); for (int i = 0; i < childcount; i++) { final view child = parent.getchildat(i); android.support.v7.widget.recyclerview v = new android.support.v7.widget.recyclerview(parent.getcontext()); final recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams(); final int top = child.getbottom() + params.bottommargin; final int bottom = top + mdivider.getintrinsicheight(); mdivider.setbounds(left, top, right, bottom); mdivider.draw(c); } } public void drawhorizontal(canvas c, recyclerview parent) { final int top = parent.getpaddingtop(); final int bottom = parent.getheight() - parent.getpaddingbottom(); final int childcount = parent.getchildcount(); for (int i = 0; i < childcount; i++) { final view child = parent.getchildat(i); final recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams(); final int left = child.getright() + params.rightmargin; final int right = left + mdivider.getintrinsicheight(); mdivider.setbounds(left, top, right, bottom); mdivider.draw(c); } } @override public void getitemoffsets(rect outrect, int itemposition, recyclerview parent) { if (morientation == vertical_list) { outrect.set(0, 0, 0, mdivider.getintrinsicheight()); } else { outrect.set(0, 0, mdivider.getintrinsicwidth(), 0); } } }
效果图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java经典排序算法之二分插入排序详解
下一篇: java web过滤器处理乱码