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. sed 常用命令

    删除以ifeq开头的行 sed -i "/^ifeq/d" file 删除空行 sed -i '/^$/d' file

  2. linux led子系统(二)

    对于led子系统中,有那么多得trigger,下面就来简单了解下. 1.default-on static void defon_trig_activate(struct led_classdev * ...

  3. linux下提示command not found

    首先就要考虑root 的$PATH里是否已经包含了这些环境变量. 主要是这四个:/bin ,/usr/bin,/sbin,/usr/sbin. 四个主要存放的东东: ./bin: bin为binary ...

  4. (org.openqa.selenium.WebDriverException: Unable to launch the app: Error: Trying to start logcat capture but it's already started! )错误解决办法

    新增: capabilities.setCapability("autoLaunch",false); 将setup中的: driver = new AndroidDriver(n ...

  5. Appium&python

    Appium官网所描述的特性,都很吸引人,刚好最近在研究Mobile Automation Testing,所以很有兴趣探索下Appium这个年轻的工具. 不过看了官网的documents,实在是让初 ...

  6. sublime 中配置 python 运行

    运行是按快捷键 ”ctrl + B“ Preferences->Browse Packages->Python->Python.sublime-build 编辑这个文件.   修改成 ...

  7. eclipse无法启动问题记录

    几天没开eclipse,居然报错“can not unload……”,搜索答案发现没有准确的,遵从了一个多人顶赞的办法重下eclipse,把配置文件拷贝一份,结果悲剧了,虽然能够打开了,但我之前配置的 ...

  8. Python中怎样用pip安装外部主机文件

    在python中安装非自带python模块.有三种方式: easy_install pip 下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的文件夹后运行python setu ...

  9. 【读后感】Netty 系列之 Netty 高性能之道 - 相比 Mina 怎样 ?

    [读后感]Netty 系列之 Netty 高性能之道 - 相比 Mina 怎样 ? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  10. Redis+EJB实现缓存(一)

        上篇博客大概的对Redis做了一个主要的了解.由于刚刚接触自己也不太明确.所以上篇博客写的乱七八糟的.这篇由于项目须要,学习了一下Redis和EJB集成. 如今脑子相对照较清晰了一些. 实现思 ...