Open specific layout within a ViewFlipper [closed]

1

I'm doing a menu, and I want each button to open a different layout that is within ViewFlipper . That is, according to the button you select in the menu, the ViewFlipper starts with that selected layout.

When I'm inside the activity that handles ViewFlipper if I can change them using setDisplayedChild , but that I can not use it on the menu page. How can I do it?

<ViewFlipper android:id="@+id/viewFlipper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  >
    <include layout="@layout/pagina1"/>
    <include layout="@layout/pagina2"/>
    <include layout="@layout/pagina3"/>
    <include layout="@layout/pagina4"/>
    <include layout="@layout/pagina5"/>

</ViewFlipper>
    
asked by Hugo AE 16.12.2016 в 00:35
source

1 answer

0

So that each "page" can be changed, use the onTouchEvent() method with which you can detect a displacement on the X axis, and in this way show the layouts contained in include :

public class MainActivity extends Activity {
    private ViewFlipper viewFlipper;
    private float lastX;

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

    }


    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {

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

            if (lastX < currentX) {
                if (viewFlipper.getDisplayedChild() == 0)
                    break;
                //Adelante.
                viewFlipper.showNext();
             }

             if (lastX > currentX) {
                 if (viewFlipper.getDisplayedChild() == 1)
                     break;
                 //Atras.
                 viewFlipper.showPrevious();
             }
             break;
         }
         return false;
    }
}
    
answered by 16.12.2016 в 01:04