码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager+Fragment实现左右滑动页

时间:2015-11-16 06:13:36      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:android   viewpager   


     

             思路:

                       每一个Tab里面的内容都放到一个fragment里面,然后再每个fragment里面处理具体的业务逻辑,在主页面上,像加入view那样加入fragment.


             首先来看fragment里面代码:


 

/**
 * Created by LiuHuiChao on 2015/11/16.
 */
public class ViewPagerFragment1 extends Fragment {
    private String   text;
    private TextView tv = null;


    public ViewPagerFragment1(){}
    @SuppressLint("ValidFragment")
    public ViewPagerFragment1(String text){
        super();
        this.text = text;
    }

    /**
     * 覆盖此函数,先通过inflater inflate函数得到view最后返回
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab1, container, false);
        tv = (TextView)v.findViewById(R.id.tv);
        tv.setText(text);
        return v;
    }
}


      fragment对应的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">

    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="tab1" />
</LinearLayout>


      之后是主页:




/**
 * Created by LiuHuiChao on 2015/11/16.
 */
public class ViewPagerFragment extends FragmentActivity {

    /** 页面list **/
    List<Fragment> fragmentList = new ArrayList<Fragment>();
    /** 页面title list **/
    List<String> titleList  = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main3);
        ViewPager pager=(ViewPager)findViewById(R.id.viewPagerThree);
        fragmentList.add(new ViewPagerFragment1("页面1"));
        fragmentList.add(new ViewPagerFragment1("页面2"));
        fragmentList.add(new ViewPagerFragment1("页面3"));
        titleList.add("title 1 ");
        titleList.add("title 2 ");
        titleList.add("title 3 ");
        pager.setAdapter(new TestFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, titleList));
    }

    class TestFragmentPagerAdapter extends FragmentPagerAdapter{

        private List<Fragment> fragmentList;
        private  List<String> titleList;

        public TestFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragmentList,List<String> titleList) {
            super(fm);
            this.fragmentList=fragmentList;
            this.titleList=titleList;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            //return (titleList.size() > position) ? titleList.get(position) : "";
            return titleList.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return (fragmentList == null || fragmentList.size() == 0) ? null : fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }
    }
}


           主页对应layout:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPagerThree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
        <android.support.v4.view.PagerTabStrip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>
</LinearLayout>


     run了一下,发现以前自己想改掉的bug还在,明天再改吧,我的鱿鱼丝吃完了,而且我 也困了。感觉用fragment做的方式更好,感谢百度!明天收拾那两个小bug。晚安,亲们!








版权声明:本文为博主原创文章,未经博主允许不得转载。

ViewPager+Fragment实现左右滑动页

标签:android   viewpager   

原文地址:http://blog.csdn.net/lhc1105/article/details/49859053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!