본문 바로가기
Android

ViewPager

by 일용직 코딩노동자 2019. 9. 10.
728x90
반응형

MainAcivity.java

import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.yujin.viewpager.Frgment.FirstFrgment;
import com.example.yujin.viewpager.Frgment.SecondFragment;
import com.example.yujin.viewpager.Frgment.TgridFragment;

public class MainActivity extends AppCompatActivity {
ViewPager vp;

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

vp = (ViewPager)findViewById(R.id.vp);
Button first = (Button)findViewById(R.id.one);
Button second = (Button)findViewById(R.id.two);
Button third = (Button)findViewById(R.id.three);

vp.setAdapter(new pagerAdapter(getSupportFragmentManager()));
vp.setCurrentItem(0);

first.setOnClickListener(movePageListener); //버튼 클릭시 동작하는 뷰페이져
first.setTag(0);
second.setOnClickListener(movePageListener);
second.setTag(1);
third.setOnClickListener(movePageListener);
third.setTag(2);
}
View.OnClickListener movePageListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int tag = (int) v.getTag(); //태그값을 가져옴 정수형으로
vp.setCurrentItem(tag);
}
};

private class pagerAdapter extends FragmentStatePagerAdapter
{
public pagerAdapter(android.support.v4.app.FragmentManager fm)
{
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) //슬라이드 동작으로 넘겼을때 작동되는 부분
{
switch(position)
{
case 0:
return new FirstFrgment();
case 1:
return new SecondFragment();
case 2:

return new TgridFragment();
default:
return null;
}
}
@Override
public int getCount() //총 뷰페이져로 이동되는 페이지(xml) 의 갯수
{
return 3;
}
}

}

Frgment라는 패키지를 따로 만들고 그안에 Frgment 페이지를 전환 할 3개의 java클래스 생성

 

FirstFrgment.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.yujin.viewpager.R;

public class FirstFrgment extends android.support.v4.app.Fragment
{
public FirstFrgment()
{
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_one, container, false);
return layout;
}
}

 

SecondFragment.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.yujin.viewpager.R;

public class SecondFragment extends android.support.v4.app.Fragment
{
public SecondFragment()
{
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_two, container, false);
return layout;
}
}

 

TgridFragment.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.yujin.viewpager.R;

public class TgridFragment extends android.support.v4.app.Fragment
{
public TgridFragment()
{
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_three, container, false);
return layout;
}
}

 

avtivity_main.xml

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

<LinearLayout
android:id="@+id/tap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="First" />

<Button
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Second" />

<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Third" />
</LinearLayout>

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tap">

</android.support.v4.view.ViewPager>

</RelativeLayout>

fragment_one.xml

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#B40404"
android:textStyle="bold"
android:text="첫번째 페이지"/>

</RelativeLayout>



fragment_two.xml

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#848484"
android:textStyle="bold"
android:text="두번째 페이지"/>

</RelativeLayout>

 

frgment_three.xml

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30dp"
android:textColor="#000000"
android:textStyle="bold"
android:text="세번째 페이지"/>

</RelativeLayout>

 

 

--결과--

 

 

궁금하신 사항은 댓글 남겨주세요.

728x90
반응형

'Android' 카테고리의 다른 글

SharedPreferences (간단한 정보저장)  (0) 2019.09.10
비콘감지  (11) 2019.09.10
EditText/TextView/ImageView/Button/Radio/RadioGroup/URI/Intent  (0) 2019.09.10
Image ViewPager  (11) 2019.09.10
ListView  (0) 2019.09.10

댓글