仿FlipBoard app效果1首頁懸浮窗

三星以前的手機自帶一個FlipBoard的app哈雏,就是個簡報吧。在主頁面手指右滑可以看到,新版本的samsung貌似取消這個app了贩疙,用bixby取代了讹弯。要看效果得去找個以前的samsung手機。
下圖是仿照寫的一個效果这溅,有點粗糙组民。


device-2018-05-23-113305.gif

來幾張圖片

剛進去的效果如下圖


image.png

稍微滾動下如下圖


image.png

繼續(xù)滾動
image.png

實現(xiàn)的思路

最上邊應(yīng)該就是個recyclerview,然后頂部帶了個懸浮條悲靴。
根據(jù)上邊的圖片分下臭胜,
最下層一個relativelayout,里邊是張圖片癞尚,文字耸三,然后我蓋了個view,用來處理滑動的時候透明度變暗的效果浇揩,
在上層就是個recyclerview仪壮,最頂部來個懸浮條。
至于recyclerview,我是給第一個item加了個itemDecoration临燃,offsetTop=底部那個relativelayout的高度睛驳。
分析完就開始代碼實現(xiàn)功能了。
題外話膜廊,這里要處理懸浮窗高度的變化乏沸,字體的變化,第一個item的時候懸浮窗的特殊處理
先上布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout
    android:id="@+id/layout_top_bg"
    android:layout_width="match_parent"
    android:layout_height="300dp">
    <ImageView
        android:id="@+id/iv_top_"
        android:src="@mipmap/pic11"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/tv_title"
        android:textColor="#fff"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_alignParentBottom="true"
        android:text="title................title..........title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_brief"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <View
        android:id="@+id/view_mask"
        android:background="#000000"
        android:alpha="0.0"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>
    <RelativeLayout
        android:id="@+id/layout_float_top1"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <ImageView
            android:id="@+id/iv_3dot1"
            android:layout_alignParentRight="true"
            android:layout_margin="3dp"
            android:src="@android:drawable/ic_input_add"
            android:layout_width="44dp"
            android:layout_height="44dp" />
        <TextView
            android:id="@+id/tv_float_title1"
            android:text="商業(yè)"
            android:textSize="26sp"
            android:textColor="#fff"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_float_sub_title1"
            android:text="8 articles"
            android:textColor="#fff"
            android:textSize="13sp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</FrameLayout>

實體類就簡單造了一個

data class BriefBean(var title:String,var newArticle: Int,var testContent:String,var color:Int)

item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:id="@+id/layout_float_top"
        android:layout_width="match_parent"
        android:layout_height="150dp">
        <ImageView
            android:id="@+id/iv_3dot"
            android:layout_alignParentRight="true"
            android:layout_margin="3dp"
            android:src="@android:drawable/ic_input_add"
            android:layout_width="46dp"
            android:layout_height="46dp" />
        <TextView
            android:id="@+id/tv_float_title"
            android:text="title"
            android:textSize="18sp"
            android:textColor="#fff"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_float_sub_title"
            android:text="8 articles"
            android:textColor="#fff"
            android:textSize="13sp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <TextView
        android:id="@+id/tv_placeholder"
        android:text="place holder"
        android:gravity="center"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="530dp" />
</LinearLayout>

activity的代碼如下

