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

Viewpager in Android to switch between days endlessly(Android 中的 Viewpager 可以无休止地在几天之间切换)

本文介绍了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 可以无休止地在几天之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android 中的 Viewpager 可以无休止地在几天之间切换

基础教程推荐