照之前紀錄的方法實作,發現我無法去讀取ViewPager中layout的控制項,所以再以之前實作的方法為基礎,但改成切換Fragment的方式,以便可以操控layout中的控制項。
另外一個頁面:layout2為一樣的實作方法
另外一個頁面:Fragment2為一樣的實作方法
執行結果:
Step1 在activity_main.xml中加入ViewPager標籤
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v4.view.ViewPager | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/pager"> | |
</android.support.v4.view.ViewPager> |
Step2 新增要切換的layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:gravity="center"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="01"/> | |
</LinearLayout> |
Step3 新增layout相對應的Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fragment1 extends Fragment { | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.layout1, container, | |
false); | |
return view; | |
} | |
} |
Step4 在MainActivity.java中新增ViewPagerAdapter的class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ViewPagerAdapter extends FragmentPagerAdapter { | |
public ViewPagerAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
switch (position) { | |
case 0: | |
return new Fragment1(); | |
case 1: | |
return new Fragment2(); | |
default: | |
return null; | |
} | |
} | |
//要切換的頁面數量 | |
@Override | |
public int getCount() { | |
return 2; | |
} | |
} |
Step5 在MainActivity.java中設定PagerAdapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private ViewPager mViewPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mViewPager = (ViewPager) findViewById(R.id.pager); | |
mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); | |
} |
留言
張貼留言