Android按钮onClick启动ViewFlipper的孩子

时间:2015-09-03 15:01:28

标签: android android-activity onclick viewflipper

我是Android的新手,我想知道如何在特定孩子的另一个Activity中启动ViewFlipper,我希望这个按钮在特定孩子的ViewFlipper中启动另一个Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);

    final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
    button1.setBackgroundResource(R.drawable.selector);
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Activity1.this,
                    Activity2.class);
            startActivity(intent);
        }
    });
}

活动2:

private ViewFlipper viewFlipper;
private float lastX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);
    viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper);
}

// Using the following method, we will handle all screen swaps.
public boolean onTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {

    case MotionEvent.ACTION_DOWN:
        lastX = touchevent.getX();
        break;
    case MotionEvent.ACTION_UP:
        float currentX = touchevent.getX();

        // Handling right to left screen swap.
        if (lastX < currentX) {

            // If there aren't any other children, just break.
            if (viewFlipper.getDisplayedChild() == 0)
                break;

            // Next screen comes in from left.
            viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
            // Current screen goes out from right.
            viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);

            // Display next screen.
            viewFlipper.showNext();
        }

        // Handling left to right screen swap.
        if (lastX > currentX) {

            // If there is a child (to the right), just break.
            if (viewFlipper.getDisplayedChild() == 1)
                break;

            // Next screen comes in from right.
            viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
            // Current screen goes out from left.
            viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);

            // Display previous screen.
            viewFlipper.showPrevious();
        }
        break;
    }
    return false;
}

那么如何告诉第一个活动在特定孩子中开始第二个活动,比如button1 start child 2和button2 start child 10

1 个答案:

答案 0 :(得分:0)

我正确理解这段代码很有用

您的另一项活动

在onCreate()方法

setContentView(R.layout.activity_another);
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);

另一个活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ViewFlipper
    android:id="@+id/viewflipper"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#00ff00">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="first" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ff0000" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="second" />

    </LinearLayout>

</ViewFlipper>

</LinearLayout>

开始另一项活动:

Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);