android.support.v4.view.ViewPager
android.support.v4.app.Fragment
android.support.v4.app.FragmentActivity
android.support.v4.app.FragmentPagerAdapter
主界面xml
android:layout_height="match_parent"
android:id="@+id/viewPage"
>
主界面java extends FragmentActivity
public class MainActivity extends FragmentActivity {
//容器
private android.support.v4.view.ViewPager viewPager;
//定义集合存fragment
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = findViewById(R.id.viewPage);
//添加2个界面,
fragmentList.add(new HomeFragment());
fragmentList.add(new TypeFragment());
//把容器添加到适配器中
viewPager.setAdapter(fragmentPagerAdapter);
}
//适配器
private FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
//哪一个界面
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
//有多少个界面
@Override
public int getCount() {
return fragmentList.size();
}
};
}
第一个界面主页:xml
android:layout_height="match_parent" tools:context="com.csdn.huadongchuangkou.HomeFragment">
android:text="第一个界面主页" />
第一个界面主页:java
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
第二个界面:xml
android:layout_height="match_parent" tools:context="com.csdn.huadongchuangkou.TypeFragment">
第二个界面:java
public class TypeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_type, container, false);
}
}