wasup

Android) ViewFlipper (xml + java) 본문

IT/Android

Android) ViewFlipper (xml + java)

wasupup 2021. 8. 7. 10:03
반응형

java.lang.Object
ㄴandroid.view.View
ㄴㄴandroid.widget.ViewGroup
ㄴㄴㄴandroid.widget.FrameLayout
ㄴㄴㄴㄴandroid.widget.ViewAnimator
ㄴㄴㄴㄴㄴandroid.widget.ViewFlipper


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">

    <LinearLayout...>
        <Button.../>
        <Button.../>
    </LinearLayout>

    <ViewFlipper...>
        <LinearLayout...>
            <ImageView.../>
        </LinearLayout>

        <LinearLayout...>
            <ImageView.../>
        </LinearLayout>

        <LinearLayout...>
            <ImageView.../>
        </LinearLayout>
    </ViewFlipper>
 </LinearLayout>

 


flipping method

startFlipping() : 시작

stopFlipping() : 정지

setFlipInterVal(millisceond) : 간격


.showPrevious(); //이전 뷰 보여주기
.showNext(); //이전 뷰 보여주기

setDisplayedChild(int whichChild) //원하는 인덱스의 뷰 보여주기

        btnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.showPrevious(); //이전 뷰 보여주기
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.showNext(); //이전 뷰 보여주기
            }
        });

activity_main.xml

<?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">

    <LinearLayout
        android:layout_width="wrap_content"
        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/viewFlipper01"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#cccccc">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/img01"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#2C817474">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/img02"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/img03"
                />
        </LinearLayout>
    </ViewFlipper>
 </LinearLayout>

MainActivity.java

package com.example.book08;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

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

        //변수
        Button btnPrev, btnNext;
        final ViewFlipper vFlipper;

        btnPrev = (Button) findViewById(R.id.btnPrev);
        btnNext = (Button) findViewById(R.id.btnNext);
        vFlipper = (ViewFlipper) findViewById(R.id.viewFlipper01);

        btnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.showPrevious(); //이전 뷰 보여주기
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vFlipper.showNext(); //이전 뷰 보여주기
            }
        });
    }//onCreate-end

}//class-end

 

반응형

'IT > Android' 카테고리의 다른 글

Android) ActionBar.TabListener  (0) 2021.08.07
Android) TabHost  (0) 2021.08.07
Android) ProgressBar, AbsSeekBar, RatingBar, SeekBar  (0) 2021.08.06
Android) 자동완성 텍스트뷰 예제  (0) 2021.08.06
Android) 시계와 캘린더 예제  (0) 2021.08.05
Comments