参考了Android Reference http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html.
首先新建一个ViewPageActivity类继承FragmentActivity(android.support.v4.app.FragmentActivity而不是android.app.FragmentActivity)
import android.content.Context;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.app.ListFragment;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import com.ryan.test.R;public class ViewPageActivity extends FragmentActivity {private static String[] mStrings = { "tab1", "tab2", "tab3", "tab4" };static final int NUM_ITEMS = 4;private static final String TAG = ViewPageActivity.class.getName();private SectionsPagerAdapter pagerAdapter;private ViewPager viewPager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.viewpage_act);pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());viewPager = (ViewPager) findViewById(R.id.view_pager);viewPager.setAdapter(pagerAdapter);Button button1 = (Button) findViewById(R.id.goto_one);button1.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {viewPager.setCurrentItem(0);}});Button button2 = (Button) findViewById(R.id.goto_two);button2.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {viewPager.setCurrentItem(1);}});Button button3 = (Button) findViewById(R.id.goto_three);button3.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {viewPager.setCurrentItem(2);}});Button button4 = (Button) findViewById(R.id.goto_four);button4.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {viewPager.setCurrentItem(3);}});}@Overridepublic View onCreateView(String name, Context context, AttributeSet attrs) {return super.onCreateView(name, context, attrs);}public static class SectionsPagerAdapter extends FragmentPagerAdapter {public SectionsPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {return ArrayListFragment.newInstance(position);}@Overridepublic int getCount() {return NUM_ITEMS;}}public static class ArrayListFragment extends ListFragment {int mNum;static ArrayListFragment newInstance(int num) {ArrayListFragment f = new ArrayListFragment();Bundle args = new Bundle();args.putInt("num", num);f.setArguments(args);return f;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i(TAG, "Fragment" + mNum + ":onCreate");mNum = getArguments() != null ? getArguments().getInt("num") : 1;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {Log.i(TAG, "Fragment"+mNum+":onCreateView");View v = inflater.inflate(R.layout.fragment_pager_list, container, false);View tv = v.findViewById(R.id.fpl_text);((TextView) tv).setText("Fragment #" + mNum);return v;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);Log.i(TAG, "Fragment"+mNum+":onActivityCreated");setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, mStrings));}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {Log.i(TAG, "Item clicked:" + id);}}}

对应的Layout文件viewpage_actxml:
<?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:gravity="center_horizontal"    android:orientation="vertical"    android:padding="4dip" >    <android.support.v4.view.ViewPager        android:id="@+id/view_pager"        android:layout_width="match_parent"        android:layout_height="0px"        android:layout_weight="1" >    </android.support.v4.view.ViewPager>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="0"        android:gravity="center"        android:measureWithLargestChild="true"        android:orientation="horizontal" >        <Button            android:id="@+id/goto_one"            android:layout_width="0px"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="First" />                <Button            android:id="@+id/goto_two"            android:layout_width="0px"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="Second" />        <Button            android:id="@+id/goto_three"            android:layout_width="0px"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="Third" />                <Button            android:id="@+id/goto_four"            android:layout_width="0px"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="Forth" />    </LinearLayout></LinearLayout>

每个单独的ListFragment对应的Layout文件:
<?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:background="@android:drawable/gallery_thumb"    android:orientation="vertical" >    <TextView        android:id="@+id/fpl_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center_vertical|center_horizontal"        android:text="@string/hello_world"        android:textAppearance="?android:attr/textAppearanceMedium" />    <FrameLayout        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1" >        <ListView            android:id="@android:id/list"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:drawSelectorOnTop="false" />        <!-- Here is the view to show if the list is emtpy -->        <TextView            android:id="@android:id/empty"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="No items."            android:textAppearance="?android:attr/textAppearanceMedium" />    </FrameLayout></LinearLayout>


Android Fragment和ViewPage 实现简易可滑动Tab

Android Fragment和ViewPage 实现简易可滑动Tab

更多相关文章

  1. android socket 配置文件
  2. Android学习笔记之mainfest文件中android属性
  3. android 下载文件注意事项
  4. mainfest文件中android属性
  5. ~/.dirlst 文件
  6. Android 读取和保存文件(手机内置存储器)
  7. Android——文件存储
  8. Android layout文件中 '?' 的作用
  9. :如何安装apk文件在Android仿真器中

随机推荐

  1. Android中的SystemClock类
  2. ADT Plugin for Eclipse(Android开发工具
  3. Android 5.0 Material 自定义Dialog 背景
  4. eclipse 导入报错 Invalid project descr
  5. Fiddler 跟踪 Android 数据包
  6. Android语音播报、后台播报、语音识别
  7. ArrayAdapter ,SimpleAdapter ,SimpleCur
  8. [置顶] ArcGIS发布最新的 ArcGIS Runtime
  9. Android本地环境搭建步骤
  10. android 开源播放器与编码器