Android ViewPager FragmentPagerAdapter

ViewPager  里面放Fragment

Android ViewPager FragmentPagerAdapterAndroid ViewPager FragmentPagerAdapter

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lesson10_viewpager_fragmentpageradapter.MainActivity"> <android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </android.support.v4.view.ViewPager> </RelativeLayout>

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"> <EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入聊天消息" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send" /> </LinearLayout> <ListView
android:id="@+id/lv_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ll" /> </RelativeLayout>

fragment_chat

 <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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:padding="10dp"
android:gravity="center"
android:textSize="18sp"
android:textColor="@android:color/white"
android:text="电话列表"/>
<ListView
android:id="@+id/lv_call"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

fragment_call

 public class ChatFragment extends Fragment {
//设置假数据
List<String> mList = new ArrayList<>();
ListView lv_chat;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); for (int i = 0; i <50 ; i++) {
mList.add("邹:"+i);
}
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.chat_layout,null); return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); lv_chat = (ListView) view.findViewById(R.id.lv_chat);
lv_chat.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,mList));
} }

ChatFragment.java

 public class CallFragment extends Fragment {
//设置假数据
List<String> mList = new ArrayList<>();
ListView lv_call;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); for (int i = 0; i <100 ; i++) {
mList.add("电话号码"+i);
}
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.call_layout,null); return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); lv_call = (ListView) view.findViewById(R.id.lv_call);
lv_call.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,mList));
} }

CallFragment.java

 public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

     //适配器需要数据,这里是两页Fragment
List<Fragment> mList; public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList) {
super(fm);
this.mList = mList;
} @Override
public Fragment getItem(int position) {
return mList.get(position);
} @Override
public int getCount() {
return mList.size();
}
}

MyFragmentPagerAdapter.java

 public class MainActivity extends AppCompatActivity {

     List<Fragment> mList = new ArrayList<>();
ViewPager vp; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mList.add(new ChatFragment());
mList.add(new CallFragment()); vp = (ViewPager) findViewById(R.id.vp);
FragmentManager fm = getSupportFragmentManager(); vp.setAdapter(new MyFragmentPagerAdapter(fm,mList));
} //必须使用类的方法创建
//因为FragmentPagerAdapter构造方法需要一个FragmentManager,下面这种方式,管理器拿不到
//成员对象在onCreate()方法之前就加载了
/* private FragmentPagerAdapter adapter = new FragmentPagerAdapter(fm) {
@Override
public Fragment getItem(int position) {
return null;
} @Override
public int getCount() {
return 0;
}
}*/
}

MainActivity.java

上一篇:MySQL——约束(constraint)详解


下一篇:解决.NET Web API生成的Help无Controller说明&服务端接收不到请求