Android仿智联详情

简介: Android仿智联详情

最近在投简历的时候发现智联招聘的公司详情页设计的很有意思,虽然很简单,但是也忍不住仿一下,

智联的效果

image.png

仿的效果(基本效果一致)

image.png

效果需求分析:

1、刚进入界面时,“在招职位”布局在底部露头,并距离屏幕左边一半屏幕宽度的距离。

2、当详情滑动到最底部,然后再向上滑动时,展开“在招职位”布局。

3、“在招职位”布局支持手势拖动,向上时布局距离屏幕左边越来越近(最终距离屏幕左边距离为0),向下时布局距离越来越远(最终是屏幕的宽度的一半)。


根据上面的分析,主要的功能其实就是这个“在招职位”布局的收起和展开,默认收起,然后手势控制展开。说到这里其实我们就知道如何实现了,那就是behavior,它默认已经给我们处理了收起展开状态及拖动操作,我们只需要监听拖动和状态来设置布局的位置变化就可以了。开干~


为了省事底部布局我就直接截图放到布局了

###XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc">
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/icon_detail_zhilian" />
    </androidx.core.widget.NestedScrollView>
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="50dp"
        android:visibility="visible"
        app:behavior_hideable="true"
        app:behavior_peekHeight="110dp"
        app:layout_behavior="@string/bottom_sheet_behavior"
        tools:ignore="MissingPrefix">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/shape_header">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/layout_bottom_sheet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="center"
                android:orientation="vertical"
                android:text="在招职位-54个"
                android:textColor="@android:color/black"
                android:textSize="14sp"
                android:layout_margin="24dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/drag"
                android:layout_width="16dp"
                android:layout_height="16dp"
                android:layout_margin="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher_round" />
        </androidx.constraintlayout.widget.ConstraintLayout>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

###Activity

package com.study.fangdemo.zhilian

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup.MarginLayoutParams
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.*
import com.study.fangdemo.databinding.ActivityMainBinding
import com.study.fangdemo.utils.RecyclerDataUtils


class ZhiLianActivity : AppCompatActivity() {
    private var binding: ActivityMainBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding?.root)
        initView()
        initListener()

    }

    private fun initListener() {
        val behavior = from(binding!!.bottomSheet)
        behavior?.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                //左边图标显示
                binding?.drag?.isVisible = true
                when (newState) {
                    STATE_DRAGGING -> {}
                    STATE_SETTLING -> {}
                    STATE_EXPANDED -> {
                        //展开状态时隐藏左边图标
                        binding?.drag?.isVisible = false
                    }
                    STATE_COLLAPSED -> {}
                    STATE_HIDDEN -> {}
                    STATE_HALF_EXPANDED -> {}
                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                val width = windowManager.defaultDisplay.width
                val margin = ((0.5 - slideOffset / 2) * width).toInt()
                margin(bottomSheet, margin)
            }
        })

        //详情滑动到底部 再往上滑动时展开
        binding?.scrollview?.setOnTouchListener(object : View.OnTouchListener {
            var startX: Float = 0.0f
            var startY: Float = 0.0f
            var offsetX: Float = 0.0f
            var offsetY: Float = 0.0f
            var touchSlop = ViewConfiguration.get(baseContext).scaledTouchSlop
            override fun onTouch(view: View?, event: MotionEvent?): Boolean {
                when (event?.action) {
                    MotionEvent.ACTION_DOWN -> {
                        startX = event.x
                        startY = event.y

                    }
                    MotionEvent.ACTION_UP -> {
                        offsetX = event.x - startX
                        offsetY = event.y - startY
                        if (Math.abs(offsetX) > Math.abs(offsetY)) {
                            // left
                            if (offsetX < -touchSlop) {

                            }
                            // right
                            else if (offsetX > touchSlop) {
                            }
                        } else {
                            // up
                            if (offsetY < -touchSlop) {
                                if (isScrollViewEnd()) {
                                    behavior.state = STATE_EXPANDED
                                }
                            }
                            // down
                            else if (offsetY > touchSlop) {
                            }
                        }
                    }
                }
                return false
            }

        })

    }

    /**
     * NestedScrollView 是否滚动到底部
     */
    private fun isScrollViewEnd(): Boolean {
        binding?.apply {
            var scrollY = scrollview?.scrollY
            var onlyChild = scrollview?.getChildAt(0)
            if (onlyChild?.height!! <= (scrollY!! + scrollview?.height!!)) {
                return true
            }
        }
        return false
    }

    private fun initView() {
        binding?.apply {
            //设置测试列表数据
            RecyclerDataUtils.setRecyclerAdater(baseContext, recyclerview, "测试数据", 30)

            //设置屏幕宽
            bottomSheet?.post {
                val params = bottomSheet.layoutParams
                params.width = windowManager.defaultDisplay.width
                bottomSheet.layoutParams = params
            }

            //默认外左边距是屏幕宽度一半
            margin(bottomSheet, windowManager.defaultDisplay.width / 2)
        }

    }

    /**
     * 设置左边Margin
     */
    private fun margin(v: View, l: Int) {
        if (v.layoutParams is MarginLayoutParams) {
            val p = v.layoutParams as MarginLayoutParams
            p.leftMargin = l
            v.requestLayout()
        }
    }
}

代码其实还是比较少的,其实主要是通过监听拖动来设置“在招职位”布局距离左边的margin值就可以了,希望能对新入手的同学有点帮助,我也算复习了下。


具体的测试APK可以通过这个链接测试下:

http://d.firim.pro/7by9


github:https://github.com/yixiaolunhui/FangDemo


相关文章
|
XML Android开发 数据格式
Android仿淘宝、京东Banner滑动查看图文详情
本文基于 `ViewPager2` 实现的 `Banner` 效果,进而实现了仿淘宝、京东`Banner`滑动至最后一页时继续滑动来查看图文详情的效果。
201 0
|
XML 缓存 API
Android 天气APP(十四)修复UI显示异常、优化业务代码逻辑、增加详情天气显示
Android 天气APP(十四)修复UI显示异常、优化业务代码逻辑、增加详情天气显示
198 0
Android 天气APP(十四)修复UI显示异常、优化业务代码逻辑、增加详情天气显示
|
Android开发
Android跳转到应用商店的APP详情页面,以及 Google GMS 各个apk的包
Android跳转到应用商店的APP详情页面,以及 Google GMS 各个apk的包
|
Java API 开发工具
android开发错误详情汇总及解决方法(持续更新)
android开发错误详情汇总及解决方法(持续更新)
|
Android开发
【Android】仿淘宝商品详情页面的下拉渐变
最近需要做一个和最新版淘宝相似的商品详情页。先看原版: 淘宝的版本 我实现的效果: 我的版本 因为单纯实现渐变功能,所以背景只用了一张尺寸较大的imageview。
2598 0
|
Android开发
Android--仿淘宝商品详情(继续拖动查看详情)及标题栏渐变
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/52510844 本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发。
1264 0
|
Android开发
android TabLayout实现京东详情效果
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个support库里面,Google给我们提供了更加规范的MD设计风格的控件。最重要的是,Android Design Support Library的兼容性更广,直接可以向下兼容到Android 2.2。
1526 0
|
XML 测试技术 Android开发
Android社交类APP动态详情代码实现通用模板
 Android社交类APP动态详情代码实现通用模板 Android平台上一些比较流行的社交类APP比如微信、陌陌等,都有动态详情页,在该页面,用户发表的动态详情,好友可以发起评论、点赞等等。
819 0