我的布局看起来像这样:
<?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:id="@+id/main_read_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.project.books.flipview.FlipView
android:id="@+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_black"
app:orientation="horizontal"
tools:context=".ReadActivity" >
</com.project.books.flipview.FlipView>
<include
android:id="@+id/readingtoolbar"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
layout="@layout/readingbar_layout" />
</android.support.constraint.ConstraintLayout>
FlipView 是一个自定义的Framelayout,它具有像书本一样的翻页动画。用户还可以在每页传播上捏缩放。
下面的工具栏有一系列蜡笔。用户点击颜色后,会启动一个不可见的片段,用户可以在页面上展开。
这是工具栏布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/readbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/defaultdark_color"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="4dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/tabtools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@drawable/icontools"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/tabdictionary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_marginStart="16dp"
android:src="@drawable/icondictionary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/tabtools"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/tabassessment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:alpha="0.3"
android:scaleType="fitXY"
android:src="@drawable/iconassesment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/tabdictionary"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/tabexit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:scaleType="fitXY"
android:src="@drawable/iconclose"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/eraser"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="4dp"
android:src="@drawable/drawingeraser"
android:scaleType="fitXY"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guide_eraser"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="@+id/crayon_black"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingblack"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_blue"
app:layout_constraintStart_toEndOf="@+id/guide_eraser" />
<ImageView
android:id="@+id/crayon_blue"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingblue"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_green"
app:layout_constraintStart_toEndOf="@+id/crayon_black" />
<ImageView
android:id="@+id/crayon_green"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawinggreen"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_orange"
app:layout_constraintStart_toEndOf="@+id/crayon_blue" />
<ImageView
android:id="@+id/crayon_orange"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingorange"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_pink"
app:layout_constraintStart_toEndOf="@+id/crayon_green" />
<ImageView
android:id="@+id/crayon_pink"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingpink"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_red"
app:layout_constraintStart_toEndOf="@+id/crayon_orange" />
<ImageView
android:id="@+id/crayon_red"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingred"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_violet"
app:layout_constraintStart_toEndOf="@+id/crayon_pink" />
<ImageView
android:id="@+id/crayon_violet"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingviolet"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_white"
app:layout_constraintStart_toEndOf="@+id/crayon_red" />
<ImageView
android:id="@+id/crayon_white"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="@drawable/drawingwhite"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/crayon_yellow"
app:layout_constraintStart_toEndOf="@+id/crayon_violet" />
<ImageView
android:id="@+id/crayon_yellow"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
android:src="@drawable/drawingyellow"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/crayon_white"
app:layout_constraintEnd_toStartOf="@id/guide_center"/>
<ImageView
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/iconreset"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/guide_center"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:visibility="gone"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:src="@drawable/iconshare"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tabexit2"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/tabexit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:visibility="gone"
android:src="@drawable/iconclose"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guide_eraser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1"/>
<android.support.constraint.Guideline
android:id="@+id/guide_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
工具栏最初以不可见的方式开始。如果单击页面上的任何位置,它应从底部滑出。我可以使用dispatchTouchEvent
捕获click事件override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (ev!!.action == MotionEvent.ACTION_DOWN) {
startX = ev.x
startY = ev.y
}
if (ev.action == MotionEvent.ACTION_UP){
val endX = ev.x
val endY = ev.y
if (isAClick(startX, endX, startY, endY)) {
onSlideViewButtonClick(reading_toolbar!!)
return true
} else {
return super.dispatchTouchEvent(ev)
}
}
return super.dispatchTouchEvent(ev)
}
因此工具栏滑出,但如果我点击工具栏上的任何蜡笔,它会向下滑动,这是不应该做的。基本上,我想要的是,如果用户点击工具栏本身的其他任何地方,那么它应该向下滑动。
我尝试使用onTouch,因此我可以传递视图而不是dispatchTouchEvent,但即使使用相同的逻辑,我也无法捕获点击。不过,我可以捕捉拖拽事件。
class ReadActivity : Activity(), FlipViewAdapter.Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener, View.OnTouchListener {
...
}
设置
flip_view.setOnTouchListener(this)
测试点击此处
override fun onTouch(v: View?, ev: MotionEvent?): Boolean {
println("clicking")
// put actual clicking logic
return true
}