Android ViewPager FragmentPagerAdapter
ViewPager 里面放Fragment
<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
Android ViewPager FragmentPagerAdapter的更多相关文章
- Android ViewPager Fragment使用懒加载提升性能
Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...
- Android ViewPager的简单实现
研究了两天ViewPager,看了几篇网上的帖子,但总的来说看得一头雾水,理不清头绪,偶然发现了一篇简单易懂的帖子,讲的调理比较清晰,原文链接附在文后. 在本例中使用ViewPager + Fra ...
- Android中FragmentPagerAdapter对Fragment的缓存(一)
ViewPager + FragmentPagerAdapter,时我们经常使用的一对搭档,其实际应用的代码也非常简单,但是也有一些容易被忽略的地方,这次我们就来讨论下FragmentPagerAda ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- 笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法
TabLayout的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡
<Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...
- Android ViewPager 用法
Android ViewPager 用法 场景:一般第一次打开应用程序时,程序会有一个提示页来给展现应用程序都有哪些功能:或者程序更新时,又更新哪些新特性,都可以使用ViewPager Demo 描述 ...
- Android ViewPager再探:增加滑动指示条
上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...
- Android ViewPager初探:让页面滑动起来
下一篇:<Android ViewPager再探:增加滑动指示条> ViewPager需要用到适配器PagerAAdapter,以下四个函数需要重写: instantiateItem(Vi ...
随机推荐
- 关于jQuery的cookies插件2.2.0版设置过期时间的说明
欢迎转载,转载请注明作者RunningOn jQuery应该是各位用JavaScript做web开发的常用工具了,它有些插件能非常方便地操作cookie. 不过非常让人郁闷的是,网上几乎所有人对于这些 ...
- WordPress防暴力破解:安全插件和用.htpasswd保护WordPress控制面板
正在用Wordpress的博主们一定知道最近全球兴起的一波黑客锁定Wordpress暴力破解控制面板密码的风波了,据CloudFlare执行长Matthew Prince所说,所谓的暴力密码攻击是输入 ...
- php实现的太平洋时间和北京时间互转的自定义函数
date_default_timezone_set('Asia/Shanghai'); $time = time(); } date_default_timezone_set('Pacific/Api ...
- redis-消息订阅
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称发布内容 客户端例子: redis > subscribe news Reading messages... ...
- 基于cx_freeze编译PyQt4程序(numpy & scipy)
当开发完成PyQt4程序后,需要提供给他人使用,这时最好的办法是将Python程序编译成exe文件. 通常我采用cx_freeze完成这个工作,即编写setup.py文件,执行python setup ...
- day11基础代码——函数指针
// // main.m // Demo11 // // Created by scjy on 15/10/29. // Copyright © 2015年 lizhipeng. All ri ...
- EJB
Enterprise JavaBean,企业级javabean,是J2EE的一部分,定义了一个用于 开发基于组件的企业多重应用程序的标准.其特点包括网络服务支持和核心开发工具(SDK). 是Jav ...
- 支付宝集成获取私钥与公钥-b
项目需要,需要在客户端集成支付宝接口.就研究了一下:因为使用支付宝接口,就需要到支付宝官网:注册帐号,并申请.下面讲的是申请好之后的操作.登录成功之后, 店家我的商家服务—在页面的下方找到——&g ...
- LINUX下安装ORACLE,完全搞定
参考文档: http://www.tuicool.com/articles/eE3mmy http://blog.chinaunix.net/uid-11209572-id-3599052.html
- memcached采用的网络模型
memcached采用的网络模型是早前提到的半同步半异步的网络模型. 简 单的说,大致流程就是:主线程负责接收新的连接,接收到新的连接之后,选择一个worker副线程,将该新连接push到副线程的连接 ...