0

我正在使用具有自定義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> 
+0

您可以包含您的EditTexts/Buttons的xml,問題可能在於此。 – REG1

+0

@ REG1我已更新xml文件 – reyyez

+0

您是否能夠點擊其他片段佈局的視圖? – REG1

回答

0

請在您的MainActivity中嘗試以下內容,它應該擴展FragmentActivity。

private PagerAdapter mPagerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.setContentView(R.layout.sign_up); 
    //Initialize the pager 
    this.initialisePaging(); 
} 

/** 
* Initialize the fragments to be paged 
*/ 
private void initialisePaging() { 

    List<Fragment> fragments = new Vector<Fragment>(); 
    fragments.add(Fragment.instantiate(this, MyFragment1.class.getName())); 
    fragments.add(Fragment.instantiate(this, MyFragment2.class.getName())); 
    fragments.add(Fragment.instantiate(this, MyFragment3.class.getName())); 
    this.myPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); 
    ViewPager pager = (ViewPager) super.findViewById(R.id.signUpViewPager); 
    pager.setAdapter(this.mPagerAdapter); 
} 

我看不出有什麼理由你應該創建自定義SignUpAdapter擴展FragmentStatePagerAdapter。相反,只需嘗試使用PagerAdapter即可。

+0

我SignUpPager擴展了ViewPager而不是FragmentStatePagerAdapter。而且我使用FragmentStatePagerAdapter,因爲我需要用戶在水平面上的片段之間進行真正的滑動。 PagerAdapter會實現相同的結果嗎? (p,s。我會盡快進行我自己的測試) – reyyez

+0

@reyyez對不起,我的意思是SignUpAdapter。是的,使用我的代碼使用PagerAdapter而不是擴展FragmentStatePagerAdapter的SignUpAdapter,它應該可以工作。 – REG1

+0

如果它適合您,請接受它作爲答案:) – REG1