Kotlin 实现新闻时间轴效果

简介

最近做一个新闻列表功能时,设计师设计了一个时间轴新闻列表,同一个日期第一条显示月日,并且滑动到顶端时悬浮,效果如下:


效果图

看了效果之后,考虑过第一条可见悬停,发现右侧还有时间且可以看到整个滑动过程,所以这条方案就pass,还是使用最简单实现方式,悬浮的时间布局在Activity布局上。

实现

Activity布局

RecyclerView与时间空间叠加布局,LinearLayout包裹可以控制布局在父布局的位置,保证RecycleView第一个item与其重合,这样视觉看不出是同一个。

<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--内容-->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rl_new"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!--悬浮时间控件-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dp_10"
            android:layout_marginLeft="@dimen/dp_10"
            android:paddingTop="@dimen/dp_10">

            <include layout="@layout/item_time_layout" />
        </LinearLayout>

    </FrameLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
悬浮时间空间布局

悬浮控件可自定义不同颜色样式

<?xml version="1.0" encoding="utf-8"?>
<!--            时间框-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/time_layout"
    android:layout_width="@dimen/dp_30"
    android:layout_height="wrap_content"
    android:background="@drawable/time_bg_color"
    android:paddingStart="@dimen/dp_5"
    android:paddingEnd="@dimen/dp_5">

    <!--            日-->
    <TextView
        android:id="@+id/tv_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:maxLines="2"
        android:text="@string/defalut_"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/text_size_12"
        android:textStyle="bold" />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_day"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-2dp">

        <!--                月-->
        <TextView
            android:id="@+id/tv_month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:text="@string/defalut_"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/text_size_10" />

        <!--                月单位-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:text="@string/month"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/text_size_10" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</RelativeLayout>
Adapter布局

其中父布局使用LinearLayout可以动态的根据子控件的内容宽度决定🔗线的长度,考虑item之间需用细线连接,父布局上下不能出现间隔样式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/dp_10"
    android:layout_marginEnd="@dimen/dp_10"
    android:orientation="horizontal">

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

        <!-- 普通时间线-->
        <LinearLayout
            android:id="@+id/rl_line"
            android:layout_width="@dimen/dp_30"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <!--            上半部分竖线-->
            <View
                android:id="@+id/view_top"
                android:layout_width="@dimen/dp_1"
                android:layout_height="@dimen/dp_10"
                android:layout_gravity="center_horizontal"
                android:background="@color/colorAccent" />

            <!--            中间点-->
            <ImageView
                android:id="@+id/dot_img"
                android:layout_width="@dimen/dp_10"
                android:layout_height="@dimen/dp_10"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@string/contentDescription"
                android:src="@drawable/ic_hongdian" />

            <!--下半部分竖线-->
            <View
                android:id="@+id/v_line"
                android:layout_width="@dimen/dp_1"
                android:layout_height="0dp"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:background="@color/colorAccent" />

        </LinearLayout>

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/ll_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="-10dp"
            android:layout_marginLeft="-10dp"
            android:layout_marginTop="@dimen/dp_5"
            android:layout_toEndOf="@+id/rl_line"
            android:layout_toRightOf="@+id/rl_line"
            android:orientation="horizontal">
            <!--横向线-->
            <View
                android:id="@+id/view_line"
                android:layout_width="@dimen/dp_20"
                android:layout_height="@dimen/dp_1"
                android:layout_gravity="center_vertical"
                android:layout_toEndOf="@+id/dot_img"
                android:layout_toRightOf="@+id/dot_img"
                android:background="@color/colorAccent" />

            <!--详细时间-->
            <TextView
                android:id="@+id/tv_title_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/view_line"
                android:layout_toRightOf="@+id/view_line"
                android:background="@drawable/shape_time_bg"
                android:gravity="center"
                android:paddingLeft="@dimen/dp_8"
                android:paddingTop="@dimen/dp_2"
                android:paddingRight="@dimen/dp_8"
                android:paddingBottom="@dimen/dp_2"
                android:text="@string/defalut_"
                android:textColor="@android:color/white"
                android:textSize="@dimen/text_size_10" />
        </androidx.appcompat.widget.LinearLayoutCompat>

        <!--            时间框-->
        <include layout="@layout/item_time_layout" />

        <!--    内容-->
        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ll_time"
            android:layout_marginStart="@dimen/dp_10"
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_marginTop="@dimen/dp_5"
            android:layout_toEndOf="@+id/rl_line"
            android:layout_toRightOf="@+id/rl_line"
            android:orientation="vertical"
            android:paddingBottom="@dimen/dp_8">

            <!--        新闻标题-->
            <TextView
                android:textColor="@android:color/black"
                android:id="@+id/tv_news_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:lineSpacingExtra="@dimen/dp_5"
                android:maxLines="2"
                android:textSize="@dimen/text_size_14" />

            <!--            底部详细信息-->
            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_5"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_tag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/dp_5"
                    android:layout_marginRight="@dimen/dp_5"
                    android:textSize="@dimen/text_size_12" />

                <TextView
                    android:id="@+id/tv_source"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/dp_5"
                    android:layout_marginRight="@dimen/dp_5"
                    android:textSize="@dimen/text_size_12" />

                <TextView
                    android:id="@+id/tv_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/dp_5"
                    android:layout_marginLeft="@dimen/dp_5"
                    android:textSize="@dimen/text_size_12" />
            </androidx.appcompat.widget.LinearLayoutCompat>

            <!--        新闻图片-->
            <ImageView
                android:id="@+id/new_pic"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_80"
                android:layout_marginTop="@dimen/dp_8"
                android:contentDescription="@string/contentDescription"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher" />
        </androidx.appcompat.widget.LinearLayoutCompat>
    </RelativeLayout>
