安卓和iOS实现上滑停靠顶部悬浮框

一言不合就上图

Android_view.gif
iOS_view.gif

1:安卓

很早以前写过一个类似的效果,不过当时的做法是:

  • 在FrameLayout中布局listview和你想要在滑动时显示在顶部的布局并将属性设置为invisible。
  • 在代码中动态添加想要悬停的布局为listview头布局(也就是悬停部分写两遍)。
  • 监听listview的滚动(滚动时显示在布局中隐藏的部分达到悬停效果)
    @Override
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem >= 1) {
                    //悬浮布局显示
                    invis.setVisibility(View.VISIBLE);
                } else {
                    invis.setVisibility(View.GONE);
                }
            }

缺点:

  • 布局不够灵活
  • 不能处理事件(如果想要作为多个fragment的滑动指示器不能实现)。

安卓5.0后实现这样的布局就很简单了而且还能处理各种事件

做法:

  • layout代码
      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:background="@color/white"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:elevation="0dp">
        <!--
            app:layout_scrollFlags="scroll"
            加此属性可滚动  不加就固定在顶部-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"
            app:layout_scrollFlags="scroll"
            android:background="@color/main_Green">

            <TextView
                android:id="@+id/nav_tv_title"
                style="@style/ShowTv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="在线测评"
                android:textColor="@color/white" />
        </LinearLayout>

        <com.bigkoo.convenientbanner.ConvenientBanner
            android:id="@+id/banner"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:background="@mipmap/banner_loading"
            app:canLoop="true"
            app:layout_scrollFlags="scroll" />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@android:color/darker_gray" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="#f0f0f0"></LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="130dp"
                android:layout_weight="1"
                android:id="@+id/ll_consult"
                android:gravity="center_horizontal"
                android:background="@drawable/btn_bg_main"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:src="@mipmap/consult_icon"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="1dp"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="心理咨询师考试"
                        android:textColor="#3399cc"
                        style="@style/ShowTv2"/>
                    <TextView
                        android:layout_marginTop="5dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="做自己的咨询师"
                        android:textColor="@color/default_gray"
                        style="@style/ShowTv4"/>
                </LinearLayout>

            </LinearLayout>
            <ImageView
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray" />
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/ll_hunyin"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/btn_bg_main"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp">

                    <ImageView
                        android:id="@+id/iv_home_icon_1"
                        android:layout_width="44dp"
                        android:layout_height="44dp"
                        android:src="@mipmap/hunyin_icon" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/tv_home_lable_1"
                            style="@style/ShowTv3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#f11e8a"
                            android:text="婚姻家庭咨询师" />

                        <TextView
                            android:id="@+id/tv_home_contemt_1"
                            style="ShowTv5"
                            android:layout_marginTop="5dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="为幸福服务"
                            android:textColor="@color/default_gray" />


                    </LinearLayout>
                </LinearLayout>

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:background="@android:color/darker_gray" />

                <LinearLayout
                    android:id="@+id/ll_yingyang"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/btn_bg_main"
                    android:focusable="true"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp">

                    <ImageView
                        android:id="@+id/iv_home_icon_3"
                        android:layout_width="44dp"
                        android:layout_height="44dp"
                        android:focusable="false"
                        android:src="@mipmap/yingyang_icon1" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:focusable="false"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/tv_home_lable_3"
                            style="ShowTv3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:focusable="false"
                            android:textColor="#ff742f"
                            android:text="营养师资格证" />

                        <TextView
                            android:id="@+id/tv_home_contemt_3"
                            style="ShowTv5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:focusable="false"
                            android:text="与健康同行"
                            android:layout_marginTop="5dp"
                            android:textColor="@color/default_gray" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="#f0f0f0"></LinearLayout>

        <View
            android:id="@+id/line_divider"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@android:color/darker_gray"
            android:visibility="gone"
            />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/vertical_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</android.support.design.widget.CoordinatorLayout>
layout.png

注意点:

  • 1:新属性app:layout_scrollFlags的使用,对应的值为:scroll,enterAlways,enterAlwaysCollapsed,snap,exitUntilCollapsed;也可以在代码中通过setScrollFlags(int)方法使用,app:layout_scrollFlags="scroll"就会跟着一起滚动如果不设置则在滑动的时候会悬停在顶部。
  • 2: 下面必须使用RecyclerView。

页面代码

package com.ygxinjian.anhui.test_online.Fragment;


import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.adapter.CBPageAdapter;
import com.bigkoo.convenientbanner.adapter.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.ygxinjian.anhui.test_online.Activity.Consult_Test_Activity;
import com.ygxinjian.anhui.test_online.Activity.Consult_Test_intros_Activity;
import com.ygxinjian.anhui.test_online.Controller.NetworkImageHolderView;
import com.ygxinjian.anhui.test_online.Model.JsEntry;
import com.ygxinjian.anhui.test_online.R;
import com.ygxinjian.anhui.test_online.TestOnlineApplication;
import com.ygxinjian.anhui.test_online.View.DividerItemDecoration;
import com.ygxinjian.anhui.test_online.utils.DevUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Created by handongqiang on 16/12/29.
 */

