476
No
class MainActivity
package org.androidtown.mypager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager =(ViewPager)findViewById(R.id.pager);
//어댑터 설정
MyAdapter adapter=new MyAdapter(this);
pager.setAdapter(adapter);
}
public void onClick(View v){
//두번째 화면으로 이동
pager.setCurrentItem(1);
}
class MyAdapter extends PagerAdapter {
String[] names={"소녀시대", "걸스데이", "씨스타"} ;
int[] mLayout={R.layout.first, R.layout.second, R.layout.third};
private LayoutInflater mInflater;
public MyAdapter(Context c){
super();
mInflater=LayoutInflater.from(c);
}
@Override
public int getCount() {
return names.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
/*
// 인덱스가 개수에 따라 화면 만들기
// layout 을 inflate 를 해서 화면 자체를 변환
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = null;
if(position==0){
v = mInflater.inflate(mLayout[0], null);
}
else if(position==1){
v = mInflater.inflate(mLayout[1], null);
}else{
v = mInflater.inflate(mLayout[2], null);
}
((ViewPager)pager).addView(v, 0);
return v;
}*/
// 인덱스가 개수에 따라 화면 만들기
// 일반적인 레이아웃 생성 텍스트만 전환
@Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout layout=new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
TextView view =new TextView(getApplicationContext());
view.setText(names[position]);
view.setTextSize(40.0f);
layout.addView(view);
container.addView(layout);
return layout;
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:text="특정페이지로 이동 2페잊"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button"
android:onClick="onClick"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
first.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"
android:id="@+id/first"
android:background="@color/colorAccent"
>
</LinearLayout>
second.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"
android:id="@+id/second"
android:background="@android:color/holo_orange_light"
>
</LinearLayout>
third.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"
android:id="@+id/third"
android:background="@android:color/holo_green_light"
>
</LinearLayout>
댓글 ( 4)
댓글 남기기