//我們頂部懸浮窗的高度是從150到50變化的爪瓜,因為recyclerview里那個高度默認是150
    var minHeight=50//頂部懸浮窗的最小高度
    var maxHeigth=150//最大高度.和item里的頂部分類高度一樣
    var originalHeigth=300//默認顯示的底圖的高度蹬跃,也是recyclerview第一個item的itemDecoration的top的高度
    var datas= arrayListOf<BriefBean>()
    private fun initRv(){
        datas.add(BriefBean("商業(yè)",8,"place holder business",Color.BLACK))
        datas.add(BriefBean("新聞",2,"place holder news",Color.RED))
        datas.add(BriefBean("科技",3,"place holder technology",Color.GREEN))
        datas.add(BriefBean("體育",6,"place holder sports",Color.BLUE))
        datas.add(BriefBean("娛樂",8,"place holder recreation",Color.YELLOW))
        rv_brief.apply {
            layoutManager=LinearLayoutManager(this@ActivityBriefSamsung)
            addItemDecoration(object :RecyclerView.ItemDecoration(){
                override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
                    var position=parent.getChildAdapterPosition(view)
                    if(position==0){
                        outRect.top=originalHeigth //只給第一個弄個偏移量,方便剛進去的時候能看到底部的圖片文字
                    }
                }
            })
            adapter=object :BaseRvAdapter<BriefBean>(datas){
                override fun getLayoutID(viewType: Int): Int {
                    return R.layout.item_brief_samsung
                }

                override fun onBindViewHolder(holder: BaseRvHolder, position: Int) {
                    var bean=getItemData(position)
                    holder.setText(R.id.tv_float_title,bean.title)
                    holder.setText(R.id.tv_float_sub_title,"${bean.newArticle} new articles")
                    holder.setText(R.id.tv_placeholder,bean.testContent)
                    holder.getView<View>(R.id.layout_float_top).apply {
                        visibility=if(position==0) View.GONE else View.VISIBLE//第一個不顯示頂部的分類條目的
                        setBackgroundColor(bean.color)
                    }
                }

            }
        }
        rv_brief.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

            }
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                 var manager=recyclerView.layoutManager as LinearLayoutManager
                var first=manager.findFirstVisibleItemPosition();
                var holder=recyclerView.findViewHolderForAdapterPosition(first)
                var firstTop=holder.itemView.top//第一個可見item的top铆铆,和屏幕頂部的距離
                var firstBottom=holder.itemView.bottom
                var layoutParams1=layout_float_top1.layoutParams//這個是頂部懸浮條的布局蝶缀,因為滑動的時候要修改
                var data=datas[first]


                if(first==0){//第一條數(shù)據(jù)和后邊有點區(qū)別,單獨處理
                    if(firstTop>=minHeight){
                        layoutParams1.height=minHeight//剛開始的時候上邊的標題也就文字從透明到不透明薄货,而高度是沒變化的翁都,
                        layout_float_top1.setBackgroundColor(Color.TRANSPARENT)//背景開始是透明的
                        var alpha=1-(firstTop-minHeight)*1f/(originalHeigth-minHeight)//根據(jù)滑動距離來修改透明度。
                        tv_float_title1.alpha=alpha
                        view_mask.alpha=alpha
                        view_mask.y=firstTop-originalHeigth*1f
                        iv_top_.scrollTo(0,-(firstTop-originalHeigth)/2)//滾動底圖谅猾,用實際的一半距離柄慰。
                    }else{
                        view_mask.y=-originalHeigth*1f//把mask這個view滾到屏幕外邊去,免得影響
                        layout_float_top1.setBackgroundColor(data.color)/修改懸浮條顏色
                    }
                    tv_float_title1.setTextSize(TypedValue.COMPLEX_UNIT_SP,26f)//實際體驗發(fā)現(xiàn)首個黑色的背景標題字體顏色比較大
                    tv_float_sub_title1.setText("")
                }else{

                    var changeHeight=maxHeigth+firstTop
                    layoutParams1.height=Math.max(changeHeight,minHeight)
                    //字體大小從18sp到15sp變化税娜,而懸浮窗高度從maxheight到minHeight變化
                    tv_float_title1.setTextSize(TypedValue.COMPLEX_UNIT_SP,15f+3f*(layoutParams1.height-minHeight)/(maxHeigth-minHeight))
                    layout_float_top1.setBackgroundColor(data.color)
                    tv_float_sub_title1.setText("${data.newArticle} new articles")
                    tv_float_sub_title1.alpha=Math.max(0f,1-(maxHeigth-layoutParams1.height)/40f)//滑動個40dp就讓子標題不可見
                }
                tv_float_title1.setText(data.title)
                if(firstBottom<=minHeight){//這里處理懸浮窗往上滾出屏幕的效果
                    layout_float_top1.y=firstBottom-minHeight*1f
                    layoutParams1.height=minHeight
                }else{
                    layout_float_top1.y=0f
                }
                layout_float_top1.layoutParams=layoutParams1
         
            }
        })

    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坐搔,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子敬矩,更是在濱河造成了極大的恐慌概行,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弧岳,死亡現(xiàn)場離奇詭異凳忙,居然都是意外死亡业踏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門消略,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堡称,“玉大人,你說我怎么就攤上這事艺演∪唇簦” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵胎撤,是天一觀的道長晓殊。 經(jīng)常有香客問我,道長伤提,這世上最難降的妖魔是什么巫俺? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮肿男,結(jié)果婚禮上介汹,老公的妹妹穿的比我還像新娘。我一直安慰自己舶沛,他們只是感情好嘹承,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著如庭,像睡著了一般叹卷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上坪它,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天骤竹,我揣著相機與錄音,去河邊找鬼往毡。 笑死蒙揣,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的开瞭。 我是一名探鬼主播懒震,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼惩阶!你這毒婦竟也來了挎狸?” 一聲冷哼從身側(cè)響起扣汪,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤断楷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后崭别,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體冬筒,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡恐锣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了舞痰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片土榴。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖响牛,靈堂內(nèi)的尸體忽然破棺而出玷禽,到底是詐尸還是另有隱情,我是刑警寧澤呀打,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布矢赁,位于F島的核電站,受9級特大地震影響贬丛,放射性物質(zhì)發(fā)生泄漏撩银。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一豺憔、第九天 我趴在偏房一處隱蔽的房頂上張望额获。 院中可真熱鬧,春花似錦恭应、人聲如沸抄邀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撤摸。三九已至,卻和暖如春褒纲,著一層夾襖步出監(jiān)牢的瞬間准夷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工莺掠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留衫嵌,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓彻秆,卻偏偏與公主長得像楔绞,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子唇兑,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容