Android--ViewPager-Fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.cnn.viewpager02;
 
import java.util.ArrayList;
import java.util.List;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
 
//因为V4 继承 FragmentActivity
public class MainActivity extends FragmentActivity {
    private ViewPager viewPager;
    private List<String> titleList;
    private List<Fragment> fragmentList;
    private PagerTitleStrip tabStrip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        viewPager = (ViewPager) findViewById(R.id.viewPage1);
        tabStrip = (PagerTitleStrip) findViewById(R.id.tab);
        //tabStrip.set
         
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new Fragment01());
        fragmentList.add(new Fragment02());
        fragmentList.add(new Fragment03());
         
        titleList = new ArrayList<String>();
        titleList.add("第一页");
        titleList.add("第二页");
        titleList.add("第一页");
        //FragmentManager fm = getSupportFragmentManager();
        MyFragmentPagerAdapter adapter
        = new MyFragmentPagerAdapter( getSupportFragmentManager(), titleList, fragmentList);
        viewPager.setAdapter(adapter);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.cnn.viewpager02;
 
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
 
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    private List<String> titleList;
    private List<Fragment> fragmentList;
     
    public MyFragmentPagerAdapter(FragmentManager fm,List<String> titleList,List<Fragment> fragmentList) {
        super(fm);
        // TODO 自动生成的构造函数存根
        this.titleList = titleList;
        this.fragmentList = fragmentList;
    }
 
    @Override
    public CharSequence getPageTitle(int position) {
        // TODO 自动生成的方法存根
        return titleList.get(position);
    }
     
    @Override
    public Fragment getItem(int arg0) {
        // TODO 自动生成的方法存根
        return fragmentList.get(arg0);
    }
 
    @Override
    public int getCount() {
        // TODO 自动生成的方法存根
        return fragmentList.size();
    }
     
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.cnn.viewpager02;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class Fragment01 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        //return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.item1, container, false);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.cnn.viewpager02;
 
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class Fragment02 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        //return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.item2, container, false);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.cnn.viewpager02;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class Fragment03 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        //return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.item3, container, false);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPage1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/tab"
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:layout_gravity="top"
            >
             
        </android.support.v4.view.PagerTitleStrip>
    </android.support.v4.view.ViewPager>
</LinearLayout>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一页" />
 
</LinearLayout>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二页" />
 
</LinearLayout>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三页" />
 
</LinearLayout>

  

posted @   在路上的白羊  阅读(267)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示