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的更多相关文章

  1. Android -- ViewPager、Fragment、状态保存、通信

    工程架构                                                                                      TabAFm到Tab ...

  2. Android Viewpager加Fragment做界面切换时数据消失的解决方式

    今天遇到多个Fragment切换,回来后页面空白的情况,找到这个博客方法设置了一下,就可以了 vpAdapter = new VpAdapter(getSupportFragmentManager() ...

  3. Android -- ViewPager切换动画,PageTransformer

    transformPage(View view, float position) view就是滑动中的那个view,position这里是float类型,是当前滑动状态的一个表示,比如当滑动到正全屏时 ...

  4. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  5. Android ViewPager Fragment使用懒加载提升性能

     Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...

  6. 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  7. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  8. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  9. Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

随机推荐

  1. 李洪强iOS开发之- 点击屏幕遮挡键盘

    李洪强iOS开发之- 点击屏幕遮挡键盘 实现的效果:  01 - 给当前的view添加点击事件,使点击屏幕的时候,让键盘退出 /** * 点击屏幕 隐藏键盘 * * @param tap */-(vo ...

  2. 关闭mongodb 集群

    [root@hadoop1 ~]# ps -aux | grep mongodb; root ? Sl : : /usr/local/mongodb/bin/mongod -f /usr/local/ ...

  3. 正则表达式pattern的匹配格式

    0> 匹配 -------------------------------------------------------------------------------- (pattern)  ...

  4. Configuring Your EMS Server or EMS Console Server on Windows/Linux

    EMS Configuration Files RAD Studio provides the scripts to render the web-browser console, the EMS s ...

  5. SDUT OJ 1598 周游列国

    周游列国 Time Limit: 1000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 大家都知道孔子吧,春秋战国时候的一个老头儿.当时出国还不用护照,所以他经 ...

  6. hdu 2066 一个人的旅行 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目意思:给出T条路,和草儿家相邻的城市编号,以及草儿想去的地方的编号.问从草儿家到达草儿想去的 ...

  7. MD5Util1

    package com.cc.hkjc.util; import java.math.BigInteger;import java.security.MessageDigest;import java ...

  8. OpenCV——PS滤镜算法之 Ellipsoid (凹陷)

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  9. 「LuoguP3252」 [JLOI2012]树

    Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节点1是根节点,根的深度是0,它的儿子节点的深 ...

  10. 【POJ 3461】 Oulipo

    [题目链接] 点击打开链接 [算法] KMP [代码] #include <algorithm> #include <bitset> #include <cctype&g ...