欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现简单下拉筛选框

程序员文章站 2023-09-04 13:14:17
最近接到一个新的项目,项目时间比较紧张,有一个功能类似于58同城,京东的一个下拉筛选框,为了节省时间,从网上面拷贝了一份封装好的代码,进行的自己的一些修改,感觉灵活性还挺高...

最近接到一个新的项目,项目时间比较紧张,有一个功能类似于58同城,京东的一个下拉筛选框,为了节省时间,从网上面拷贝了一份封装好的代码,进行的自己的一些修改,感觉灵活性还挺高的,分享出来给大家看一看

大致效果如下,可以自己加入自己的布局

Android实现简单下拉筛选框

先看一下这个expandtabview这个类  代码比较简单 我就不做具体介绍了 有不懂的可以私信我

public class expandtabview extends linearlayout implements ondismisslistener {
 
 private togglebutton selectedbutton;
 private arraylist<string> mtextarray = new arraylist<string>();
 private arraylist<relativelayout> mviewarray = new arraylist<relativelayout>();
 private arraylist<togglebutton> mtogglebutton = new arraylist<togglebutton>();
 private context mcontext;
 private final int small = 0;
 private int displaywidth;
 private int displayheight;
 private popupwindow popupwindow;
 private int selectposition;
 
 public expandtabview(context context) {
 super(context);
 init(context);
 }
 
 public expandtabview(context context, attributeset attrs) {
 super(context, attrs);
 init(context);
 }
 
 /**
 * 根据选择的位置设置tabitem显示的值
 */
 public void settitle(string valuetext, int position) {
 if (position < mtogglebutton.size()) {
 mtogglebutton.get(position).settext(valuetext);
 }
 }
 
 public void settitle(string title){
 
 }
 /**
 * 根据选择的位置获取tabitem显示的值
 */
 public string gettitle(int position) {
 if (position < mtogglebutton.size() && mtogglebutton.get(position).gettext() != null) {
 return mtogglebutton.get(position).gettext().tostring();
 }
 return "";
 }
 
 /**
 * 设置tabitem的个数和初始值
 */
 public void setvalue(arraylist<string> textarray, arraylist<view> viewarray) {
 if (mcontext == null) {
 return;
 }
 layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service);
 
 mtextarray = textarray;
 for (int i = 0; i < viewarray.size(); i++) {
 final relativelayout r = new relativelayout(mcontext);
 int maxheight = (int) (displayheight * 0.7);
 relativelayout.layoutparams rl = new relativelayout.layoutparams(relativelayout.layoutparams.fill_parent, maxheight);
 rl.leftmargin = 10;
 rl.rightmargin = 10;
 r.addview(viewarray.get(i), rl);
 mviewarray.add(r);
 r.settag(small);
 togglebutton tbutton = (togglebutton) inflater.inflate(r.layout.toggle_button, this, false);
 addview(tbutton);
 view line = new textview(mcontext);
 line.setbackgroundresource(r.drawable.choosebar_line);
 if (i < viewarray.size() - 1) {
 layoutparams lp = new layoutparams(2, layoutparams.fill_parent);
 addview(line, lp);
 }
 mtogglebutton.add(tbutton);
 tbutton.settag(i);
 tbutton.settext(mtextarray.get(i));
 
 r.setonclicklistener(new onclicklistener() {
 @override
 public void onclick(view v) {
  onpressback();
 }
 });
 
 r.setbackgroundcolor(mcontext.getresources().getcolor(r.color.popup_main_background));
 tbutton.setonclicklistener(new onclicklistener() {
 @override
 public void onclick(view view) {
  // initpopupwindow();
  togglebutton tbutton = (togglebutton) view;
 
  if (selectedbutton != null && selectedbutton != tbutton) {
  selectedbutton.setchecked(false);
  }
  selectedbutton = tbutton;
  selectposition = (integer) selectedbutton.gettag();
  startanimation();
  if (monbuttonclicklistener != null && tbutton.ischecked()) {
  monbuttonclicklistener.onclick(selectposition);
  }
 }
 });
 }
 }
 
 private void startanimation() {
 
 if (popupwindow == null) {
 popupwindow = new popupwindow(mviewarray.get(selectposition), displaywidth, displayheight);
 popupwindow.setanimationstyle(r.style.popupwindowanimation);
 
 popupwindow.setfocusable(false);
 popupwindow.setoutsidetouchable(true);
 }
 
 if (selectedbutton.ischecked()) {
 if (!popupwindow.isshowing()) {
 showpopup(selectposition);
 } else {
 popupwindow.setondismisslistener(this);
 popupwindow.dismiss();
 hideview();
 }
 } else {
 if (popupwindow.isshowing()) {
 popupwindow.dismiss();
 hideview();
 }
 }
 }
 
 private void showpopup(int position) {
 view tview = mviewarray.get(selectposition).getchildat(0);
 if (tview instanceof viewbaseaction) {
 viewbaseaction f = (viewbaseaction) tview;
 f.show();
 }
 if (popupwindow.getcontentview() != mviewarray.get(position)) {
 popupwindow.setcontentview(mviewarray.get(position));
 }
 popupwindow.showasdropdown(this, 0, 0);
 }
 
 /**
 * 如果菜单成展开状态,则让菜单收回去
 */
 public boolean onpressback() {
 if (popupwindow != null && popupwindow.isshowing()) {
 popupwindow.dismiss();
 hideview();
 if (selectedbutton != null) {
 selectedbutton.setchecked(false);
 }
 return true;
 } else {
 return false;
 }
 
 }
 
 private void hideview() {
 view tview = mviewarray.get(selectposition).getchildat(0);
 if (tview instanceof viewbaseaction) {
 viewbaseaction f = (viewbaseaction) tview;
 f.hide();
 }
 }
 
 private void init(context context) {
 mcontext = context;
 displaywidth = ((activity) mcontext).getwindowmanager().getdefaultdisplay().getwidth();
 displayheight = ((activity) mcontext).getwindowmanager().getdefaultdisplay().getheight();
 setorientation(linearlayout.horizontal);
 }
 
 @override
 public void ondismiss() {
 showpopup(selectposition);
 popupwindow.setondismisslistener(null);
 }
 
 private onbuttonclicklistener monbuttonclicklistener;
 
 /**
 * 设置tabitem的点击监听事件
 */
 public void setonbuttonclicklistener(onbuttonclicklistener l) {
 monbuttonclicklistener = l;
 }
 
 /**
 * 自定义tabitem点击回调接口
 */
 public interface onbuttonclicklistener {
 public void onclick(int selectposition);
 }
 
}

