viewpager+fragmentactivity怎么点击滑动
1个回答
展开全部
实现
1. 首先在Activity中放入ViewPager控件
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
2. 创建三个Fragment
(1) 创建布局文件
(2) 创建类继承Fragment
public class PageOneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container,
false);
return rootView;
}
}
3. 为ViewPager创建Adapter
(1) 创建类继承FragmentPagerAdapter
(2) 重写getItem(int index) 以及 getCount() 方法
注意:自定义的构造函数一定要实现父类的构造函数 super(fm);
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<Fragment>();
public MyPagerAdapter(FragmentManager fm){
super(fm);
}
public MyPagerAdapter(FragmentManager fragmentManager,
ArrayList<Fragment> fragments){
super(fragmentManager);this.fragments = fragments;
}
@Override
public Fragment getItem(int index) {
return fragments.get(index);
}
@Override
public int getCount() {
return fragments.size();
}
}
4. 在Activity中将之前创建的3个Fragment加入到ViewPager中
ViewPager pager = null;
ArrayList<Fragment> fragments = null;
PagerAdapter adapter = null;
private void initFragment(){
fragments.add(new PageOneFragment());
fragments.add(new PageTwoFragment());
fragments.add(new PageThreeFragment());
}
//实现之前写的adapter并赋给ViewPager即可
private void initPager(){
adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
pager.setAdapter(adapter);
}
5. 至此,就已经实现了左右滑动切换页面的效果。
1. 首先在Activity中放入ViewPager控件
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
2. 创建三个Fragment
(1) 创建布局文件
(2) 创建类继承Fragment
public class PageOneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container,
false);
return rootView;
}
}
3. 为ViewPager创建Adapter
(1) 创建类继承FragmentPagerAdapter
(2) 重写getItem(int index) 以及 getCount() 方法
注意:自定义的构造函数一定要实现父类的构造函数 super(fm);
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<Fragment>();
public MyPagerAdapter(FragmentManager fm){
super(fm);
}
public MyPagerAdapter(FragmentManager fragmentManager,
ArrayList<Fragment> fragments){
super(fragmentManager);this.fragments = fragments;
}
@Override
public Fragment getItem(int index) {
return fragments.get(index);
}
@Override
public int getCount() {
return fragments.size();
}
}
4. 在Activity中将之前创建的3个Fragment加入到ViewPager中
ViewPager pager = null;
ArrayList<Fragment> fragments = null;
PagerAdapter adapter = null;
private void initFragment(){
fragments.add(new PageOneFragment());
fragments.add(new PageTwoFragment());
fragments.add(new PageThreeFragment());
}
//实现之前写的adapter并赋给ViewPager即可
private void initPager(){
adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
pager.setAdapter(adapter);
}
5. 至此,就已经实现了左右滑动切换页面的效果。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询