참고URL : http://itpangpang.tistory.com/284
[ ViewPager 1단계 ]
* 개발 방향 및 Logic
1. ActionBar를 사용하지 않는다.
2. ActionBar대신에 TextView를 통해서 ActionBar역할을 하도록 한다.
3. TextView (탭버튼)를 클릭 시 ViewPager에 페이지가 변경되도록 한다.
[activity_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/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_first" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="첫번째 탭" />
<Button android:id="@+id/btn_second" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="두번째 탭" />
<Button android:id="@+id/btn_third" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:text="세번째 탭" /> </LinearLayout>
<android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/ll"> </android.support.v4.view.ViewPager>
</RelativeLayout> |
[fragment_first.xml], [fragment_second.xml], [fragment_third.xml] 3가지 거의 유사
<?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="#FFFFFF" android:textStyle="bold" android:text="첫번째 페이지"/>
</RelativeLayout> |
[FirstFragment.java], [SecondFragment.java], [ThirdFragment.java]
public class FirstFragment extends Fragment { public FirstFragment() { }
@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_first, container, false); return layout; } } |
[MainActivity.java]
public class MainActivity extends AppCompatActivity { ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
vp = (ViewPager)findViewById(R.id.vp); Button btn_first = (Button)findViewById(R.id.btn_first); Button btn_second = (Button)findViewById(R.id.btn_second); Button btn_third = (Button)findViewById(R.id.btn_third);
//ViewPage의 Page를 관리해주는 Adapter가 필요. //ViewPage와 Adapter연결 vp.setAdapter(new pagerAdapter(getSupportFragmentManager()));
//ViewPage.setCurrentItem()는 해당 ViewPage에 Fragment 를 보여준다. //맨 처음에 setCurrentItem()을 안하면 default로 첫번째(0) Fragment를 보여준다. //vp.setCurrentItem(0);
btn_first.setOnClickListener(movePageListener); btn_first.setTag(0); btn_second.setOnClickListener(movePageListener); btn_second.setTag(1); btn_third.setOnClickListener(movePageListener); btn_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 FirstFragment(); case 1: return new SecondFragment(); case 2: return new ThirdFragment(); default: return null; } } @Override public int getCount() { return 3; } } } |