我正在使用具有自定義ViewPager的FragmentStatePagerAdapter。以前我使用的是標準的ViewPager,我可以正確地滑動到我的所有片段,並與EditTexts/Buttons和其他視圖上的其他組件進行交互,但是我必須在限制滑動方向時創建自定義ViewPager。 一旦我使用自定義的ViewPager,一切都顯示正確等等,但我無法點擊任何EditTexts /按鈕等Android自定義ViewPager不允許我點擊查看組件
任何人都可以看到這個問題?我假設SignUpPager不覆蓋某個方法或不正確地處理MotionEvent ....但我的嘗試都沒有奏效。
SignUpPager.java:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class SignUpPager extends ViewPager{
public SignUpPager(Context context){
super(context);
}
public SignUpPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
private float initialXValue;
public SwipeDirection direction = SwipeDirection.all;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.IsSwipeAllowed(event)) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.IsSwipeAllowed(event)) {
return super.onTouchEvent(event);
}
return false;
}
private boolean IsSwipeAllowed(MotionEvent event) {
if(this.direction == SwipeDirection.all) return true;
if(direction == SwipeDirection.none)//disable any swipe
return false;
if(event.getAction()==MotionEvent.ACTION_DOWN) {
initialXValue = event.getX();
return true;
}
if(event.getAction()==MotionEvent.ACTION_MOVE) {
try {
float diffX = event.getX() - initialXValue;
if (diffX > 0 && direction == SwipeDirection.right) {
// swipe from left to right detected
return false;
} else if (diffX < 0 && direction == SwipeDirection.left) {
// swipe from right to left detected
return false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
return true;
}
public enum SwipeDirection {
all, left, right, none ;
}
}
在MainActivity.java我的SignUpPager連接到FragmentStatePagerAdapter(注意我剛纔複製的代碼的臨界線這一部分):
public SignUpAdapter signUpAdapter;
public static SignUpPager pager;
signUpAdapter = new SignUpAdapter(getSupportFragmentManager());
pager = (SignUpPager) findViewById(R.id.signUpViewPager);
pager.setAdapter(signUpAdapter)
public static class SignUpAdapter extends FragmentStatePagerAdapter{
..........
}
sign_up .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<signUp.SignUpPager
android:id="@+id/signUpViewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
</LinearLayout>
fragment_view.xml(這是視圖的XML h我無法點擊組件):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/signUpLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
tools:context="com.example.tabbyreyez.helloworld.SignUpSecondaryController">
<TextView
android:id="@+id/signUpDataTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signUpData"
android:layout_marginTop="30dp"
android:layout_marginLeft="27dp"
android:textSize="35dp"/>
<TextView
android:id="@+id/firstNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name:"
android:layout_below="@+id/signUpDataTitle"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:textSize="17dp"/>
<EditText
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="e.g. Tabraiz"
android:layout_below="@+id/signUpDataTitle"
android:layout_marginTop="18dp"
android:layout_marginLeft="130dp"
android:ems="9"/>
<TextView
android:id="@+id/lastNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name:"
android:layout_below="@+id/firstNameLabel"
android:layout_marginTop="23dp"
android:layout_marginLeft="10dp"
android:textSize="17dp" />
<EditText
android:id="@+id/lastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="e.g. Malik"
android:layout_below="@+id/firstName"
android:layout_marginLeft="130dp"
android:ems="9" />
<Button
android:id="@+id/signUpButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Sign Up"
android:layout_below="@+id/passwd"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
您可以包含您的EditTexts/Buttons的xml,問題可能在於此。 – REG1
@ REG1我已更新xml文件 – reyyez
您是否能夠點擊其他片段佈局的視圖? – REG1