ViewPager+Fragment+FragmentPagerAdapter实现软件主界面

ViewPager之前的页面是由View构成的,现在由Fragment构成,之前的PagerAdapter这里也换成了FragmentPagerAdapter.因为PagerAdapter有

public Object instantiateItem(ViewGroup container, int position) {
 container.addView(viewList.get(position));
 return viewList.get(position);
}

方法是针对视图View的,这里已经不适用。

效果图上:

ViewPager+Fragment+FragmentPagerAdapter实现软件主界面

ViewPager+Fragment+FragmentPagerAdapter实现软件主界面

ViewPager+Fragment+FragmentPagerAdapter实现软件主界面

ViewPager+Fragment+FragmentPagerAdapter实现软件主界面

代码部分:

主界面的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:focusable="false"
 >
 <android.support.v4.view.ViewPager
 android:id="@+id/viewPager"
 android:layout_height="fill_parent"
 android:layout_width="fill_parent">
 </android.support.v4.view.ViewPager>
 <RadioGroup
 android:id="@+id/titleGroup"
 android:layout_width="match_parent"
 android:layout_height="80dp"
 android:orientation="horizontal"
 android:layout_alignParentBottom="true"
 android:clickable="true"
 >
 <RadioButton
 android:id="@+id/radioHome"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:gravity="center"
 android:text="第一个页面"
 android:checked="true"
 android:clickable="true"
 android:button="@null"
 android:drawableTop="@drawable/radio1"
 android:textColor="@drawable/text1"
 android:drawablePadding="5dp"
 android:padding="5dp"
 />
 <RadioButton
 android:id="@+id/radioSearch"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:layout_gravity="center"
 android:text="第二个页面"
 android:checked="false"
 android:button="@null"
 android:drawableTop="@drawable/radio2"
 android:textColor="@drawable/text2"
 android:drawablePadding="5dp"
 android:padding="5dp"/>
 <RadioButton
 android:id="@+id/radioMore"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:gravity="center"
 android:text="第三个页面"
 android:checked="false"
 android:button="@null"
 android:drawableTop="@drawable/radio3"
 android:textColor="@drawable/text3"
 android:drawablePadding="5dp"
 android:padding="5dp"/>
 <RadioButton
 android:id="@+id/radioPersonal"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:layout_gravity="center"
 android:text="第四个页面"
 android:checked="false"
 android:button="@null"
 android:drawableTop="@drawable/radio4"
 android:textColor="@drawable/text4"
 android:drawablePadding="5dp"
 android:padding="5dp"/>
 </RadioGroup>
</RelativeLayout>

对应的MainActivity:

package com.tab.tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
 private ViewPager viewPager;
 private List<Fragment> viewList;
 private FragmentPagerAdapter fpagerAdapter;
 private RadioGroup radioGroup;
 private RadioButton radioHome,radioSearch,radioMore,radioPersonal;
 private int[] all={0,1,2,3};
 private List<RadioButton> radioButtons;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 viewPager= (ViewPager) findViewById(R.id.viewPager);
 Fragment fragment1=new HomeActivity();
 Fragment fragment2=new SearchActivity();
 Fragment fragment3=new MoreActivity();
 Fragment fragment4=new PersonalActivity();
 viewList=new ArrayList<Fragment>();
 viewList.add(fragment1);
 viewList.add(fragment2);
 viewList.add(fragment3);
 viewList.add(fragment4);
 fpagerAdapter=new mPagerAdapter(getSupportFragmentManager());
 viewPager.setAdapter(fpagerAdapter);
 radioHome= (RadioButton) findViewById(R.id.radioHome);
 radioSearch= (RadioButton) findViewById(R.id.radioSearch);
 radioMore= (RadioButton) findViewById(R.id.radioMore);
 radioPersonal= (RadioButton) findViewById(R.id.radioPersonal);
 radioButtons=new ArrayList<RadioButton>();
 radioButtons.add(radioHome);
 radioButtons.add(radioSearch);
 radioButtons.add(radioMore);
 radioButtons.add(radioPersonal);
 viewPager.setOnPageChangeListener(new pageChangeListener());
 radioGroup= (RadioGroup) findViewById(R.id.titleGroup);
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
 int i=0;
 for(RadioButton button:radioButtons){
 if(button.getId()==checkedId){
 viewPager.setCurrentItem(i);
 break;
 }
 i++;
 }
 }
 });
 }
 class mPagerAdapter extends FragmentPagerAdapter{
 public mPagerAdapter(FragmentManager fm) {
 super(fm);
 }
 @Override
 public Fragment getItem(int i) {
 return viewList.get(i);
 }
 @Override
 public int getCount() {
 return viewList.size();
 }
 }
 class pageChangeListener implements ViewPager.OnPageChangeListener{
 @Override
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 }
 @Override
 public void onPageSelected(int position) {
 for(int i:all){
 if(i==position){
 radioButtons.get(i).setChecked(true);
 }else{
 radioButtons.get(i).setChecked(false);
 }
 }
 }
 @Override
 public void onPageScrollStateChanged(int state) {
 }
 }
}

Fragment部分:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeActivity extends Fragment {
 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view=inflater.inflate(R.layout.layout1,null);
 return view;
 }
}

相关推荐