FragmentPagerAdapter+ViewPager实现Tab切换效果

时间:2021-12-15 17:19:13

1.Activity  加载布局文件,获取Viewpager控件   给ViewPager填充适配器.

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ActionMode; import java.util.ArrayList; public class MainActivity extends Activity {
private static final String INSTANCESTATE_TAB = "tab";
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
ActionMode mActionMode; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager);
mViewPager = (ViewPager) findViewById(R.id.pager); final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME); mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_sd),
Fragment1.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_remote),
Fragment2.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt(INSTANCESTATE_TAB, 0));
}
} @Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(INSTANCESTATE_TAB, getActionBar().getSelectedNavigationIndex());
} public void setActionMode(ActionMode actionMode) {
mActionMode = actionMode;
} public ActionMode getActionMode() {
return mActionMode;
} public static class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
private Fragment fragment; TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
} public TabsAdapter(Activity activity, ViewPager pager) {
super(activity.getFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
} public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
} @Override
public int getCount() {
return mTabs.size();
} @Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
if (info.fragment == null) {
info.fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
return info.fragment;
} @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
} @Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
} @Override
public void onPageScrollStateChanged(int state) {
} @Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
if(!tab.getText().equals(mContext.getString(R.string.tab_sd))) {
ActionMode actionMode = ((MainActivity) mContext).getActionMode();
if (actionMode != null) {
actionMode.finish();
}
}
} @Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
} @Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}

2.fragment_pager.xml布局文件   里面就一个ViewPager控件   大家记得导入v4包

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

3.Fragment1.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// return View.inflate(getActivity(), R.layout.fragment1, null);
return inflater.inflate(R.layout.fragment1,container, false);
}
}

4.fragment1.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"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第一个页面" /> </RelativeLayout>

效果图如下:

FragmentPagerAdapter+ViewPager实现Tab切换效果