我的片段中有这个布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContenedorPrincipal">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="@android:drawable/btn_default"
android:id="@+id/floatingActionButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
<Button
android:text="@string/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@+id/floatingActionButton"
android:layout_toLeftOf="@+id/floatingActionButton"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_above="@+id/button">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutglobal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutobligatorios"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/obligatorios"
android:id="@+id/textViewirgeneral"
android:background="@color/naranja"
android:layout_marginTop="15dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/imageViewirgeneral"
android:background="@color/black" />
//Here are some textinputlayout
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutgeneral"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/general"
android:id="@+id/textViewiugeneral"
android:background="@color/naranja"
android:layout_marginTop="15dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/imageViewiugeneral"
android:background="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutsuelo"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/suelo"
android:id="@+id/textViewiusuelo"
android:background="@color/naranja"
android:layout_marginTop="15dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/imageViewiusuelo"
android:background="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutcon"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/construccion"
android:id="@+id/textViewiucons"
android:background="@color/naranja"
android:layout_marginTop="15dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/imageViewiucons"
android:background="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutcomp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/inmueblecompleto"
android:id="@+id/textViewiucomp"
android:background="@color/naranja"
android:layout_marginTop="15dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/imageViewiucomp"
android:background="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutamort"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/amortizaciones"
android:id="@+id/textViewiuamort"
android:background="@color/naranja"
android:layout_marginTop="15dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="@+id/imageViewiuamort"
android:background="@color/black" />
</LinearLayout>
</LinearLayout>
</ScrollView>
我需要对向右或向左滑动的用户作出反应,因此,在使用此布局的片段中,我实现了View.OnTouchListener,并覆盖了OnTouch方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
if(!touching){
Log.i("TAG", "onTouch llamado");
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
initialX = event.getX();
initialY = event.getY();
Log.d("TAG", "Action was DOWN");
break;
case MotionEvent.ACTION_UP:
float finalX = event.getX();
float finalY = event.getY();
Log.d("TAG", "Action was UP");
if (initialX < finalX) {
Log.d("TAG", "Left to Right swipe performed");
}
if (initialX > finalX) {
Log.d("TAG", "Right to Left swipe performed");
validate();
}
if (initialY < finalY) {
Log.d("TAG", "Up to Down swipe performed");
}
if (initialY > finalY) {
Log.d("TAG", "Down to Up swipe performed");
}
initialX=0F;
initialY=0F;
finalX=0F;
finalY=0F;
break;
case MotionEvent.ACTION_CANCEL:
Log.d("TAG","Action was CANCEL");
break;
case MotionEvent.ACTION_OUTSIDE:
Log.d("TAG", "Movement occurred outside bounds of current screen element");
break;
}
return true;
}else{
return false;
}
}
这样,就不会调用OnTouch方法。我已经尝试在我的xml的每个不同的LinearLayout中设置OnTouchListener,这样就调用了OnTouch方法,只要我没有触及另一个组件(如TextInputLayout)的空间。我希望在父布局中随处调用OnTouch,但仍然可以使用TextInputLayout。我尝试在TextInputLayouts上设置onTouchListener,但随后它们按预期停止工作,并且表现得好像没有启用。
我想避免使用ViewPager,因为如果我需要使用该组件,我必须更改许多功能。
如何让整个布局对OnTouch做出反应,同时保持TextInputLayout正常工作?
谢谢。
答案 0 :(得分:1)
您可以在片段中充气的视图上setOnTouchListener()
。这将确保每当用户触摸片段上的任何位置时调用onTouch()
。要启用TextInputLayout
,您可以扩展TextInputLayout
以制作自定义类,覆盖其中的onTouchEvent
,调用super并返回true,如下所示。
public class MyTextInputLayout extends TextInputLayout {
public MyTextInputLayout(Context context) {
super(context);
}
public MyTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
return true;
}
}
然后,您将在布局XML中使用MyTextInputLayout
而不是TextInputLayout
,或者在其中对其进行充气。当用户触摸您的MyTextInputLayout
时,这将阻止触摸事件传递到片段。
此外,创建一个自定义类,扩展片段XML根目录中的ViewGroup
,假设它是LinearLayout
。该课程看起来像:
public class MyViewGroup extends LinearLayout {
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
View myTextInputLayout = findViewById(R.id.my_text_input_layout); // Replace with your resource ID
if (myTextInputLayout != null) {
Rect rect = new Rect();
myTextInputLayout.getHitRect(rect);
if (rect.contains(rect)) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}
显然,您将使用上述类而不是XML中的LinearLayout
。