仿微信地图的可变化高度的列表页

栏目: Android · 发布时间: 4年前

内容简介:通过拖拽事件,改变列表的高度

点击实现列表的伸缩,拖拽实现列表的伸缩

仿微信地图的可变化高度的列表页

一句话原理

通过拖拽事件,改变列表的高度

代码

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical">

    <include layout="@layout/common_title_lib" />

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:layout_weight="1"></com.amap.api.maps.MapView>

    <LinearLayout
        android:id="@+id/ll_line"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@android:color/white"
        android:orientation="vertical"

        app:layout_constraintBottom_toBottomOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:id="@+id/cl_top"
            android:paddingLeft="16dp"
            android:paddingTop="8dp"
            android:paddingRight="16dp">

            <ImageView

                android:layout_width="56dp"
                android:layout_height="8dp"
                android:src="@drawable/gray_tag"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />

            <ImageView
                android:id="@+id/imgVew_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:src="@drawable/red_point"
                app:layout_constraintBottom_toTopOf="@id/imgVew_2"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/tv_start_address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:textColor="@android:color/black"
                app:layout_constraintBottom_toBottomOf="@id/imgVew_1"

                app:layout_constraintLeft_toRightOf="@id/imgVew_1"
                app:layout_constraintTop_toTopOf="@id/imgVew_1"
                tools:text="山西鼎盛钢铁" />

            <ImageView
                android:id="@+id/imgVew_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:paddingBottom="8dp"
                android:src="@drawable/blue_point"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imgVew_1" />

            <TextView
                android:id="@+id/tv_end_address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"

                android:paddingBottom="8dp"
                android:textColor="@android:color/black"
                app:layout_constraintBottom_toBottomOf="@id/imgVew_2"
                app:layout_constraintLeft_toRightOf="@id/imgVew_2"
                app:layout_constraintTop_toTopOf="@id/imgVew_2"
                tools:text="山西鼎盛钢铁" />
        </android.support.constraint.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/edit_hint" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/lstVew_line"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

关键逻辑代码

llLine = findViewById(R.id.ll_line);
        clTop = findViewById(R.id.cl_top);
 llLineMinHight = (int) (ScreenUtil.getScreenHeight(GpsActivity.this) * 0.3 + 0.5);
        llLineMaxHight = (int) (ScreenUtil.getScreenHeight(GpsActivity.this) * 0.8 + 0.5);
        llLineCurrentHight = llLineMinHight;
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) llLine.getLayoutParams();
        lp.height = llLineMinHight;
        llLine.setLayoutParams(lp);


        clTop.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 获取当前列表的高度
                        currentHight = llLine.getLayoutParams().height;
                        lastY = (int) event.getRawY();
                        break;
                        case MotionEvent.ACTION_MOVE:
                        // 滑动的距离
                        int dy = (int) event.getRawY() - lastY;
                        //判断是点击事件还是拖拽事件
                        if(dy==0){
                            isClick=true;
                        }else{
                            isClick=false;
                        }
                        int newHight = currentHight - dy;
                        //判断拖拽的高度在最大值和最小值之间
                        if (newHight >= llLineMaxHight) {
                            llLineCurrentHight = llLineMaxHight;
                            newHight = llLineMaxHight;

                        } else if (newHight <= llLineMinHight) {
                            llLineCurrentHight = llLineMinHight;
                            newHight = llLineMinHight;
                        }
            
                        llLine.getLayoutParams().height = newHight;
                        //刷新UI
                        llLine.requestLayout();
                        break;

                    case MotionEvent.ACTION_UP:
                        // 手指抬起时,获取当前列表的高度
                        currentHight = llLine.getLayoutParams().height;
                        // 点击事件的 展开
                        if (currentHight == llLineMinHight && isClick) {
                            startPropertyAnimation(llLine, llLineMinHight, llLineMaxHight);
                            llLineCurrentHight = llLineMaxHight;
                        } 
                        //如果拖拽的高度接近最大,则缓缓展开
                        else if (currentHight > llLineMinHight && currentHight <= llLineMaxHight/1.5) {
                            startPropertyAnimation(llLine, currentHight, llLineMinHight);
                            llLineCurrentHight = llLineMinHight;
                        } 
                        //如果拖拽的高度接近最小,则缓缓收缩
                        else if (currentHight > llLineMaxHight / 1.5 && currentHight < llLineMaxHight) {
                            startPropertyAnimation(llLine, currentHight, llLineMaxHight);
                            llLineCurrentHight = llLineMaxHight;
                        } 
                        //收缩点击事件
                        else if (currentHight == llLineMaxHight && isClick) {
                            startPropertyAnimation(llLine, llLineMaxHight, llLineMinHight);
                            llLineCurrentHight = llLineMinHight;
                        }
                        clTop.performClick();
                        break;
                                        default:
                        break;
                }
                return true;
            }
        });

以上所述就是小编给大家介绍的《仿微信地图的可变化高度的列表页》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++语言的设计和演化

C++语言的设计和演化

[美] Bjarne Stroustrup / 裘宗燕 / 机械工业出版社 / 2002-1 / 48.00元

这本书是C++的设计者关于C++语言的最主要著作之一。作者综合性地论述了C++的历史和发展,C++中各种重要机制的本质意义和设计背景,这些机制的基本用途和使用方法,讨论了C++所适合的应用领域及其未来的发展前景。一起来看看 《C++语言的设计和演化》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具