ViewFlipper : 화면 전환

2019. 3. 19. 14:14Andriod

# *.xml

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btnPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="이전화면"/>
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="다음화면 "/>
</LinearLayout>

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"></LinearLayout>
</ViewFlipper>
</LinearLayout>


# *.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.example.a0319_01;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
 
public class MainActivity extends AppCompatActivity {
    /*  사용될 변수 선언 */
    Button btnPrev,btnNext;
    ViewFlipper vFlipper;
 
    /* 뷰 플리퍼 ViewFlipper */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
      btnPrev = (Button)findViewById(R.id.btnPrev);
      btnNext = (Button)findViewById(R.id.btnNext);
      vFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
 
      btnPrev.setOnClickListener(new View.OnClickListener() { //이전 화면 버튼
          @Override
          public void onClick(View v) {
              vFlipper.showPrevious();
          }
      });
 
      btnNext.setOnClickListener(new View.OnClickListener() { // 다음 화면 버튼
          @Override
          public void onClick(View v) {
              vFlipper.showNext();
          }
      });
 
    }
}
 
cs