Android仿掌上英雄联盟首页,实现折叠效果

不单单是掌上英雄联盟,像微博发现页也用了这样的布局,当滑动到一定距离的时候,自动隐藏轮播图,或者标题栏下面的布局。并且使tablayout置顶。

与之相似的还有简书的个人页面也是这样的布局。

lol.gif

图片处理的有些不清楚。建议下载安装包自行查看效果

首页大概分为几个部分
  • 状态栏
  • 标题栏
  • 轮播图
  • 切换的Tab
  • 资讯列表
  • 资讯列表头部推荐
  • 刷新控件
lol.png

整个页面是一个Activity,最外层是刷新控件,然后是标题栏和折叠布局ScrollableLayout。

<com.cjj.MaterialRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.cpoopc.scrollablelayoutlib.ScrollableLayout
            android:id="@+id/scrollablelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            android:orientation="vertical">
            
        </com.cpoopc.scrollablelayoutlib.ScrollableLayout>

        <include layout="@layout/title_bar" />
    </RelativeLayout>
</com.cjj.MaterialRefreshLayout>

ScrollableLayout里面嵌套了轮播图、tablayout、viewpager。

        <com.cpoopc.scrollablelayoutlib.ScrollableLayout
            android:id="@+id/scrollablelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            android:orientation="vertical">
            <!--header-->
            <com.youth.banner.Banner
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <!--置顶的部分-->
            <android.support.design.widget.TabLayout
                android:id="@+id/tab"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/white"
                app:tabIndicatorColor="@color/tab_select"
                app:tabMode="scrollable"
                app:tabSelectedTextColor="@color/tab_select" />
            <!--滚动视图-->
            <android.support.v4.view.ViewPager
                android:id="@+id/vp"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </com.cpoopc.scrollablelayoutlib.ScrollableLayout>

然后切换是通过tab和viewpager联动加载的Fragment
Fragment中列表用的是RecyclerView,然后再给RecyclerView添加了一个Header,实现推荐功能。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:id="@+id/fragment_lv"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:nestedScrollingEnabled="false"
            android:layout_height="match_parent"/>

        <com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_gravity=""
            android:nestedScrollingEnabled="false"
            android:layout_height="60dp"/>
    </RelativeLayout>
</ScrollView>
创建布局需要注意的问题
  • ScrollView和RecyclerView滚动冲突,造成滑动不流畅。
    需要在RecyclerView设置android:nestedScrollingEnabled="false"属性,使滚动事件交给ScrollView处理。

  • 添加RecyclerViewHeader的时候,父布局只能识别RelativeLayout 、LinearLayout、和FrameLayout这三种控件。

  • ScrollableLayout子布局是固定的格式,分为三部分。

设置好布局后,进行数据的填充,先操作activty中的元素

实例化控件直接用ButterKnife一键绑定了。直接加载控件数据。

    private void initView() {
        //加载轮播图数据
        initBanner();
        //TabLayout
        initTabLayout();
        //创建Fragment
        initFragment();
        //监听滚动状态
        initOnClickScroll();
    }

随便在网上找了三张图片,使用Picasso框架完成图片的加载。
start开启轮播。
这时候打开app就能看到效果了。
该框架支持多种轮播样式风格,根据需要自己设置。

    /*轮播*/
    private void initBanner() {
        //圆形指示器
        header.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //指示器居中
        header.setIndicatorGravity(BannerConfig.CENTER);
        img.add("http://m.beequick.cn/static/bee/img/m/boot_logo-275a61e3.png");
        img.add("http://m.beequick.cn/static/bee/img/m/boot_logo-275a61e3.png");
        img.add("http://m.beequick.cn/static/bee/img/m/boot_logo-275a61e3.png");
        header.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Picasso.with(context)
                        .load(url)
                        .into(imageView);
            }
        });
        header.setImages(img);
        header.start();
    }

然后进行tablayout的初始化

    private String[] titles = new String[]{"最新", "专栏", "官方", "活动", "攻略", "娱乐", "收藏"};

    /*初始化tab标签*/
    private void initTabLayout() {

        for (int i=0;i<titles.length;i++){
            tab.addTab(tab.newTab().setText(titles[i]));
        }

    }

上面只是装载了标签数据,通过setupWithViewPager关联viewpager

    /*初始化Fragment*/
    private void initFragment() {

        ScrollableFragment fragment = new ScrollableFragment();
        ScrollableFragment fragment1 = new ScrollableFragment();
        ScrollableFragment fragment2 = new ScrollableFragment();
        ScrollableFragment fragment3 = new ScrollableFragment();
        ScrollableFragment fragment4 = new ScrollableFragment();
        ScrollableFragment fragment5 = new ScrollableFragment();
        ScrollableFragment fragment6 = new ScrollableFragment();
        fragmentList.add(fragment);
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        fragmentList.add(fragment5);
        fragmentList.add(fragment6);
        adapterVP = new ViewPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(adapterVP);
        tab.setupWithViewPager(vp);
    }