</LinearLayout>
Adapter数据绑定

这里我们为了简化adapter的使用,依赖

com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.0-beta11

在里面实现时间框的显示、隐藏,时间标签的颜色改变,条目内容的数据绑定,内容详情显示隐藏,图片显示

package com.zj.timeaxis

/**
 * 新闻适配器
 *
 * @author zj
 */
class NewsListAdapter(layoutResId: Int) :
    BaseQuickAdapter<NewsBean?, BaseViewHolder>(layoutResId) {
    @SuppressLint("SimpleDateFormat")
    var sdfDay = SimpleDateFormat("yyyy-MM-dd")
    @SuppressLint("SimpleDateFormat")
    var sdfHour = SimpleDateFormat("HH:mm:ss")


    override fun convert(helper: BaseViewHolder, item: NewsBean?) {
        item?.let {
            val position = helper.adapterPosition
            //前个条目时间
            val datePre: Long = data[Math.max(position - 1, 0)]!!.time
            //当前条目
            val date: Long = item.time

            val ca: Calendar = Calendar.getInstance()
            ca.timeInMillis = date

            //当前条目时间
            val caPre: Calendar = Calendar.getInstance()
            caPre.timeInMillis = datePre

            //item绑定数据
            helper.setText(R.id.tv_news_title, item.title)
                .setText(R.id.tv_title_time, sdfHour.format(ca.time))
                .setText(R.id.tv_source, item.source)

            val viewTop: View = helper.getView(R.id.view_top)
            val monthRl: RelativeLayout = helper.getView(R.id.time_layout)
            val tvDay: TextView = helper.getView(R.id.tv_day)
            val tvMonth: TextView = helper.getView(R.id.tv_month)
            val tvTAG: TextView = helper.getView(R.id.tv_tag)
            val titleTime: TextView = helper.getView(R.id.tv_title_time)
            if (0 == position) {
                //设置首个条目上间距
                helper.itemView.setPadding(0, ScreenUtil.dip2px(context, 10f), 0, 0)
                val color = ContextCompat.getColor(context, android.R.color.holo_red_light)
                tvTAG.setTextColor(color)
                tvTAG.text = "推荐"
                tvTAG.visibility = View.VISIBLE
                titleTime.setBackgroundResource(R.drawable.shape_time_bg)
            } else if (1 == position) {
                helper.itemView.setPadding(0, 0, 0, 0)
                val color = ContextCompat.getColor(context, android.R.color.holo_blue_bright)
                tvTAG.setTextColor(color)
                tvTAG.text = "热门"
                tvTAG.visibility = View.VISIBLE
                titleTime.setBackgroundResource(R.drawable.shape_time_bg_grey)
            } else { //防止复用设置间距
                helper.itemView.setPadding(0, 0, 0, 0)
                tvTAG.visibility = View.GONE
                titleTime.setBackgroundResource(R.drawable.shape_time_bg_grey)
            }

            //时间框绑定
            val dayInt = ca[Calendar.DATE]
            val day = if (dayInt < 10) "0$dayInt" else dayInt.toString()
            val monthInt = ca[Calendar.MONTH] + 1
            val month = monthInt.toString()

            if (0 == position) {
                //红点
                helper.setImageResource(R.id.dot_img, R.drawable.ic_hongdian)
                //上画线
                viewTop.visibility = View.INVISIBLE
                monthRl.visibility = View.VISIBLE
                tvDay.text = day
                tvMonth.text = month
            } else {
                viewTop.visibility = View.VISIBLE
                //判断前后时间是否一致
                val condition = TextUtils.equals(sdfDay.format(ca.time), sdfDay.format(caPre.time))
                if (condition) {
                    helper.setImageResource(R.id.dot_img, R.drawable.ic_huidian)
                    //这里为了区分是不是没有还是临时隐藏
                    monthRl.visibility = View.GONE
                } else {
                    helper.setImageResource(R.id.dot_img, R.drawable.ic_hongdian)
                    titleTime.setBackgroundResource(R.drawable.shape_time_bg)
                    monthRl.visibility = View.VISIBLE
                    tvDay.text = day
                    tvMonth.text = month
                }
            }
        }
    }
}
悬浮时间框的逻辑控制
package com.zj.timeaxis

