0

私はカスタムViewPagerでFragmentStatePagerAdapterを使用しています。以前は標準のViewPagerを使用していましたが、すべてのフラグメントを正しくスワイプして、さまざまなビューのEditTexts/Buttonsや他のコンポーネントとやり取りできましたが、スワイプの方向を制限するときにカスタムViewPagerを作成する必要がありました。 カスタムViewPagerを使用すると、すべてが正しく表示されますが、EditTexts/Buttonsなどのいずれかをクリックすることができません。AndroidカスタムViewPagerでViewコンポーネントをクリックできません

この問題は誰にでも見られますか?私はSignUpPagerが特定のメソッドをオーバーライドしていないか、MotionEventを間違って処理していると仮定しています....しかし、私の試みはどれもうまくいきませんでした。

SignUpPager.java:MainActivity.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 ; 
    } 
} 

私は(私はこの部分のコードの重要な行をコピーして注意してください)FragmentStatePagerAdapterにSignUpPagerを接続します

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(これはwhicのビュー用のXMLですH I)は、コンポーネントをクリックすることができません:

<?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/Buttonのxmlを含めることができますが、そこに問題がある可能性があります。 – REG1

+0

@ REG1 xmlファイルで更新しました – reyyez

+0

他のフラグメントレイアウトのビューをクリックすることはできますか? – REG1

答えて

0

がFragmentActivityを拡張する必要があり、あなたのMainActivityに次のように試してみてください。

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); 
} 

FragmentStatePagerAdapterを拡張するカスタムSignUpAdapterを作成する理由はありません。代わりに、単にPagerAdapterを使用してみてください。

+0

私はSignPpagerがViewPagerをFragmentStatePagerAdapterではなく拡張しています。そして私は、ユーザーが実際に水平面でフラグメント間をスワイプする必要があるので、私はFragmentStatePagerAdapterを使用しています。 PagerAdapterは同じ結果を得ますか? (p、s。私は自分のテストをできるだけ早く行います) – reyyez

+0

@reyyez申し訳ありません、私はSignUpAdapterを意味していました。はい、FragmentStatePagerAdapterを継承するSignUpAdapterの代わりにPagerAdapterを使用するコードを使用してください。動作するはずです。 – REG1

+0

うまくいけば、答えとして受け入れてください:) – REG1

関連する問題