如果到这一步运行app,发现tab标签的状态或者颜色没有选中的效果,检查viewpager的adapter是否重写了getPageTitle方法

 public CharSequence getPageTitle(int position) {
            return titles[position];
        }
到这里已经完成了acticity的工作,但是我们还要实现标题栏渐变消失的效果。

在android中大多数跟滚动有关的控件,都有自己的滚动监听事件,来让开发者调用,以实现高级的效果。

而这里用的是ScrollableLayout控件,该控件内部也是基于ScrollView滚动,所以在内部给我们封装好了监听事件,直接调动监听方法就可以

  /*滚动监听*/
    private void initOnClickScroll() {
        scrollablelayout.setOnScrollListener(new ScrollableLayout.OnScrollListener() {
            @Override
            public void onScroll(int i, int i1) {
                if (i >= i1) {
                    title.setVisibility(View.GONE);
                } else {
                    title.setVisibility(View.VISIBLE);
                }
                //通过距离设置渐变效果
                float scale = (float) i1-i;
                float alpha = (255 * scale);
                float alpha2 = scale/i1*150;
                float alphaTv = scale / i1 * 255;
                title.setBackgroundColor(Color.argb((int) alpha2, 0, 0, 0));
                titleBarTitle.setTextColor(Color.argb((int) alphaTv, 198, 166, 102));
                titleBarContent.setTextColor(Color.argb((int) alphaTv,198,166,102));
            }
        });
    }

onScroll有两个属性,一个I是滚动的距离,是根据手势滑动的距离计算出的距离,i1是从开始滚动到header消失这之间的总距离。也就是固定的。

为了区别,这里加了标题栏的显示和隐藏,当底部滚动视图置顶的时候,也就是i=i1的时候,就把标题栏隐藏掉。

但是我们这里是需要一个渐变隐藏的效果,也就是让控件背景颜色从不透明到全透明的实时渐变的一个过程。

颜色需要用到argb,有四个参数,第一个就是透明度,
如果需要递增则用255 * scale
递减用scale / i1 * 255
需要半透的话,把255再除以2。

Fragment里面需要操作的东西就少了

两行代码就实现了headerview的添加

  private void initAdapter() {
        View headerView = View.inflate(getContext(), R.layout.view_header, null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        header.addView(headerView);
        header.attachTo(recyclerView);
        adapter = new FragmentAdapter(data, getActivity());
        //分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);
    }

总结

其实要实现这种效果的方法还有很多,比如利用Design库中的CoordinatorLayout,和AppBarLayout结合来用,也能实现折叠效果。

包括GitHub也有一些开源的库可供我们选择使用,像ScrollableLayout 、ObservableScrollView这些都是非常优秀的框架。

在实际项目中节省了很多开发时间。

唯一的时间成本就是我们学习这些框架的时间,当熟练运用之后,这些看似复杂的东西,可能只需要短短的几分钟而已。


关于

本文所用到的开源库:

    //recyclerview列表
    compile 'com.android.support:recyclerview-v7:25.0.0'
    //design库,用于tablayout,CoordinatorLayout折叠布局等
    compile 'com.android.support:design:25.0.0'
    //一键绑定控件
    compile 'com.jakewharton:butterknife:5.1.1'
    compile 'com.android.support:appcompat-v7:25.0.0'
    //网络请求
    compile 'com.squareup.picasso:picasso:2.5.2'
    //ConstraintLayout
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    //轮播控件
    compile 'com.youth.banner:banner:1.4.9'
    //刷新加载控件
    compile 'com.cjj.materialrefeshlayout:library:1.3.0'
    //折叠控件,解决了滚动冲突
    compile 'com.github.cpoopc:scrollablelayoutlib:1.0.1'
    //RecyclerViewHeader
    compile 'com.bartoszlipinski:recyclerviewheader2:2.0.1'

最后附上Demo地址:https://github.com/wapchief/imitationLOL

新的分支

添加了新的分支new,布局调整,使更接近于实际效果。
调整了header的位置,为了实现悬浮消失的效果,将header从recyclerView独立出来,放在了activity,并且对头部标题栏做了优化,
可以自助选择tab置顶的时候,标题栏是否消失。

并且在标题栏选项中增加了一个彩蛋,是仿美团外卖配送时折叠的效果。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,574评论 25 707
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,424评论 22 663
  • 最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库...
    OpenDigg阅读 17,047评论 6 223
  • 一滴露珠 渺小的可以忽略不计 许许多多 无数的汇积 滋润万物 一天的努力 微不足道 每天的勤奋 构建进步的阶梯
    百世随风阅读 115评论 0 0
  • 拖延是工作与生活的大敌,番茄工作法有效解决了拖延。 以25分钟为一个工作单位,在此期间内集中所有精力将手中事情做好...
    曲米粒阅读 154评论 0 0