利用ViewPager+FragmentPagerAdapter+Fragment做一个能左右滑动的页面
2014-02-27 17:25
423 查看
先看主布局文件 main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
下边是Fragment布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/hello_world"/>
</LinearLayout>
下边看Fragment实现类的实现文件
package com.wind.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class PagerContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//首先接收新建Fragment时候传过来的参数
Bundle args = getArguments();
int layoutId = args.getInt("layoutId");
int imageId = args.getInt("image");
View view = inflater.inflate(layoutId, container, false);
ImageView image = (ImageView)view.findViewById(R.id.image);
image.setImageResource(imageId);
return view;
}
}
下边是应用的主要实现代码
package com.wind.fragment;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
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.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager)this.findViewById(R.id.pager);
initial();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initial() {
int[] scroll_img = new int[]{R.drawable.a1, R.drawable.a2, R.drawable.a3};
List<Fragment> contents = new ArrayList<Fragment>();
for (int i = 0; i < scroll_img.length; i++) {
Fragment content = new PagerContentFragment();
//新建Fragment的实例对象,并设置参数传递到Fragment中
Bundle args = new Bundle();
args.putInt("layoutId", R.layout.fragment_content);
args.putInt("image", scroll_img[i]);
content.setArguments(args);
contents.add(content);
}
//这个getSupportFragmentManager只有activity继承FragmentActivity才会有
MyFragmentPageAdapter adapter = new MyFragmentPageAdapter(getSupportFragmentManager(),contents);
mViewPager.setAdapter(adapter);
}
private class MyFragmentPageAdapter extends FragmentPagerAdapter {
private List<Fragment> mContents;
public MyFragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public MyFragmentPageAdapter(FragmentManager fm, List<Fragment> contents) {
super(fm);
mContents = contents;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return mContents.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mContents.size();
}
}
}
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
下边是Fragment布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/hello_world"/>
</LinearLayout>
下边看Fragment实现类的实现文件
package com.wind.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class PagerContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//首先接收新建Fragment时候传过来的参数
Bundle args = getArguments();
int layoutId = args.getInt("layoutId");
int imageId = args.getInt("image");
View view = inflater.inflate(layoutId, container, false);
ImageView image = (ImageView)view.findViewById(R.id.image);
image.setImageResource(imageId);
return view;
}
}
下边是应用的主要实现代码
package com.wind.fragment;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
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.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager)this.findViewById(R.id.pager);
initial();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initial() {
int[] scroll_img = new int[]{R.drawable.a1, R.drawable.a2, R.drawable.a3};
List<Fragment> contents = new ArrayList<Fragment>();
for (int i = 0; i < scroll_img.length; i++) {
Fragment content = new PagerContentFragment();
//新建Fragment的实例对象,并设置参数传递到Fragment中
Bundle args = new Bundle();
args.putInt("layoutId", R.layout.fragment_content);
args.putInt("image", scroll_img[i]);
content.setArguments(args);
contents.add(content);
}
//这个getSupportFragmentManager只有activity继承FragmentActivity才会有
MyFragmentPageAdapter adapter = new MyFragmentPageAdapter(getSupportFragmentManager(),contents);
mViewPager.setAdapter(adapter);
}
private class MyFragmentPageAdapter extends FragmentPagerAdapter {
private List<Fragment> mContents;
public MyFragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public MyFragmentPageAdapter(FragmentManager fm, List<Fragment> contents) {
super(fm);
mContents = contents;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return mContents.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mContents.size();
}
}
}
相关文章推荐
- ViewPager的一个应用,用fragment来当页面,可以左右滑动,上面有一条线,来分隔
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- 【FastDev4Android框架开发】HorizontalScrollView,Fragment,FragmentStatePagerAdapter打造网易新闻Tab及滑动页面效果(三十六)
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果(转载)
- 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- viewpager+fragment左右滑动切换页面
- 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- TabLayout 和ViewPager和Fragment的多页面滑动(主要实现一个水平的布局用来展示Tabs加上ViewPager实现联动效果)
- 利用viewpager、Fragment、pagertabStrip 实现多页面滑动效果
- android 学习之Fragment+ViewPager实现页面左右滑动标签页
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- 页面滑动2:使用FragmentPagerAdapter(ViewPagerDemo2)
- Viewpager+Fragment实现左右滑动切换页面
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效
- 如何利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- AppBarLayout、ViewPager、Fragment实现页面左右滑动
- HorizontalScrollView,Fragment,FragmentStatePagerAdapter打造网易新闻Tab及滑动页面效果(三十六)
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果(牛逼)
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效