0

我正在尝试在片段上使用 setOnTouchListener() 创建翻转卡动画。为此,我正在关注 Joerock 的 Xamarin 教程。下面是我的代码。当我尝试运行时,前卡正在显示,但是当我双击片段时,它没有响应。请帮助我哪里出错了。 .

public class OpenClass extends Activity {

private boolean mShowingBack;
public GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //int gotBread;
    //Bundle gotBasket=getIntent().getExtras();
    //gotBread=gotBasket.getInt("key");

    setContentView(R.layout.flashframe);
    mGestureDetector=new GestureDetector(this, new MyGestureListener(this));

    if(savedInstanceState==null){
        getFragmentManager().beginTransaction()
                .add(R.id.container, new CardFrontFragment()).commit();
    }

    }
public void flipCard(){
    if(mShowingBack){
    getFragmentManager().popBackStack();
    mShowingBack=false;
    return;
    }
    else{
        FragmentTransaction transaction=getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                R.animator.card_flip_left_in, R.animator.card_flip_left_out);
        transaction.replace(R.id.container, new CardBackFragment());
        transaction.addToBackStack(null);
        transaction.commit();
        mShowingBack=true;
    }
}


public class CardFrontFragment extends Fragment implements  OnTouchListener{
    public CardFrontFragment(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View frontCard=inflater.inflate(R.layout.fragment1,container,false);
        frontCard.setOnTouchListener(this);
        return frontCard;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        //OpenClass parentActivity=(OpenClass) getActivity();
        return mGestureDetector.onTouchEvent(event);

    }


}
public class CardBackFragment extends Fragment implements OnTouchListener{
    public CardBackFragment(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View backCard=inflater.inflate(R.layout.fragment2,container,false);
        backCard.setOnTouchListener(this);
        return backCard;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        //OpenClass parentActivity= (OpenClass) getActivity();
        return mGestureDetector.onTouchEvent(event);

    }

}

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    private OpenClass mMainActivity;

    public MyGestureListener(OpenClass activity){
    mMainActivity = activity;   
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        mMainActivity.flipCard();
        return true;
    }



}
}
4

0 回答 0