多個Fragment切換效果

這裏用到ViewPager來幫助我們實現Fragment之間的滑動切換,而不是像上一篇博客那樣子通過古板的點擊textview才能切換Fragment,用viewPager就像listview之類的也需要一個adapter。具體用法看下面就知道了。不過要非常注意的是,Fragment的包不能用錯,我用的是android.support.v4.app.Fragment,而不是android.app.Fragment。忽略這點會造成一些比較頭疼的錯誤。

下面是代碼:

package com.example.myfragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
		viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
	}

}

package com.example.myfragment;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyPagerAdapter extends FragmentPagerAdapter {
	private Fragment fragment1;
	private Fragment fragment2;
	private Fragment fragment3;
	private Fragment fragment4;
	private List<Fragment> fragments;
	private String[] pagerTitle;

	public MyPagerAdapter(FragmentManager fm) {
		super(fm);
		// TODO Auto-generated constructor stub
		Fragment f = new Fragment1();
		fragment1 = new Fragment1();
		fragment2 = new Fragment2();
		fragment3 = new Fragment3();
		fragment4 = new Fragment4();
		fragments = new ArrayList<Fragment>();
		fragments.add(fragment1);
		fragments.add(fragment2);
		fragments.add(fragment3);
		fragments.add(fragment4);
		pagerTitle = new String[]{"Fragment1", "Fragment2","Fragment3", "Fragment4"};

	}

	@Override
	public android.support.v4.app.Fragment getItem(int arg0) {
		// TODO Auto-generated method stub
		return fragments.get(arg0);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fragments.size();
	}
	
	@Override
	public CharSequence getPageTitle(int position) {
		// TODO Auto-generated method stub
		return pagerTitle[position];
	}

}

package com.example.myfragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class Fragment1 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment1, null);
		Button bt = (Button) view.findViewById(R.id.bt1);
		bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "f1", 0).show();
			}
		});
		return view;
	}
}

還有三個Fragment,但是和上面是一樣的就不貼了。

<LinearLayout 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:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>

</LinearLayout>
<?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" >

    <Button 
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="f1"
        />

</LinearLayout>

還有3個Fragment的xml,因爲是一樣所以不貼了。

通過點擊每個Fragment裏面的按鈕都能toast出相應的信息,所以正常。

如果要實現通過點擊標題(而不用滑動)來達到快速切換的話,可以把PagerTitleStrip換成PagerTabStrip,實際上也是建議這樣做得。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章