<legend id='hFJKS'><style id='hFJKS'><dir id='hFJKS'><q id='hFJKS'></q></dir></style></legend>
      <i id='hFJKS'><tr id='hFJKS'><dt id='hFJKS'><q id='hFJKS'><span id='hFJKS'><b id='hFJKS'><form id='hFJKS'><ins id='hFJKS'></ins><ul id='hFJKS'></ul><sub id='hFJKS'></sub></form><legend id='hFJKS'></legend><bdo id='hFJKS'><pre id='hFJKS'><center id='hFJKS'></center></pre></bdo></b><th id='hFJKS'></th></span></q></dt></tr></i><div id='hFJKS'><tfoot id='hFJKS'></tfoot><dl id='hFJKS'><fieldset id='hFJKS'></fieldset></dl></div>
    1. <tfoot id='hFJKS'></tfoot>

          <bdo id='hFJKS'></bdo><ul id='hFJKS'></ul>
      1. <small id='hFJKS'></small><noframes id='hFJKS'>

      2. Android 中的 Viewpager 可以无休止地在几天之间切换

        时间:2023-10-04

            1. <tfoot id='9leKL'></tfoot>

              <small id='9leKL'></small><noframes id='9leKL'>

              <legend id='9leKL'><style id='9leKL'><dir id='9leKL'><q id='9leKL'></q></dir></style></legend>
                <tbody id='9leKL'></tbody>
              <i id='9leKL'><tr id='9leKL'><dt id='9leKL'><q id='9leKL'><span id='9leKL'><b id='9leKL'><form id='9leKL'><ins id='9leKL'></ins><ul id='9leKL'></ul><sub id='9leKL'></sub></form><legend id='9leKL'></legend><bdo id='9leKL'><pre id='9leKL'><center id='9leKL'></center></pre></bdo></b><th id='9leKL'></th></span></q></dt></tr></i><div id='9leKL'><tfoot id='9leKL'></tfoot><dl id='9leKL'><fieldset id='9leKL'></fieldset></dl></div>

                  <bdo id='9leKL'></bdo><ul id='9leKL'></ul>
                  本文介绍了Android 中的 Viewpager 可以无休止地在几天之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我目前正在制作一个应用程序,学生可以在其中查看课程网格.使用 json 文件 检索课程.现在日期更改是通过操作栏中的按钮完成的,但我想让它也适用于平滑滑动效果.

                  I am currently making a app where students can view there lesson grid. The lessons are retrieved using a json file. Now the date changing is done via buttons in the actionbar, but i want to make it also work with smooth swipe effects.

                  唯一需要更改的是左侧滑动上的 SlectedDate-1.右侧的 SelectedDate+1 滑动.

                  The only things that need to change is the SlectedDate-1 on left swipe. the SelectedDate+1 on right swipe.

                  我用 ViewPagers 试过了,但是没有用.有人知道怎么做吗?也许有 3 个 viewpagers 的东西?请提供详细的代码,因为我对 viewpagers 代码不是很熟悉!

                  I tried it with ViewPagers but it didn't work does anybody know how to do it? Maybe something with 3 viewpagers?, please a detailed code because I'm not very familar with viewpagers code!

                  我已经尝试了 Android ViewPager 使用日期代码,但它没有不适合我.

                  I've tried out Android ViewPager working with date code but it doesn't work for me.

                   private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
                      public ScreenSlidePagerAdapter(FragmentManager fm) {
                          super(fm);
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                          String myFormat = "yyyy-MM-dd"; 
                          SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                  
                           switch (position) {
                             case 0:
                                    // create your dates for the leftmost fragment  
                                      myCalendar.add(Calendar.DAY_OF_YEAR, -1);
                                      final String dateje = sdf.format(myCalendar.getTime());
                                     // mPager.setCurrentItem(1);
                                    return ScreenSlidePageFragment.create(dateje);
                  
                  
                                case 1:
                                    // create your dates for the center fragment 
                                    myCalendar.add(Calendar.DAY_OF_YEAR, 0);
                                  final String dateje2 = sdf.format(myCalendar.getTime());
                                //mPager.setCurrentItem(1);
                                return ScreenSlidePageFragment.create(dateje2);
                  
                                case 2: 
                                    myCalendar.add(Calendar.DAY_OF_YEAR, 1);
                                  final String dateje3 = sdf.format(myCalendar.getTime());
                                //mPager.setCurrentItem(1);
                  
                                return ScreenSlidePageFragment.create(dateje3);
                             }
                  
                        return null;
                      }
                  
                      @Override
                      public int getCount() {
                          return 3;
                      }
                  

                  我的示例片段

                  public class ScreenSlidePageFragment extends Fragment {
                      /**
                       * The argument key for the page number this fragment represents.
                       */
                      public static final String ARG_PAGE = "page";
                  
                      /**
                       * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
                       */
                      private int mPageNumber;
                  
                      /**
                       * Factory method for this fragment class. Constructs a new fragment for the given page number.
                       */
                      public static ScreenSlidePageFragment create(String dateNumber) {
                          ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
                          Bundle args = new Bundle();
                          args.putString(ARG_PAGE, dateNumber);
                          fragment.setArguments(args);
                          return fragment;
                      }
                  
                      public ScreenSlidePageFragment() {
                      }
                  
                      @Override
                      public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          mPageNumber = 1;
                  
                  
                  
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                          // Inflate the layout containing a title and body text.
                          ViewGroup rootView = (ViewGroup) inflater
                                  .inflate(R.layout.fragment_screen_slide_page, container, false);
                  
                  
                          String date = this.getArguments().getString(ARG_PAGE);
                          // Set the title view to show the page number.
                          ((TextView) rootView.findViewById(android.R.id.text1)).setText(date);
                  
                          return rootView;
                      }
                  
                      /**
                       * Returns the page number represented by this fragment object.
                       */
                      public int getPageNumber() {
                          return mPageNumber;
                      }
                  }
                  

                  推荐答案

                  我刚刚解决了这个问题,这段代码有点基于Kuffs 但我没有得到数组中的日期,因为将很多天放入数组中会带来很多性能.

                  I have fixed this problems a little time ago, this code is a bit based on the answer from Kuffs but I don't get the date in an array because it's a lot of performance to put a lot of days in array.

                  代码摘要(1) 该代码生成了一个 FragmentPager ,其中包含大约 10.000 个页面.在主活动初始化应用程序将当前页面设置为中间(5000).

                  Summary of code (1) The code makes a FragmentPager with around 10.000 pages in it. On the main activity init the app set the current page to the middle(5000).

                  pager.setAdapter(new BootstrapPagerAdapter(getResources(), getSupportFragmentManager()));
                  pager.setCurrentItem(5000, false);
                  pager.post(new Runnable() {
                      public void run() {
                          pager.setCurrentItem(5000, false);
                      }
                  });
                  pager.getAdapter().notifyDataSetChanged();
                  pager.setOffscreenPageLimit(0);
                  

                  bootstrappageradapter中的代码总结(2)

                  代码查看用户滚动的位置并查找现在的日期

                  The code looks what position the user has scrolled and looks for the date now

                  DateTime pagerdate = DateTime.now(TimeZone.getDefault());
                  DateTime days = pagerdate.plusDays(position - 5000);
                  

                  例如用户接下来三天刷卡.位置是5003,所以总和是5003-5000=3天

                  e.g. user swiped three days next. position is 5003, so the sum is 5003-5000=3 days

                  使用 date4j 库 可以轻松计算天数加上 3 天(我强烈推荐它!)然后我制作了一个新片段,其中包含日期!

                  The days count easily plus 3 days with date4j library (I highly recommend it!) then I make a new fragment with the date in its bundle!

                  public class BootstrapPagerAdapter extends FragmentPagerAdapter
                  {
                      /**
                      * Create pager adapter
                      *
                      * @param resources
                      * @param fragmentManager
                      */
                      public BootstrapPagerAdapter(Resources resources, FragmentManager fragmentManager) {
                          super(fragmentManager);
                      }
                  
                      @Override
                      public int getCount() {
                          return 10000;
                      }
                  
                      @Override
                      public int getItemPosition(Object object){
                          return FragmentStatePagerAdapter.POSITION_NONE;
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                          DateTime pagerdate = DateTime.now(TimeZone.getDefault());
                          DateTime days = pagerdate.plusDays(position - 5000);
                  
                          Bundle bundle = new Bundle();
                          bundle.putString("date", days.format("YYYY-MM-DD").toString());
                  
                          RoosterFragment roosterFragment = new RoosterFragment();
                          roosterFragment.setArguments(bundle);
                  
                          return roosterFragment;
                      }
                  }
                  

                  你可以用更高的数字来伪造无休止的计数,比如

                  You could fake the endlessly count with more high numbers like

                  • 0001 年
                  • 3999 年

                  大约:1.500.000 天相信我,没有人会一路刷到这么远!

                  Around the: 1.500.000 days believe me no one is gonna go this far all the way swiping!

                  我尝试了很多无限的例子,但这是我认为唯一可行的例子!

                  I've tried the a lot infinite examples but this is the only working example I think!

                  如果它对您有用,请点赞,不要羞于寻求更多示例或帮助您的应用!

                  Upvote if it worked for you and don't be shy to ask for more examples or help with your app!

                  但是,如果有人得到像真正无限的浏览器这样没有虚假计数的东西,那就太酷了;)

                  However it would be cool if someone got something like a really infinte viewpager without fake counts and so on ;)

                  这篇关于Android 中的 Viewpager 可以无休止地在几天之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:一侧 ViewPager 仅滑动 下一篇:在恢复活动时刷新 FragmentStatePagerAdapter 上的图像

                  相关文章

                    1. <i id='1POpg'><tr id='1POpg'><dt id='1POpg'><q id='1POpg'><span id='1POpg'><b id='1POpg'><form id='1POpg'><ins id='1POpg'></ins><ul id='1POpg'></ul><sub id='1POpg'></sub></form><legend id='1POpg'></legend><bdo id='1POpg'><pre id='1POpg'><center id='1POpg'></center></pre></bdo></b><th id='1POpg'></th></span></q></dt></tr></i><div id='1POpg'><tfoot id='1POpg'></tfoot><dl id='1POpg'><fieldset id='1POpg'></fieldset></dl></div>

                      <small id='1POpg'></small><noframes id='1POpg'>

                    2. <legend id='1POpg'><style id='1POpg'><dir id='1POpg'><q id='1POpg'></q></dir></style></legend>

                        <bdo id='1POpg'></bdo><ul id='1POpg'></ul>
                    3. <tfoot id='1POpg'></tfoot>