class MainActivity : AppCompatActivity() {
    lateinit var adpter: NewsListAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
         // 这里为了区别adapter布局时间控件的id使用findViewById
        val timeLayout = findViewById<RelativeLayout>(R.id.time_layout)
        val tvDay = findViewById<TextView>(R.id.tv_day)
        val tvMonth = findViewById<TextView>(R.id.tv_month)

        //设置下拉刷新
        refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshlayout: RefreshLayout) {
                val initData = initData()
                adpter.setNewData(initData)
                refreshlayout.finishRefresh(2000 /*,false*/) //传入false表示刷新失败
            }
        })

        //上拉加载更多
        refreshLayout.setOnLoadMoreListener(object : OnLoadMoreListener {
            override fun onLoadMore(refreshlayout: RefreshLayout) {
                val initData = initData()
                initData?.let { adpter.addData(it) }
                refreshlayout.finishLoadMore(2000 /*,false*/) //传入false表示加载失败
            }
        })
        
        //初始化RecycleView
        val linearLayoutManager = LinearLayoutManager(this)
        rl_new.layoutManager = linearLayoutManager
        adpter = NewsListAdapter(R.layout.item_layout_new)
        rl_new.adapter = adpter
        // 当RecycleView初始化完成复制悬浮框
        rl_new.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                if (linearLayoutManager.itemCount > 0) {
                    timeLayout.visibility = View.VISIBLE
                    val data = adpter.data
                    //获取第一个可见条目的position
                    val firstVisPos = linearLayoutManager.findFirstVisibleItemPosition()
                    //获取第一个可见条目的position的数据
                    val itemData = data.get(firstVisPos)

                    //格式化时间
                    val ca: Calendar = Calendar.getInstance()
                    ca.timeInMillis = itemData!!.time
                    val dayInt = ca[Calendar.DATE]
                    val day = if (dayInt < 10) "0$dayInt" else dayInt.toString()
                    val monthInt = ca[Calendar.MONTH] + 1
                    val month = monthInt.toString()

                    //时间控件赋值
                    tvDay.text = day
                    tvMonth.text = month
                } else {  
                    // 没有数据隐藏时间悬浮
                    timeLayout.visibility = View.GONE
                }
            }
        })

        rl_new.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            var linearLayoutManager = rl_new.getLayoutManager() as LinearLayoutManager
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                //获取第一个可见条目的position
                val firstVisPos = linearLayoutManager.findFirstVisibleItemPosition()
                //下滑情况下   下滑为负值  上滑为正值
                val firstVisiableChildView: View? =
                    linearLayoutManager.findViewByPosition(firstVisPos)             //gridLayoutManager布局管理器

                val timeLayout: RelativeLayout? =
                    adpter.getViewByPosition(firstVisPos, R.id.time_layout) as RelativeLayout

                //获取item条目的高度
                val itemHeight = firstVisiableChildView?.getHeight();

                //第一个条目标签滑动瞬间隐藏
                if (firstVisiableChildView!!.getTop() < 0) {
                    //如果显示的状态隐藏 GONE的item不变
                    if (timeLayout?.visibility == View.VISIBLE) {
                        //设置成INVISIBLE 为了区分没有标签和暂时隐藏
                        timeLayout.visibility = View.INVISIBLE
                    }
                }

                //当条目数量大于2个考虑下个显示情况
                if (linearLayoutManager.itemCount > 1) {
                    //获取第一个可见item的下一个
                    val nextVisPos = firstVisPos + 1
                    val timeLayoutNext: RelativeLayout? =
                        adpter.getViewByPosition(nextVisPos, R.id.time_layout) as RelativeLayout

                    //除第一个之外的包含标签的隐藏   Activity 浮动的标签距离顶部10dp
                    if (itemHeight!! + firstVisiableChildView.getTop() <= ScreenUtil.dip2px(
                            this@MainActivity,
                            10f
                        )
                    ) {
                        //滑倒标签位置隐藏 GONE的item不变
                        if (timeLayoutNext?.visibility == View.VISIBLE) {
                            timeLayoutNext.visibility = View.INVISIBLE
                        }
                    } else {
                        //滑出标签位置显示 GONE的item不变
                        if (timeLayoutNext?.visibility == View.INVISIBLE) {
                            timeLayoutNext.visibility = View.VISIBLE
                        }
                    }
                }
            }
        })

        val initData = initData()
        adpter.setNewData(initData)
    }

    /**
     * 模拟不同天的数据
     */
    fun initData(): MutableList<NewsBean?>? {
        val newList: MutableList<NewsBean?> = ArrayList();
        for (index in 1..100) {
            val new: NewsBean;
            when (index / 10) {
                0 -> {
                    new = NewsBean(System.currentTimeMillis(), "新闻标题" + index, "CCTV")
                }
                1 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000, "新闻标题" + index, "湖南卫视")
                }
                2 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 2, "新闻标题" + index, "江苏卫视")
                }
                3 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 3, "新闻标题" + index, "浙江卫视")
                }
                4 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 4, "新闻标题" + index, "江苏卫视")
                }
                5 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 5, "新闻标题" + index, "江苏卫视")
                }
                6 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 6, "新闻标题" + index, "江苏卫视")
                }
                7 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 7, "新闻标题" + index, "江苏卫视")
                }
                8 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 8, "新闻标题" + index, "江苏卫视")
                }
                9 -> {
                    new = NewsBean(System.currentTimeMillis() - 100000000 * 9, "新闻标题" + index, "江苏卫视")
                }
                else -> new =
                    NewsBean(System.currentTimeMillis() - 100000000 * 10, "新闻标题" + index, "CCTV")
            }

            newList.add(new)
        }
        return newList
    }
}

结束

喜欢您就STAR一下~
github 地址

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