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 ...
随机推荐
- underscorejs-shuffle学习
2.21 shuffle 2.21.1 语法 _.shuffle(list) 2.21.2 说明 返回一个随机乱序的list副本数组, 使用 Fisher-Yates shuffle 来进行随机乱序. ...
- python操作memcache
48.python 操作memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存 ...
- block 和delegate的用法
//block 和delegate的用法 设置代理 #import <UIKit/UIKit.h> typedef void (^ASIHeadersBlock)(NSString *my ...
- 查看Oracle是否锁表
--Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作,那么这些操作是怎么实现的呢?本文我们主要就介绍一下这部分内容. --(1)锁表查询的代码有以下的形式: select ...
- 取消开机window 设备选择
问题:开机,出现window 设备选择,在win7 页面上会停留几秒.解决方案:win+R 输入 -msconfig 进入引导 把延迟改为3
- Communication System
poj1018:http://poj.org/problem?id=1018 题意:某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产, ...
- AsyncCalls – Asynchronous function calls
AsyncCalls – Asynchronous function callsWith AsyncCalls you can execute multiple functions at the sa ...
- 了解运行时类型信息(RTTI)
RTTI需要引用单元TypeInfo GetPropInfo 函数用于获得属性的 RTTI 指针 PPropInfo.它有四种重载形式,后面三种重载的实现都是调用第一种形式.AKinds 参数用于限制 ...
- Netty4.0学习笔记系列之一:Server与Client的通讯
http://blog.csdn.net/u013252773/article/details/21046697 本文是学习Netty的第一篇文章,主要对Netty的Server和Client间的通讯 ...
- curl post传递json数据
有时想在命令行使用post http提交一个表单,比较常用的是POST模式和GET模式 GET模式什么option都不用,只需要把变量写在url里面就可以了 比如:curl http://www.wa ...