Android ViewPager FragmentPagerAdapter
ViewPager 里面放Fragment
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/activity_main" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.lesson10_viewpager_fragmentpageradapter.MainActivity"> 7 8 9 <android.support.v4.view.ViewPager 10 android:id="@+id/vp" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content"> 13 14 </android.support.v4.view.ViewPager> 15 16 17 </RelativeLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical"> 5 6 <LinearLayout 7 android:id="@+id/ll" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:layout_alignParentBottom="true" 11 android:orientation="horizontal"> 12 13 <EditText 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_weight="1" 17 android:hint="请输入聊天消息" /> 18 19 <Button 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="send" /> 23 24 </LinearLayout> 25 26 <ListView 27 android:id="@+id/lv_chat" 28 android:layout_width="match_parent" 29 android:layout_height="match_parent" 30 android:layout_above="@id/ll" /> 31 32 </RelativeLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical"> 5 <TextView 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:background="@android:color/holo_blue_dark" 9 android:padding="10dp" 10 android:gravity="center" 11 android:textSize="18sp" 12 android:textColor="@android:color/white" 13 android:text="电话列表"/> 14 <ListView 15 android:id="@+id/lv_call" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent"/> 18 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class ChatFragment extends Fragment { 2 //设置假数据 3 List<String> mList = new ArrayList<>(); 4 ListView lv_chat; 5 @Override 6 public void onCreate(@Nullable Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 9 for (int i = 0; i <50 ; i++) { 10 mList.add("邹:"+i); 11 } 12 } 13 14 @Nullable 15 @Override 16 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 17 View layout = inflater.inflate(R.layout.chat_layout,null); 18 19 return layout; 20 } 21 22 @Override 23 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 24 super.onViewCreated(view, savedInstanceState); 25 26 lv_chat = (ListView) view.findViewById(R.id.lv_chat); 27 lv_chat.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,mList)); 28 } 29 30 31 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class CallFragment extends Fragment { 2 //设置假数据 3 List<String> mList = new ArrayList<>(); 4 ListView lv_call; 5 @Override 6 public void onCreate(@Nullable Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 9 for (int i = 0; i <100 ; i++) { 10 mList.add("电话号码"+i); 11 } 12 } 13 14 @Nullable 15 @Override 16 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 17 View layout = inflater.inflate(R.layout.call_layout,null); 18 19 return layout; 20 } 21 22 @Override 23 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 24 super.onViewCreated(view, savedInstanceState); 25 26 lv_call = (ListView) view.findViewById(R.id.lv_call); 27 lv_call.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,mList)); 28 } 29 30 31 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class MyFragmentPagerAdapter extends FragmentPagerAdapter{ 2 3 //适配器需要数据,这里是两页Fragment 4 List<Fragment> mList; 5 6 public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList) { 7 super(fm); 8 this.mList = mList; 9 } 10 11 12 @Override 13 public Fragment getItem(int position) { 14 return mList.get(position); 15 } 16 17 @Override 18 public int getCount() { 19 return mList.size(); 20 } 21 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class MainActivity extends AppCompatActivity { 2 3 List<Fragment> mList = new ArrayList<>(); 4 ViewPager vp; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 mList.add(new ChatFragment()); 12 mList.add(new CallFragment()); 13 14 vp = (ViewPager) findViewById(R.id.vp); 15 FragmentManager fm = getSupportFragmentManager(); 16 17 vp.setAdapter(new MyFragmentPagerAdapter(fm,mList)); 18 } 19 20 //必须使用类的方法创建 21 //因为FragmentPagerAdapter构造方法需要一个FragmentManager,下面这种方式,管理器拿不到 22 //成员对象在onCreate()方法之前就加载了 23 /* private FragmentPagerAdapter adapter = new FragmentPagerAdapter(fm) { 24 @Override 25 public Fragment getItem(int position) { 26 return null; 27 } 28 29 @Override 30 public int getCount() { 31 return 0; 32 } 33 }*/ 34 }