参考了Android Referencehttp://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html.
首先新建一个ViewPageActivity类继承FragmentActivity(android.support.v4.app.FragmentActivity而不是android.app.FragmentActivity)
对应的Layout文件viewpage_actxml:
每个单独的ListFragment对应的Layout文件:
首先新建一个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; @Override protected 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); } }); } @Override public 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); } @Override public Fragment getItem(int position) { return ArrayListFragment.newInstance(position); } @Override public 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; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "Fragment" + mNum + ":onCreate"); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } @Override public 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; } @Override public 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)); } @Override public 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下载功能的实现(参考Browser源码)
2014-05-23 16:55 01.实现DownloadHandler,为下载做准备。 ... -
Android中使用蓝牙
2013-10-15 16:43 6018原文地址:http://developer.and ... -
Sqlite3 基本指令(笔记)
2013-05-18 13:26 891.header ON|OFF设置是否显示数据表格的列名 .i ... -
Android 捕获App意外退出错误的日志信息
2013-05-08 17:57 35241.创建获取日志信息的类AppLogHandler,该类的 ... -
Android下Achartengine绘制折线图
2013-05-08 17:58 5551因开发需要,App中需要用到图表的绘制,参考了网上的文章和 ...
相关推荐
Tab+Viewpage+Fragment实现导航源代码,是一个很不错的Android源码,有兴趣的伙伴们抽时间可以看一下把
使用ViewPage实现滑动效果,使用Fragment使每个page页面的逻辑分开处理,代码可读性更高。每个Fragment有自己生命周期可更好把握内存部分的处理。是个demo代码十分简单,可做入门使用。注释清晰。 开发环境:android...
ViewPage+Fragment实现区域顶部tab滑动切换(烟台杰瑞教育Android培训部原创
使用ViewPager+Fragment+仿网易云音乐的tab选项栏来实现多屏滑动,实现布局的基本框架,并且有网易云音乐的tab选项风格的menu,希望大家可以用到
利用viewpager+fragment+actionbar,实现tab滑动
Android可滑动选项卡 一般项目中用的,以前用Tab 现在大多数用Fragment和ViewPage来实现可滑动
吸顶结合tabLayout+viewpage+fragment
ViewPageFragment ,很多项目都用到的功能 这里自己加上了滑动到当前加载, 动态删除和增加 tab功能
左右两个fragment上下滑动的时候,都会计算并动态调整stickyView的位置。在viewpager左右滑动的时候,两个fragment的stickyView高度之间的契合也做了调整。在豌豆荚和应用宝的app详情页界面,对stickyView的处理太过...
1.android TAb分页菜单 2.滑动切换页签 3.viewPager和tabhost组合滑动 4.ViewPager+Fragment替代TabHost效果的简单示例