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

Android 中实现ListView滑动隐藏标题栏的代码

程序员文章站 2024-02-21 17:04:34
布局中listview要覆盖标题栏 int mtouchslop = viewconfiguration.get(this).getscaledtouchslo...

布局中listview要覆盖标题栏

 int mtouchslop = viewconfiguration.get(this).getscaledtouchslop();
//滑动监听
showhidetitlebar(true);
listview standby_lv = (listview) findviewbyid(r.id.standby_lv);
standby_lv.setontouchlistener(new view.ontouchlistener() {
   @override
   public boolean ontouch(view v, motionevent event) {
    switch (event.getaction()) {
     case motionevent.action_down:
      mfirsty = event.gety();
      break;
     case motionevent.action_move:
      mcurrenty = event.gety();
      if (mcurrenty - mfirsty > mtouchslop) {
       // 下滑 显示titlebar
       showhidetitlebar(true);
      } else if (mfirsty - mcurrenty > mtouchslop) {
       // 上滑 隐藏titlebar
       showhidetitlebar(false);
      }
      break;
     case motionevent.action_up:
      break;
    }
    return false;
   }
  });
 private animator manimatortitle;
 private animator manimatortitlepage;
 private animator manimatorcontent;
 private void showhidetitlebar(boolean tag) {
  if (manimatortitle != null && manimatortitle.isrunning()) {
   manimatortitle.cancel();
  }
  if (manimatortitlepage != null && manimatortitlepage.isrunning()) {
   manimatortitlepage.cancel();
  }
  if (manimatorcontent != null && manimatorcontent.isrunning()) {
   manimatorcontent.cancel();
  }
  if (tag) {
   manimatortitle = objectanimator.offloat(mtitle, "translationy", mtitle.gettranslationy(), 0);
   manimatortitlepage = objectanimator.offloat(mtitlepage, "translationy", mtitlepage.gettranslationy(), 0);
   manimatorcontent = objectanimator.offloat(standby_lv, "translationy", standby_lv.gettranslationy(), getresources().getdimension(r.dimen.title_height));
  } else {
   manimatortitle = objectanimator.offloat(mtitle, "translationy", mtitle.gettranslationy(), -mtitle.getheight());
   manimatortitlepage = objectanimator.offloat(mtitlepage, "translationy", mtitlepage.gettranslationy(), -mtitlepage.getheight());
   manimatorcontent = objectanimator.offloat(standby_lv, "translationy", standby_lv.gettranslationy(), 0);
  }
  manimatortitle.start();
  manimatortitlepage.start();
  manimatorcontent.start();
 }

dimen.xml文件

<dimen name="titlepage_height">45dp</dimen>

以上所述是小编给大家介绍的android listview滑动隐藏标题栏的实例代码,希望对大家有所帮助