[关闭]
@cxm-2016 2016-08-30T08:15:53.000000Z 字数 6355 阅读 1533

Android中创建下拉View的简单示例

android no


先看效果

这里没有使用v7包下的内容,仅仅是为了学习,并没有太大的实用价值.现在我们来分析一下思路.

我们采用简单的观察者模式来思考这个问题,当我的手指在屏幕上滑动的时候会产生一个事件源,而下拉View的一个功能就是监听事件源.下面我们来定义接口

定义接口

1,定义事件源接口,该接口的功能就是注册一个观察者,注销观察者和处理事件源

  1. /**
  2. * 事件源接口
  3. * 陈小默 16/8/30.
  4. */
  5. interface IEventSource<Subscriber> {
  6. //向事件源中注册观察者
  7. fun register(subscriber: Subscriber)
  8. //给事件源发送事件
  9. fun event(event: MotionEvent): Boolean
  10. //取消注册
  11. fun unregister()
  12. }

2,定义观察者View,该接口定义了观察者的基本功能,开始观察和结束观察

  1. /**
  2. * View的消息订阅者接口
  3. */
  4. interface IViewSubscriber<Queue> {
  5. //开始观察数据源
  6. fun start(queue: Queue)
  7. //停止观察数据源
  8. fun stop()
  9. }

实现事件源

这里事件源的作用就是监听手指在屏幕上的滑动事件,并将相应的事件加入队列

  1. /**
  2. * 下拉容器事件源实现类
  3. * 陈小默 16/8/30.
  4. */
  5. class DropEventSource : IEventSource<DropLayout> {
  6. private var subscriber: DropLayout? = null
  7. private val queue: LinkedList<Int> = LinkedList()
  8. override fun register(subscriber: DropLayout) {
  9. this.subscriber = subscriber
  10. }
  11. override fun event(event: MotionEvent): Boolean {
  12. when (event.action) {
  13. MotionEvent.ACTION_DOWN -> start()
  14. MotionEvent.ACTION_MOVE -> move(event)
  15. MotionEvent.ACTION_UP -> stop()
  16. else->return false
  17. }
  18. return true
  19. }
  20. fun start() {
  21. subscriber!!.start(queue)
  22. }
  23. fun move(event: MotionEvent) {
  24. val height = event.y.toInt()
  25. queue.addLast(height)
  26. }
  27. fun stop() {
  28. subscriber!!.stop()
  29. }
  30. override fun unregister() {
  31. subscriber = null
  32. }
  33. }

因为面向接口的特性使得我们不需要知道对到具体是如何实现的只需要知道接口中定义的方法意义即可.下面拆看分析代码

  1. override fun event(event: MotionEvent): Boolean {
  2. when (event.action) {
  3. MotionEvent.ACTION_DOWN -> start()
  4. MotionEvent.ACTION_MOVE -> move(event)
  5. MotionEvent.ACTION_UP -> stop()
  6. else->return false
  7. }
  8. return true
  9. }

这个方法接受外部传来的事件,并按照事件的类型分发给不同的方法处理,下面我们来实现一下具体的方法:
1,start()当接收到手指按下的操作时,通知观察者开始观察事件源

  1. fun start() {
  2. subscriber!!.start(queue)
  3. }

2,move()当手指处于滑动状态时,将滑动坐标存放进事件源

  1. fun move(event: MotionEvent) {
  2. val height = event.y.toInt()
  3. queue.addLast(height)
  4. }

3,stop()当手指离开屏幕的时候通知观察者停止观察

  1. fun stop() {
  2. subscriber!!.stop()
  3. }

实现观察者

为了使用方便,这里采用了RelativeLayout作为容器基类并实现了观察者接口

  1. /**
  2. * 下拉容器
  3. * 陈小默 16/8/30.
  4. */
  5. class DropLayout : RelativeLayout, IViewSubscriber<LinkedList<Int>> {
  6. constructor(context: Context?) : super(context)
  7. constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
  8. constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
  9. var isStop: Boolean = true
  10. var drop_top = 0
  11. var drop_bottom = resources.displayMetrics.heightPixels
  12. var fixed = false
  13. override fun start(queue: LinkedList<Int>) {
  14. isStop = false
  15. Thread({
  16. while (!isStop)
  17. if (queue.size > 0) {
  18. val site = queue.removeFirst()
  19. if (site != null) {
  20. post { height = site }
  21. }
  22. }
  23. }).start()
  24. }
  25. override fun stop() {
  26. isStop = true
  27. finish()
  28. }
  29. fun finish() {
  30. val h = height
  31. val screen_height = resources.displayMetrics.heightPixels
  32. if (!fixed)
  33. if (h > screen_height / 2) {
  34. moveToBottom()
  35. } else {
  36. moveToTop()
  37. }
  38. }
  39. fun setHeight(height: Int) {
  40. val param = layoutParams
  41. param.height = height
  42. layoutParams = param
  43. }
  44. private fun moveToTop() {
  45. ObjectAnimator.ofInt(this, "height", drop_top).setDuration(500).start()
  46. }
  47. private fun moveToBottom() {
  48. val animator = ObjectAnimator.ofInt(this, "height", drop_bottom).setDuration(1000)
  49. animator.interpolator = BounceInterpolator()
  50. animator.start()
  51. }
  52. }

