您的位置:首页 > 编程语言 > PHP开发

利用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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