标签:
要注意包都要保持一致 ,在本文中是support.v4,不然会出现各种错误
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.zzw.fragmentteb.MainActivity" > 6 7 <android.support.v4.view.ViewPager 8 android:id="@+id/view_pager" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" /> 11 12 </RelativeLayout>
MyFragment:
1 package com.zzw.fragmentteb; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class MyFragment extends Fragment { 11 private static final String TAG = "TestFragment"; 12 13 String name; 14 int color; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 Bundle b = this.getArguments(); 20 name = b.getString("NAME"); 21 color = b.getInt("COLOR"); 22 } 23 24 @Override 25 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 26 View view = inflater.inflate(android.R.layout.simple_list_item_1, null); 27 view.setBackgroundColor(color); 28 return view; 29 } 30 31 @Override 32 public void onViewCreated(View view, Bundle savedInstanceState) { 33 34 TextView text1 = (TextView) view.findViewById(android.R.id.text1); 35 36 text1.setText(name); 37 } 38 }
MainActivity:
1 package com.zzw.fragmentteb; 2 3 import java.util.ArrayList; 4 5 import android.graphics.Color; 6 import android.os.Bundle; 7 import android.support.v4.app.Fragment; 8 import android.support.v4.app.FragmentActivity; 9 import android.support.v4.app.FragmentManager; 10 import android.support.v4.app.FragmentPagerAdapter; 11 import android.support.v4.view.ViewPager; 12 13 public class MainActivity extends FragmentActivity { 14 15 ArrayList<Fragment> list; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 list=new ArrayList<Fragment>(); 23 list.add(setFragmentData("我是第一个Fragment", Color.RED)); 24 list.add(setFragmentData("我是第二个Fragment", Color.GRAY)); 25 list.add(setFragmentData("我是第三个Fragment", Color.GREEN)); 26 list.add(setFragmentData("我是第四个Fragment", Color.BLUE)); 27 list.add(setFragmentData("我是第五个Fragment", Color.YELLOW)); 28 29 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 30 31 viewPager.setAdapter(new MyAdapter(this.getSupportFragmentManager())); 32 } 33 34 // 设置要传递给Fragment的参数 35 private Fragment setFragmentData(String name, int color) { 36 Fragment f = new MyFragment(); 37 38 Bundle b = new Bundle(); 39 b.putString("NAME", name); 40 b.putInt("COLOR", color); 41 f.setArguments(b); 42 return f; 43 } 44 45 private class MyAdapter extends FragmentPagerAdapter { 46 47 public MyAdapter(FragmentManager fm) { 48 super(fm); 49 } 50 51 @Override 52 public Fragment getItem(int position) { 53 return list.get(position); 54 } 55 56 @Override 57 public int getCount() { 58 return list.size(); 59 } 60 61 } 62 63 }
标签:
原文地址:http://www.cnblogs.com/zzw1994/p/4981638.html