仿大總點評浮動效果
在大眾點評團購中,有這樣一個效果. 在具體的團購頁麵中商家圖片下有一個購買條,當用戶滾動團購詳情界麵的時候,購買條會停留在界麵的最上方. 具體效果如圖:
圖1
圖2
圖3
大家可以看到,大眾點評中,為了突出這個購買條,當向上滾動時,該滾動條會顯示在最上麵(如圖2),而當用戶滑動回來的時候,又可以恢複回第一張圖的樣子(如圖1).
下麵說一下具體的實現思路:
從這張圖,我們可以看下具體的布局.實際上在最頂部的位置,有一個購買條1,最開始的時候是隱藏的,而當從上向下滑動到具體位置的時候將購買條1顯示,將購買條2隱藏.
相反,當滑動回來的時候,講購買條2顯示,將購買條1隱藏.
核心的部分就是我們要去根據ScrollView的滑動高度去控製購買條的顯示與隱藏.這裏要注意的就是一定要判斷好這個滑動的高度,否則會出現不平滑的效果,影響用戶體驗.
看一下這張圖(畫得很醜,希望大家不介意),當上麵的原始視圖滑動到這個位置時,也就是剛好原來上麵的部分留在界麵中的剛好是購買條的高度時,我們需要將隱藏的購買條顯示出來,再將原來的購買條隱藏,這樣子就不會有突兀的效果,從而使效果變得平滑.當界麵從下向上的時候也是一樣,這裏不再複述.具體的還是大家看下代碼:
布局文件:
activity_main.xml:
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- xmlns:tools="https://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.tony.orderview.OrderView
- android:id="@+id/refreshview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#77aaaa" >
- <ScrollView
- android:id="@+id/scrollview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#accaac" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="250dip"
- android:background="@drawable/upload"
- android:text="one"
- android:textColor="#ffccee" />
- <include
- android:id="@+id/theview"
- layout="@layout/deal_buy_item" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="1250dip"
- android:background="@drawable/ic_tuan_info_bg_1"
- android:text="粥麵故事 僅售49元,超值享受哦" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:background="#ff0055"
- android:text="支持隨時退" />
- </LinearLayout>
- </ScrollView>
- </com.tony.orderview.OrderView>
- <include
- android:visibility="gone"
- android:id="@+id/theviewstay"
- layout="@layout/deal_buy_item" />
- </RelativeLayout>
購買條布局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="65.0dip"
- android:background="@drawable/ic_tuan_info_bg_1"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="0.0dip"
- android:layout_weight="1.0"
- android:gravity="center_vertical"
- android:orientation="horizontal"
- android:paddingLeft="10.0dip"
- android:paddingRight="10.0dip" >
- <LinearLayout
- android:layout_width="0.0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1.0"
- android:gravity="center_vertical"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="8.0dip"
- android:singleLine="true"
- android:textColor="#ffe55600"
- android:textSize="21.0sp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ellipsize="end"
- android:singleLine="true"
- android:textColor="#ff979797"
- android:textSize="12.0sp" />
- </LinearLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ellipsize="end"
- android:maxLines="1"
- android:minWidth="150.0dip"
- android:text="立即搶購"
- android:textAppearance="?android:textAppearanceMedium" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="1.0dip"
- android:background="@drawable/ic_tuan_info_bg_3" />
- </LinearLayout>
MainActivity:
- package com.tony.orderview;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.ScrollView;
- import com.example.stayview.R;
- import com.tony.orderview.OrderView.StayViewListener;
- public class MainActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- OrderView refreshableView = (OrderView) findViewById(R.id.refreshview);
- refreshableView.setStayView(findViewById(R.id.theview), (ScrollView)findViewById(R.id.scrollview),new StayViewListener() {
- @Override
- public void onStayViewShow() {
- //從下往上拉的時候回複顯示
- findViewById(R.id.theviewstay).setVisibility(View.VISIBLE);
- }
- @Override
- public void onStayViewGone() {
- //從上往下拉隱藏布局
- findViewById(R.id.theviewstay).setVisibility(View.GONE);
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
接著是最核心的部分,具體控製高度顯示隱藏,我是這樣做的,重寫了一個OrderView,套在整個布局外麵,然後計算ScrollView的滑動高度:
- package com.tony.orderview;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.ScrollView;
- import android.widget.Scroller;
- public class OrderView extends LinearLayout {
- private Scroller scroller;
- private Context mContext;
- private View stayView;
- private StayViewListener stayViewListener;
- private ScrollView scrollView;
- public void setStayView(View stayview,ScrollView scrollview,StayViewListener stayViewListener){
- this.stayView = stayview;
- this.scrollView = scrollview;
- this.stayViewListener = stayViewListener;
- }
- public OrderView(Context context) {
- super(context);
- mContext = context;
- }
- public OrderView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext = context;
- init();
- }
- private void init() {
- setOrientation(LinearLayout.VERTICAL);
- scroller = new Scroller(mContext);
- }
- /**
- *
- */
- boolean up = true;
- @Override
- public void computeScroll() {
- if(stayView!=null&&scrollView!=null&&stayViewListener!=null){
- int y = scrollView.getScrollY();
- if(up){
- int top = stayView.getTop();
- if(y>=top){
- stayViewListener.onStayViewShow();
- up = false;
- }
- }
- if(!up){
- int bottom = stayView.getBottom();
- if(y<=bottom-stayView.getHeight()){
- stayViewListener.onStayViewGone();
- up = true;
- }
- }
- }
- }
- public interface StayViewListener{
- public void onStayViewShow();
- public void onStayViewGone();
- }
- }
其實關於這種類似大眾點評購買條的停留效果,具體還可以有很多的做法,並不一定像我這樣自已定義View. 不過整體的思路還是不變,肯定還是要根據ScrollView的滾動高度來進行判斷. 無論用何種方式實現,一定要注意位置的控製,使該效果變得平滑,而不是突然購買條出現在界麵上. 具體的細節還有很多,回頭有時間再補上吧.
最後更新:2017-04-03 14:54:32