在layout.xml中只放一个ViewPager
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/id_view_pager"/>
</LinearLayout>
尽量不要复制,手打好一点,快一点
onCreate中总共做了三件事,初始化View,初始化适配器和初始化ViewPager
Activity extends FragmentActivity {
private ViewPager mViewPager;
private List<Fragment> mListFragment;
private FragmentPagerAdapter mFragmentPagerAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
initView();
initAdapter();
initViewPager();
}
}
其中初始化View,初始化适配器函数如下,适配器特别简单,只需要两个返回函数。Ctrl键+F关注mListFragment的操作
Activity extends FragmentActivity {
private void initView(){
mViewPager =findViewById(R.id.id_view_pager);
}
private void initAdapter(){
mListFragment=new ArrayList<>();
mListFragment.add(new FirstFragment());
mListFragment.add(new SecondFragment());
//getSupportFragmentManager方法来自FragmentActivity
mFragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return mListFragment.get(position);
}
@Override
public int getCount() {
return mListFragment.size();
}
};
}
}
其中的FirstFragment.java如下
可以把onCreateView当成活动的onCreate,inflate当成setContentView,只不过inflate是加载,setContentView是显示。
public class FirstFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_layout,container,false);
}
}
first_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/dreamcatcher"/>
</LinearLayout>
很简单,就一张图
第二个SecondFragment.java如下
public class SecondFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.second_layout,container,false);
}
}
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/axes"/>
</LinearLayout>
两个Fragment是为了做List< Fragment >,而做List< Fragment >是为了给FragmentPagerAdapter配逻辑,能通过mListFragment找到相关的Fragment显示出来。
最后一个函数初始化viewpager,设置了适配器以后就可以选择显示哪一项了
Activity extends FragmentActivity {
private void initViewPager(){
mViewPager.setAdapter(mFragmentPagerAdapter);
mViewPager.setCurrentItem(0);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mViewPager.setCurrentItem(position);
//一般在此选择改变按钮的颜色
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
一般我们为了做出滑动时按键同时变色的效果,可以用一个函数设置所有的按钮变为未选中状态,然后设定相关的按钮为选中状态,(起setButtonImageToSelectAndSelectItem这个长名是为了好辨认)
Activity extends FragmentActivity {
//三个Tab对应的Button
private Button mTabLeftButton;
private Button mTabCenterButton;
private Button mTabRightButton;
private void setButtonImageToSelectAndSelectItem(int i){
//设置全部为默认
mTabCenterButton.setBackgroundResource(R.drawable.center_normal_img);
mTabCenterButton.setTextColor(Color.parseColor("#000000"));
mTabLeftButton.setBackgroundResource(R.drawable.left_normal_img);
mTabLeftButton.setTextColor(Color.parseColor("#000000"));
mTabRightButton.setBackgroundResource(R.drawable.right_normal_img);
mTabRightButton.setTextColor(Color.parseColor("#000000"));
//根据传入参数选则选中按钮
switch (i){
case 0:
mTabLeftButton.setBackgroundResource(R.drawable.left_selected_img);
mTabLeftButton.setTextColor(Color.parseColor("#ffffff"));
break;
case 1:
mTabCenterButton.setBackgroundResource(R.drawable.center_selected_img);
mTabCenterButton.setTextColor(Color.parseColor("#ffffff"));
break;
case 2:
mTabRightButton.setBackgroundResource(R.drawable.right_selected_img);
mTabRightButton.setTextColor(Color.parseColor("#ffffff"));
}
mViewPager.setCurrentItem(i);
}
}
然后在点击事件和页面选中事件中设置它
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.id_left_button:
setButtonImageToSelectAndSelectItem(0);
break;
case R.id.id_center_button:
setButtonImageToSelectAndSelectItem(1);
break;
case R.id.id_right_button:
setButtonImageToSelectAndSelectItem(2);
break;
}
}
private void initEvents(){
mTabCenterButton.setOnClickListener(this);
mTabLeftButton.setOnClickListener(this);
mTabRightButton.setOnClickListener(this);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setButtonImageToSelectAndSelectItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}