首页 > 代码库 > ViewPager+Fragment简单讲解

ViewPager+Fragment简单讲解

对于加载多个视图的容器,一般来说都是需要用Adapter来让系统自动加载,因为方便。不然自己写代码一个一个的加载那多麻烦啊,对吧?


同样,对于ViewPager,它也是加载多个视图的,因此也需要有对应的Adapter来进行视图的加载。

关于Adapter,是连通数据和视图容器的桥粱。Adapter根据数据生成一个视图,Adapter对视图容器的作用是加载对应的视图进去容器中。(我个人习惯喜欢把装载多个视图的控件成为容器)


MainActivity代码:

package com.example.viewpagerdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

	private ViewPager viewPager;
	

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		viewPager = (ViewPager)findViewById(R.id.viewPager);
		FragmentManager manager = getSupportFragmentManager();
		//框架的pagerAdapter主要是两种Adapter,第一是FragmentStatePagerAdapter,第二是FragmentPagerAdapter
		//两种adapter的区别是第一种对不需要的Fragment摧毁掉,第二种只摧毁视图,实例依然保存在FragmentManager中,
		//爱使用哪种根据实际情况决定
		
		viewPager.setAdapter(new FragmentStatePagerAdapter(manager) {
			
			//在两种adapter中都必须要覆盖getCount和getItem方法。
			//getCount()里返回的是框架的总个数
			@Override
			public int getCount() {
				return 3;
			}
			
			//在getItem()中返回的是对应位置的框架
			@Override
			public Fragment getItem(int position) {
				//根据position生成3个略有区别的框架,看起来效果明显点
				return new FragmentDemo(position);
			}
		});
	}
}
</pre><p></p><p><span style="font-size:24px; color:#ff0000"><span style="background-color:rgb(255,255,255)">构建框架的代码FragmentDemo:</span></span></p><p></p><pre name="code" class="java">package com.example.viewpagerdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentDemo extends Fragment{
	private String text;
	int[] color_res = {Color.RED,Color.GREEN,Color.BLUE};
	private int colorint;
	
	public FragmentDemo(int fragmentNumber){
		this.text = "框架"+fragmentNumber;
		//颜色的id值
		colorint = color_res[fragmentNumber];
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_demo,container,false);
		TextView textView = (TextView)view.findViewById(R.id.textView);
		textView.setText("框架"+text);
		textView.setBackgroundColor(colorint);
		
		
		return view;
	}
}


MainActivity对应的xml:activity_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="com.example.viewpagerdemo.MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        
        />
    

</RelativeLayout>

框架的xml:fragment_demo.xml

<?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" >
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        />

    

</LinearLayout>

演示的结果图:



ViewPager+Fragment简单讲解