又是一个风和日丽的一天,又是as人的一天~
如题。
我当前正在编写一个单机的点餐app。在拉跨的原型图上有一个轮播图用于播放菜单图片,由于项目说明书没有说可以使用第三方依赖,所以我用vp实现了一个拉跨的banner控件。界面大概是这个样子:
这个banner(vp)不包括那左右两个箭头控件(imageView)。
布局代码大概如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
tools:context=".ui.OrderActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false">
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/holo_red_light"
android:minHeight="60dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="@+id/expand_im"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="top"
android:layout_marginTop="-20dp"
android:layout_marginStart="30dp"
android:src="@drawable/ic_baseline_shopping_cart_24" />
<ListView
android:id="@+id/select_lv"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_marginTop="10dp"
android:visibility="gone"/>
<TextView
android:id="@+id/total_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="总价: 0"
android:layout_marginBottom="10dp"
android:textColor="@color/white"
android:textSize="18sp"
android:layout_marginEnd="15dp"/>
<TextView
android:id="@+id/count_tv"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="@color/green"
android:text="结账"
android:layout_gravity="end"
android:textSize="22sp"
android:textColor="@color/white"
android:gravity="center"
android:visibility="gone"/>
</LinearLayout>
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/bottom"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<--两个左右箭头->
<ImageView
android:id="@+id/left"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:background="@drawable/shape_gray_45"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:src="@drawable/ic_baseline_keyboard_arrow_left_24" />
<ImageView
android:id="@+id/right"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:background="@drawable/shape_gray_45"
android:src="@drawable/ic_baseline_keyboard_arrow_right_24" />
<--自己写的拉跨banner-->
<edu.wschina.bAbbas.view.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp" />
</RelativeLayout>
<ListView
android:id="@+id/main_lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
当我Activity当中监听这两个箭头的时候却发现居然不触发onClickListener。
@Override
protected void initEvent() {
//左边箭头监听
left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast("click");
banner.toLeft();
}
});
right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
banner.toRight();
}
});
banner.start();
}
这真是离了个大谱。
我立刻就面向百度编程。结果发现大部分都是焦点问题。
当我百思不得其姐的时候。我看着xml文件突然一个激灵。
我把布局顺序调整了一下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<edu.wschina.bAbbas.view.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ImageView
android:id="@+id/left"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:background="@drawable/shape_gray_45"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:src="@drawable/ic_baseline_keyboard_arrow_left_24" />
<ImageView
android:id="@+id/right"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:background="@drawable/shape_gray_45"
android:src="@drawable/ic_baseline_keyboard_arrow_right_24" />
</RelativeLayout>
运行。
呦西。
安卓的绘制体系是覆盖性的,当一个控件覆盖在另一个控件之上时,被覆盖的控件可能因为覆盖它的控件背景是透明的而看得见,但却无法点击。因为它被覆盖了。