利用ViewPager+FragmentPagerAdapter+Fragment做一个能左右滑动的页面

先看主布局文件 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();
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值