当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿美团网、大众点评购买框悬浮效果修改版

Android仿美团网、大众点评购买框悬浮效果修改版

2019年07月24日  | 移动技术网IT编程  | 我要评论

凹槽练字板,吴碧霞简历,多啦a梦粗口版

我之前写了一篇关于美团网,大众点评的购买框效果的文章android对scrollview滚动监听,实现美团、大众点评的购买悬浮效果,我自己感觉效果并不是很好,如果快速滑动界面,显示悬浮框的时候会出现一卡的现象,有些朋友说有时候会出现两个布局的情况,特别是对scrollview滚动的y值得监听,我还使用了handler来获取,还有朋友给我介绍了scrolling tricks这个东西,我下载试了下,确实美团网,大众点评的购买框用的是这种效果,但是scrolling tricks只能在api11以上使用,这个有点小悲剧,然后我做了下修改,并将实现思路分享给大家,实现起来很简单

首先还是要先对scrollview进行滚动监听,直接在onscrollchanged()方法中就能获取滚动的y值,之前那篇文章使用了handler,走弯路了,直接看代码吧

package com.example.meituandemo; 
 
import android.content.context; 
import android.util.attributeset; 
import android.widget.scrollview; 
/** 
 * @blog http://blog.csdn.net/xiaanming 
 * 
 * @author xiaanming 
 * 
 */ 
public class myscrollview extends scrollview { 
 private onscrolllistener onscrolllistener; 
 
 public myscrollview(context context) { 
 this(context, null); 
 } 
 
 public myscrollview(context context, attributeset attrs) { 
 this(context, attrs, 0); 
 } 
 
 public myscrollview(context context, attributeset attrs, int defstyle) { 
 super(context, attrs, defstyle); 
 } 
 
 /** 
 * 设置滚动接口 
 * @param onscrolllistener 
 */ 
 public void setonscrolllistener(onscrolllistener onscrolllistener) { 
 this.onscrolllistener = onscrolllistener; 
 } 
 
 
 @override 
 public int computeverticalscrollrange() { 
 return super.computeverticalscrollrange(); 
 } 
 
 
 @override 
 protected void onscrollchanged(int l, int t, int oldl, int oldt) { 
 super.onscrollchanged(l, t, oldl, oldt); 
 if(onscrolllistener != null){ 
  onscrolllistener.onscroll(t); 
 } 
 } 
 
 
 
 /** 
 * 
 * 滚动的回调接口 
 * 
 * @author xiaanming 
 * 
 */ 
 public interface onscrolllistener{ 
 /** 
  * 回调方法, 返回myscrollview滑动的y方向距离 
  * @param scrolly 
  *  、 
  */ 
 public void onscroll(int scrolly); 
 } 
 
 
 
} 

接下来看看主界面的布局文件

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/parent_layout" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 
 <imageview 
 android:id="@+id/imageview1" 
 android:layout_width="match_parent" 
 android:layout_height="45dip" 
 android:scaletype="centercrop" 
 android:src="@drawable/navigation_bar" /> 
 
 <com.example.meituandemo.myscrollview 
 android:id="@+id/scrollview" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" > 
 
 <framelayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" > 
 
  <linearlayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="vertical" > 
 
  <imageview 
   android:id="@+id/iamge" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:background="@drawable/pic" 
   android:scaletype="centercrop" /> 
 
  <include 
   android:id="@+id/buy" 
   layout="@layout/buy_layout" /> 
 
  <imageview 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:background="@drawable/one" 
   android:scaletype="centercrop" /> 
 
  <imageview 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:background="@drawable/one" 
   android:scaletype="centercrop" /> 
 
  <imageview 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:background="@drawable/one" 
   android:scaletype="centercrop" /> 
  </linearlayout> 
 
  <include 
  android:id="@+id/top_buy_layout" 
  layout="@layout/buy_layout" /> 
 </framelayout> 
 </com.example.meituandemo.myscrollview> 
 
</linearlayout> 

下面是布局的效果图:

从主界面的布局你可以看出,我们在上面放置了一个购买的布局,可能你会想,先让上面的布局隐藏起来,等下面的布局滑动上来就将其显示出来,如果这样子就跟我之前写的那篇文章差不多,效果不是很棒,所以这篇修改版的肯定不是这样子的,我们还是先看主界面的代码吧

package com.example.meituandemo; 
 
import android.app.activity; 
import android.os.bundle; 
import android.view.viewtreeobserver.ongloballayoutlistener; 
import android.widget.linearlayout; 
 
