android ViewPager 与Fragment
ViewPager
左右滑动数据显示
1. 整体布局
FragmentLayout 容器包裹Fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="demo.yuchen.com.ex04_extras.MainActivity"> <!--<fragment-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:name="demo.yuchen.com.ex04_extras.MyFragment"-->
<!--android:id="@+id/fragment"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:layout_marginTop="136dp" />--> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加Fragment"
android:id="@+id/btnAdd"
android:onClick="btnAdd"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnAdd"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp"
android:id="@+id/container"></FrameLayout> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="替换"
android:id="@+id/btnReplace"
android:onClick="btnReplace"
android:layout_below="@+id/btnAdd"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
2.Fragment内容替换
public class MainActivity extends FragmentActivity {
public void btnAdd(View view) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MyFragment()).commit();
}
public void btnReplace(View view) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new ViewPagerFragment()).commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2. ViewPagerFragment
/**
* A simple {@link Fragment} subclass.
*/
public class ViewPagerFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.fragment_blank, container, false);
ViewPager viewPager = (ViewPager) layout.findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
return layout;
} class MyPagerAdapter extends FragmentPagerAdapter{ List<Fragment> fragmentList = new ArrayList<>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
fragmentList.add(new PagerItemFragment());
} @Override
public Fragment getItem(int position) {
return fragmentList.get(position);
} @Override
public int getCount() {
return fragmentList.size();
} }
}
3. fragment_blank.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPagerFragment"> <android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </FrameLayout>
4.
public class PagerItemFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pager_item, container, false);
}
}
android ViewPager 与Fragment的更多相关文章
- Android -- ViewPager、Fragment、状态保存、通信
工程架构 TabAFm到Tab ...
- Android Viewpager加Fragment做界面切换时数据消失的解决方式
今天遇到多个Fragment切换,回来后页面空白的情况,找到这个博客方法设置了一下,就可以了 vpAdapter = new VpAdapter(getSupportFragmentManager() ...
- Android -- ViewPager切换动画,PageTransformer
transformPage(View view, float position) view就是滑动中的那个view,position这里是float类型,是当前滑动状态的一个表示,比如当滑动到正全屏时 ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- Android ViewPager Fragment使用懒加载提升性能
Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...
- 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...
随机推荐
- bash仅仅读的环境变量
环境变量名 变量的用途 $0 程序的名字 $1~$9 命令參数1~9的值 $* 全部命令行參数的值 $@ 全部命令行參数的值.假设$@被""包含.即"$@",这 ...
- AVL树,红黑树,B-B+树,Trie树原理和应用
前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作 ...
- 设计模式学习笔记——Chain of Responsibility职责链模式
重点在链.一条链,如果本节点处理不了,则传递给下一个节点处理. 关键是如何传给下一个节点? 主要是由本节点决定传给哪一个节点. public class Client { public static ...
- LIS(最长上升子序列)的三种经典求法
求最长上升子序列的三种经典方案: 给定一个长度为 \(N\) 的数列,求它数值单调递增的子序列长度最大为多少.即已知有数列 \(A\) , \(A=\{A_1,A_2....A_n\}\) ,求 \( ...
- IE67 下 setattribute class 失效
解决办法.将class 换成 className ,同理.ff不能识别className,将其换成class element.setAttribute("class"," ...
- html5--6-24 css3前缀
html5--6-24 css3前缀 学习要点 掌握css3前缀的使用 CSS3目前很多新增属性尚未被W3C列为标准,对这些暂时未被公布为标准的属性,各家浏览器会在属性前加上前缀词,也将其称之为浏览器 ...
- [SoapUI] Jenkins 配置不同环境(TP, LIVE)
- 【POJ 2407】 Relatives
[题目链接] 点击打开链接 [算法] 欧拉函数 [代码] #include <algorithm> #include <bitset> #include <cctype& ...
- FTOUR2 - Free tour II
传送门 题目翻译的很清楚……似乎点分治的题题目描述都非常简洁. 还是那个操作,一条路径要么全部在一棵子树中,要么经过当前的重心,所以考虑点分治. 首先dfs求出重心的每一棵子树中,有i个黑点的最长路径 ...
- RobotFramework:App九宫格滑动解锁
转自:http://blog.csdn.net/codekxx/article/details/50577381 手势密码在很多手机应用都会运到,手势密码都要求至少连接4个点,但AppiumLibra ...