前言
又到了新的一月,今天提供一个Android自定义底部上拉布局的实现,起因是自己在项目中需要实现这样一个控件,干脆自己写一个练练手。
写完了觉得能想到的需求都基本有了(可能会有其它需求,不过基本上改吧改吧就行了),又花了一点时间直接放到了Github上托管,希望能给您一些参考价值:
先看一下实现效果:
分析一下这种控件的基本需求有以下几种:
1.有一个部分是能够作为把手(就是图中的handle,)进行拖拽的,这部分高度是暴露在界面中的 -> 需要实现:Handle按钮
* 特殊需求特殊分析,比如让这个Handle透明实现无Handle的效果
2.底部上啦布局是有一定高度限制的,不一定覆盖设备的整个屏幕 -> 需要自定义最大高度
3.当从底部上拉一点点时抬手,布局缩回,若超过一定高度,自动弹到最高,隐藏同理 -> 需要自定义自动到达顶部/隐藏的阈值
直接使用
直接使用也很简单,笔者进行了简单的封装,以供参考:
1. 在Project的build.gradle文件中添加:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2.在Module的build.gradle文件中添加:
dependencies {
compile 'com.github.qingmei2:SlideBottomLayout-Android:1.2.3'
}
3.Add view in your layout:
需要注意的是:为了简单实现,笔者偷了个懒,设定为该布局下只能有一个直接的子View(类似ScrollView)
因此如果您添加需要一个布局,请在外面嵌套一个ViewGroup:
android:id="@+id/slideLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="200dp"
app:handler_height="50dp">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/handle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#d95858"
android:gravity="center"
android:text="handle"
android:textColor="@android:color/white"
android:textSize="16sp" />
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
实现步骤
具体代码如下,其中上述需求的设置方式都已经在代码中声明:
先看下属性声明:
public class SlideBottomLayout extends LinearLayout {
public void setShortSlideListener(ShortSlideListener listener) {
this.shortSlideListener = listener;
}
private ShortSlideListener shortSlideListener;
/**
* The {@link MotionEvent#ACTION_DOWN} gesture location.
*/
private int downY;
/**
* The