Android的ViewPager带日期的工作 [英] Android ViewPager working with date

查看:105
本文介绍了Android的ViewPager带日期的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的测试Android的应用程序中使用viewpager。

我想使用3个片段与动态内容。我可以做没完没了滚动?因为我想用这样的日期的工作:2fragment - (的currentdate ||日期),1fragment - (日期-1),3fragment - (日期+ 1)

什么是去了解这一点的最好方法是什么?


解决方案

创建自定义FragmentStatePagerAdapter。
重写(强制)的getItem方法:

  @覆盖
  公共无效的getItem(INT位置){
   开关(位置){
   情况下0:
          //创建最左边的片段日期
          DATE1 == XXX;
          date2的== YYY;
          //将其添加到捆绑ARG
          束束=新包();
          bundle.add等等等等
          //创建您的片段
          片段片段=新YourFragmentWithTheList();
          fragment.setArgs(包);
          返回片段;
      情况1:
          //创建日期为中心片段
          [做同上]
          返回片段;
      案例2:
          //现在你最右边的片段。
          同上...
   }
返回NULL
}


  1. 此外您必须替换getCount将...

      @覆盖
    公众诠释的getCount(){
        返回3; //你只有三个硬盘codeD断枝
    }


然后在YourFragmentWithTheList.java你必须得到来自参数的日期,并做平常ListFragment东西(创建列表,初始化适配器,加载更多的在底部的时候,等)。你有日期以获得每一个正确的数据。所以,你的片段必须是假人,并期望日期进来的参数。

I'm using viewpager in my test android-app.

I'd like to use 3 fragments with dynamic content. Can i make "endless" scrolling? Because i want to work with Date like this: 2fragment - (currentdate||date), 1fragment - (date-1), 3fragment - (date+1)

What's the best way to go about this?

解决方案

Create a custom FragmentStatePagerAdapter. Override the (mandatory) getItem method:

  @Override
  public void getItem(int position){ 
   switch (position) {
   case 0:
          // create your dates for the leftmost fragment  
          date1 == xxx; 
          date2 == yyy;
          // add it to a Bundle arg
          Bundle bundle = new Bundle();
          bundle.add blah blah
          // Create your Fragment
          Fragment fragment = new YourFragmentWithTheList();
          fragment.setArgs(bundle);
          return fragment;
      case 1:
          // create your dates for the center fragment 
          [ do the same as above ]
          return fragment;
      case 2: 
          // now your rightmost fragment.
          same as above… 
   }
return null
}

  1. Also you must override getCount…

    @Override
    public int getCount() {
        return 3; // you only have three hardcoded frags
    }
    

And then in YourFragmentWithTheList.java you have to get the date from the arguments and do the usual ListFragment stuff (create the list, initialize the adapter, load more when at the bottom, etc.). You have the dates to get the correct data for each one. So your Fragment must be dummy and expect the dates to come in the arguments.

这篇关于Android的ViewPager带日期的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