public class Fragment_Home extends BaseFragment implements View.OnClickListener{
    private static final String TAG = "Fragment_Home";
    private ConvenientBanner mConvenientBanner;
    private RecyclerView mRecyclerView;
    private AppBarLayout mAppBarLayout;
    private View mLine;
    private NewsAdapter mAdapter;
    private LinearLayout ll_consult_click,ll_hunying_click,ll_yingyang_click;
    List<String> mData;
    private String[] images = {"http://img2.imgtn.bdimg.com/it/u=3093785514,1341050958&fm=21&gp=0.jpg",
            "http://img2.3lian.com/2014/f2/37/d/40.jpg",
            "http://img2.3lian.com/2014/f2/37/d/39.jpg",
            "http://f.hiphotos.baidu.com/image/h%3D200/sign=1478eb74d5a20cf45990f9df460b4b0c/d058ccbf6c81800a5422e5fdb43533fa838b4779.jpg",
            "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg"
    };

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_home, null);

        ll_consult_click = (LinearLayout) view.findViewById(R.id.ll_consult);
        ll_hunying_click = (LinearLayout) view.findViewById(R.id.ll_hunyin);
        ll_yingyang_click = (LinearLayout) view.findViewById(R.id.ll_yingyang);
        ll_consult_click.setOnClickListener(this);
        ll_hunying_click.setOnClickListener(this);
        ll_yingyang_click.setOnClickListener(this);

        mConvenientBanner = (ConvenientBanner) view.findViewById(R.id.banner);
        mAppBarLayout = (AppBarLayout) view.findViewById(R.id.appbar_layout);
        mLine = view.findViewById(R.id.line_divider);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.vertical_recyclerView);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mAdapter = new NewsAdapter(getActivity());
        //添加头尾布局
//        mAdapter.addHeaderView(getView());
        mConvenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, Arrays.asList(images))
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);

        initAdapter();
        initBanner();
        initLine();
        return view;
    }

    private void initLine() {
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if(Math.abs(verticalOffset) >= mAppBarLayout.getTotalScrollRange()){
                    mLine.setVisibility(View.VISIBLE);
                }else{
                    mLine.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initBanner() {
        mConvenientBanner.startTurning(3000);
        // 进入Banner
        mConvenientBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                DevUtil.showShortInfo(mActivity,"心理咨询师考试");
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_consult:
                DevUtil.gotoActivity(mActivity, Consult_Test_Activity.class);
                break;
            case R.id.ll_hunyin:
                DevUtil.gotoActivity(mActivity, Consult_Test_intros_Activity.class);
                break;
            case R.id.ll_yingyang:
                DevUtil.showShortInfo(mActivity,"题库整理中");
                break;
        }
    }

    /**
     * 模拟首页数据
     * @return
     */
    private List<JsEntry> mockData(){
        List<JsEntry> data = new ArrayList<>();
        JsEntry jsEntry = new JsEntry();
        jsEntry.time = "15小时前";
        jsEntry.title = "这些情商的技巧,你是不是都掌握了?";
        jsEntry.authorName = "JayChou";
        jsEntry.cover ="http://upload-images.jianshu.io/upload_images/2785318-5306a632b46a8c27.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/1020/q/80";
        JsEntry jsEntry2 = new JsEntry();
        jsEntry2.time = "10小时前";
        jsEntry2.title = "除了阴谋,《锦绣未央》里还有哪些温情?";
        jsEntry2.authorName = "菇凉似梦";
        jsEntry2.cover = "http://upload-images.jianshu.io/upload_images/2881988-b217e714eb05f88e.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/1020/q/80";
        for (int i=0;i<20;i++){
            if(i % 2 == 0){
                data.add(jsEntry);
            }else{
                data.add(jsEntry2);
            }
        }
        return data;
    }


    /**
     * 设置RecyclerView属性
     */
    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);//设置adapter
        //设置item点击事件
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(mActivity, ""+position, Toast.LENGTH_SHORT).show();

            }
        });
    }
    /**
     * RecycleView的adapter
     */
    class NewsAdapter extends BaseQuickAdapter<JsEntry> {

        public NewsAdapter(Context context) {
            super(context,R.layout.news_label_item,mockData());
        }

        @Override
        public void convert(BaseViewHolder helper, JsEntry mData) {
            helper.setText(R.id.item_content, mData.title)
                    .setText(R.id.author_name, mData.authorName)
                    .setText(R.id.publish_time, mData.time);
            ImageLoader.getInstance().displayImage(mData.cover, (ImageView) helper.getView(R.id.cover));
        }
    }

}

至此安卓端这个简单的效果就实现了


2:iOS端实现方法

思路:

  • 最底层为UIScrollView,布局上依次添加banner,悬停的UIView布局和Tableview.
  • 通过代码判断UIScrollView的滚动实现悬停

核心代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //悬浮框
    if (_bottomScrollView.contentOffset.y < self.banner.bounds.size.height) {
        _bottomScrollView.delegate = nil;
        CGPoint contentOffsetPoint = _bottomScrollView.contentOffset;
        _bottomScrollView.contentOffset = contentOffsetPoint;
        _bottomScrollView.delegate = self;
        m_tabel.scrollEnabled =NO; //设置tableview 不能滚动
    }else if(_bottomScrollView.contentOffset.y > self.banner.bounds.size.height){
        m_tabel.scrollEnabled =YES;
        _bottomScrollView.delegate = nil;
        _bottomScrollView.contentOffset = CGPointMake(0, self.banner.bounds.size.height);
        _bottomScrollView.delegate = self;
    }
}
老规矩-喜欢就点个赞呗 T.T .gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容