这个类实现的方法较多,我们一步一步的去分析:
1,setHeight()方法
这个方法非常重要,因为我们在这里用到了属性动画去处理最后手指离开屏幕的操作.当手指在屏幕的上半部分离开,就让View收缩到原始位置.如果手指在屏幕的下半部分离开,就让View完全落下,并使用弹跳效果.但是属性动画要求View必须提供相应的属性的setter/getter方法,而RelativeLayout只提供了"height"属性的get方法,所以我们必须自己实现一个setHeight方法

  1. fun setHeight(height: Int) {
  2. val param = layoutParams
  3. param.height = height
  4. layoutParams = param
  5. }

2,start()启动观察方法,当该方法被调用的时候表示事件源中即将有事件产生,此处所做的操作就是开启线程,对事件源进行轮询操作.每当取出一个事件,就更新一个当前View

  1. override fun start(queue: LinkedList<Int>) {
  2. isStop = false
  3. Thread({
  4. while (!isStop)
  5. if (queue.size > 0) {
  6. val site = queue.removeFirst()
  7. if (site != null) {
  8. post { height = site }
  9. }
  10. }
  11. }).start()
  12. }

3,finish()当一次观察完成时,View一般会处于屏幕中间的某一个位置,加入我们设置了fixed=true属性,则手指离开屏幕的时候,View会被固定到结束为止.反之,将会计算当前停止位置在屏幕的上方还是下方

  1. fun finish() {
  2. val h = height
  3. val screen_height = resources.displayMetrics.heightPixels
  4. if (!fixed)
  5. if (h > screen_height / 2) {
  6. moveToBottom()
  7. } else {
  8. moveToTop()
  9. }
  10. }

4,移动到顶部或者是底部,在finish方法计算完成之后,会调用下列方法中对应的一种,于是这里就使用了属性动画, 使得View能够平滑的移动到相应的位置.

  1. private fun moveToTop() {
  2. ObjectAnimator.ofInt(this, "height", drop_top).setDuration(500).start()
  3. }
  4. private fun moveToBottom() {
  5. val animator = ObjectAnimator.ofInt(this, "height", drop_bottom).setDuration(1000)
  6. animator.interpolator = BounceInterpolator()
  7. animator.start()
  8. }

使用

设置布局文件

include_main_drop.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <RelativeLayout
  7. android:id="@+id/re_layout"
  8. android:layout_width="match_parent"
  9. android:layout_height="50dp">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_centerInParent="true"
  14. android:text="个人中心" />
  15. <TextView
  16. android:id="@+id/settings"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentRight="true"
  20. android:layout_centerVertical="true"
  21. android:layout_marginRight="30dp"
  22. android:text="设置" />
  23. </RelativeLayout>
  24. <TextView
  25. android:id="@+id/score_tv"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_below="@+id/re_layout"
  29. android:layout_centerHorizontal="true"
  30. android:layout_marginTop="100dp"
  31. android:text="当前积分"
  32. android:textColor="@android:color/white"
  33. android:textSize="20sp" />
  34. </RelativeLayout>

main_activity.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:fitsSystemWindows="true"
  7. tools:context="com.cccxm.english.mvp.view.activity.MainActivity">
  8. <com.cxm.view.DropLayout
  9. android:id="@+id/drop_group"
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:background="#c5c500"
  13. android:elevation="10dp">
  14. <include layout="@layout/include_main_drop" />
  15. </com.cxm.view.DropLayout>
  16. <TextView
  17. android:id="@+id/word_lib"
  18. style="@style/text_button"
  19. android:layout_marginTop="50dp"
  20. android:text="单词学习" />
  21. <TextView
  22. android:id="@+id/tongue_lib"
  23. style="@style/text_button"
  24. android:layout_marginTop="200dp"
  25. android:text="口语学习" />
  26. <TextView
  27. android:id="@+id/dialog_lib"
  28. style="@style/text_button"
  29. android:layout_marginTop="350dp"
  30. android:text="情景对话" />
  31. </RelativeLayout>

MainActivity实现

我们实现事件源,并将查找到的dropLayout对象注册为观察者,我们设置DropLayout的最小高度为50dp与布局文件一致.然后我们可以将所有在DropLayout中产生的事件全部交给事件源对象统一处理

  1. override fun register() {
  2. dropSource.register(dropLayout)
  3. dropLayout.drop_top = UnitUtils().dip2px(50F, this)
  4. dropLayout.setOnTouchListener { view, motionEvent ->
  5. dropSource.event(motionEvent)
  6. }
  7. }

一般情况我们不会将整个DropLayout上产生的事件作为事件源,比如我们在DropLayout上增加了一个按钮,当点击按钮时才能拖动DropLayout是一种比较好的选择

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注