这个代码基本就是对popupwindow进行了封装,通过对togglebutton按钮的监听来实现popupwindow的弹出和收回。

外部设置的话,也特别简单,只需要将自己定义好的布局传入到list集合中就可以。

下面是mainactivity中的代码

public class mainactivity extends appcompatactivity {
 
 
 private expandtabview expandtabview;
 private arraylist<view> mviewarray = new arraylist<view>();
 private viewleft viewleft;
 private viewmiddle viewmiddle;
 private viewright viewright;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initview();
  initvaule();
  initlistener();
 
 }
 
 private void initview() {
 
  expandtabview = (expandtabview) findviewbyid(r.id.expandtab_view);
  viewleft = new viewleft(this);
  viewmiddle = new viewmiddle(this);
  viewright = new viewright(this);
 
 }
 
 private void initvaule() {
  mviewarray.add(viewmiddle);
 mviewarray.add(viewleft);
 mviewarray.add(viewright);
  arraylist<string> mtextarray = new arraylist<string>();
  mtextarray.add("区域");
 mtextarray.add("距离");
 mtextarray.add("距离");
 
  expandtabview.setvalue(mtextarray, mviewarray);
// expandtabview.settitle(viewleft.getshowtext(), 0);
// expandtabview.settitle(viewmiddle.getshowtext(), 1);
// expandtabview.settitle(viewright.getshowtext(), 2);
 
 }
 
 private void initlistener() {
 
  viewleft.setonselectlistener(new viewleft.onselectlistener() {
 
   @override
   public void getvalue(string distance, string showtext) {
    onrefresh(viewleft, showtext);
   }
  });
 
  viewmiddle.setonselectlistener(new viewmiddle.onselectlistener() {
 
   @override
   public void getvalue(string showtext) {
 
    onrefresh(viewmiddle,showtext);
 
   }
  });
 
  viewright.setonselectlistener(new viewright.onselectlistener() {
 
   @override
   public void getvalue(string distance, string showtext) {
    onrefresh(viewright, showtext);
   }
  });
 
 }
 
 private void onrefresh(view view, string showtext) {
 
  expandtabview.onpressback();
  int position = getpositon(view);
  if (position >= 0 && !expandtabview.gettitle(position).equals(showtext)) {
   expandtabview.settitle(showtext, position);
  }
//  toast.maketext(mainactivity.this, showtext, toast.length_short).show();
 
 }
 
 private int getpositon(view tview) {
  for (int i = 0; i < mviewarray.size(); i++) {
   if (mviewarray.get(i) == tview) {
    return i;
   }
  }
  return -1;
 }
 
 @override
 public void onbackpressed() {
 
  if (!expandtabview.onpressback()) {
   finish();
  }
 
 }
 
}

以上就是这个筛选菜单栏的大致用法,个人感觉还是比较简单的,也比较灵活,修改起来也比较方便。

但是在项目中使用的时候碰到了一个问题,就是popupwindow在7.0的手机上弹出位置异常的问题,,查了一下,是因为手机状态栏高度的问题

于是重写了一下popupwindow的showasdropdown方法就解决了,下面是具体代码

@override
 public void showasdropdown(view anchor, int xoff, int yoff) {
  if(build.version.sdk_int >= 24) {
   rect rect = new rect();
   anchor.getglobalvisiblerect(rect);
   int h = anchor.getresources().getdisplaymetrics().heightpixels - rect.bottom;
   setheight(h);
  }
  super.showasdropdown(anchor, xoff, yoff);
 }

通过对sdk版本来进行判断,大于24的话就执行这个方法,解决了popupwindow在7.0手机上异常弹出的问题。

最后附上demo地址 

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