import com.example.meituandemo.myscrollview.onscrolllistener; 
 
/** 
 * @blog http://blog.csdn.net/xiaanming 
 * 
 * @author xiaanming 
 * 
 */ 
public class mainactivity extends activity implements onscrolllistener{ 
 /** 
 * 自定义的myscrollview 
 */ 
 private myscrollview myscrollview; 
 /** 
 * 在myscrollview里面的购买布局 
 */ 
 private linearlayout mbuylayout; 
 /** 
 * 位于顶部的购买布局 
 */ 
 private linearlayout mtopbuylayout; 
 
 
 @suppresswarnings("deprecation") 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
  
 myscrollview = (myscrollview) findviewbyid(r.id.scrollview); 
 mbuylayout = (linearlayout) findviewbyid(r.id.buy); 
 mtopbuylayout = (linearlayout) findviewbyid(r.id.top_buy_layout); 
  
 myscrollview.setonscrolllistener(this); 
  
 //当布局的状态或者控件的可见性发生改变回调的接口 
 findviewbyid(r.id.parent_layout).getviewtreeobserver().addongloballayoutlistener(new ongloballayoutlistener() { 
  
  @override 
  public void ongloballayout() { 
  //这一步很重要,使得上面的购买布局和下面的购买布局重合 
  onscroll(myscrollview.getscrolly()); 
   
  } 
 }); 
 } 
 
 
 
 
 @override 
 public void onscroll(int scrolly) { 
 int mbuylayout2parenttop = math.max(scrolly, mbuylayout.gettop()); 
 mtopbuylayout.layout(0, mbuylayout2parenttop, mtopbuylayout.getwidth(), mbuylayout2parenttop + mtopbuylayout.getheight()); 
 } 
 
 
 
} 

主界面就短短的几行代码,可能看完这些代码你还是没有明白到底是怎么做到的,没关系,我给大家说说,其实我们是让上面的购买布局和下面的购买布局重合起来了,layout()这个方法是确定view的大小和位置的,然后将其绘制出来,里面的四个参数分别是view的四个点的坐标,他的坐标不是相对屏幕的原点,而且相对于他的父布局来说的。

我们在主页面最外层的viewgroup添加了布局状态改变的监听器,当绘制完了屏幕会回调到方法ongloballayout()中,我们在ongloballayout()方法中手动调用了下onscroll()方法,刚开始myscrollview.getscrolly()等于0,所以说当scrolly小于mbuylayout.gettop()的时候,上面的购买布局的上边缘到myscrollview的上边缘的距离等于mbuylayout.gettop()(即下面布局的上边缘到myscrollview的上边缘)所以刚开始上面的购买布局和下面的购买布局重合了。

当myscrollview向上滚动,而上面购买布局的上边缘始终要和myscrollview的上边缘保持mbuylayout.gettop()这个距离,所以上面的购买布局也跟着向上滚动,当scrolly大于mbuylayout.gettop()的时候,表示购买布局上边缘滑动到了导航栏布局,所以此时购买布局的上边缘与myscrollview的上边缘始终要保持scrolly这个距离,所以购买布局才会一直在导航栏下面,就好像粘住了一样,不知道你了解了没有?好了,不过根据这种思路你也可以刚开始使用一个悬浮框来覆盖在下面的购买布局上面,然后onscroll()方法中更新悬浮框的位置,不过悬浮框的x,y不是相对于父布局的,这点要注意下,这样子也能实现效果,不过相对于此,要复杂的多,所以我们遇到类似的功能直接使用这种就行了,简洁明了,好了,你是不迫不及待的想看下效果,那我们接下来就运行下程序吧。

运行程序你会发现,无论我们怎么滑动,都不会出现之前那篇文章的那些情况,很流畅吧,这跟美团,大众点评的效果完全一致,好了,修改版的讲解就到这里结束了,有问题的请在下面留言,我会为大家解答的!

项目源码,

含有多个购买布局的效果,下一个购买布局会将上一个购买布局顶上去,使用方法也很简单,只需要将你需要设置的布局设置tag为sticky, 如

<framelayout 
 android:layout_width="fill_parent" 
 android:layout_height="100dip" 
 android:background="#ff00ffff" 
 android:tag="sticky" > 
 
 <button 
  android:id="@+id/button" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="button" /> 
 </framelayout> 

这样子这个布局滚动到了顶部就会粘在顶部,大家可以下载试试!

推荐大家下载下面的代码:,点击下载

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网