“infinite” scrolling viewpager with days incrementing/decrementing

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

So to start with I pinched the code from here, I have set the page titles to display the date, which works well, but I am having trouble with the positions and what Date is actually being sent over in the Bundle, it starts off with it being todays date and the position being 2000,

I swipe right and the position and date go to 2002 and 2 days after today, but the PagerTitleStrip will be correct and say its tomorrow.

The same happens if I swipe left, apart from its decremented.

So my question is, "Do you know why this strange behaviour is happening and how can I fix it?"

So here is the code, am I doing something wrong?

Also, when the app loads up to the ViewPager screen, the PagerTitleStrip will be as follows (I think this is what is causing the issue)

[yesterdays date] [todays date] [tomorrows date]

public class MainMenuActivity extends AppCompatActivity {  /**  * The {@link android.support.v4.view.PagerAdapter} that will provide  * fragments for each of the sections. We use a  * {@link FragmentPagerAdapter} derivative, which will keep every  * loaded fragment in memory. If this becomes too memory intensive, it  * may be best to switch to a  * {@link android.support.v4.app.FragmentStatePagerAdapter}.  */ BootstrapPagerAdapter mSectionsPagerAdapter;  /**  * The {@link ViewPager} that will host the section contents.  */ ViewPager mViewPager; ArrayList<Set> selected = new ArrayList<>();  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main_menu_setup);     try {         getSupportActionBar().setDisplayShowHomeEnabled(true);         getSupportActionBar().setLogo(R.mipmap.ic_launcher);         getSupportActionBar().setDisplayUseLogoEnabled(true);         getSupportActionBar().setTitle("ProjectME");     } catch(NullPointerException n) {         Log.v("nullPointerCaught", n.toString());     }       // Create the adapter that will return a fragment for each of the three     // primary sections of the activity.     mSectionsPagerAdapter = new BootstrapPagerAdapter(getResources(), getSupportFragmentManager());      // Set up the ViewPager with the sections adapter.     mViewPager = (ViewPager) findViewById(R.id.pager);     mViewPager.setAdapter(mSectionsPagerAdapter);     mViewPager.setCurrentItem(2000);  }   @Override public boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.     getMenuInflater().inflate(R.menu.menu_main, menu);     return true; }  @Override public boolean onOptionsItemSelected(MenuItem item) {     // Handle action bar item clicks here. The action bar will     // automatically handle clicks on the Home/Up button, so long     // as you specify a parent activity in AndroidManifest.xml.     int id = item.getItemId();      //noinspection SimplifiableIfStatement     if (id == R.id.action_settings) {         return true;     }      if(id == R.id.action_add) {         Intent intent = new Intent(MainMenuActivity.this, CategoryListView.class);         startActivity(intent);     }      if(id == R.id.action_cal) {         Intent intent = new Intent(MainMenuActivity.this, CalendarView.class);         startActivity(intent);     }      if(id == R.id.action_copy) {         Toast.makeText(getApplicationContext(), "Copy has been clicked", Toast.LENGTH_SHORT).show();     }      return super.onOptionsItemSelected(item); }  public ArrayList<Set> getSelected() {     return selected; }  public void setSelected(ArrayList<Set> selected) {     this.selected = selected; }  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 super.getItemPosition(object);     }      @Override     public CharSequence getPageTitle(int position) {         DateTime pagerdate = DateTime.now(TimeZone.getDefault());         DateTime days = pagerdate.plusDays(position - 2000);         return days.format("DD/MM/YYYY").toString();     }      @Override     public Fragment getItem(int position) {           DateTime pagerdate = DateTime.now(TimeZone.getDefault());         DateTime days = pagerdate.plusDays(position - 2000);          Bundle bundle = new Bundle();         bundle.putString("date", days.format("DD/MM/YYYY").toString());         bundle.putInt("position", position);         Log.v("date", days.format("DD/MM/YYYY").toString());         Log.v("position", position + "");         MainMenuView2 mainMenuView2 = new MainMenuView2();         mainMenuView2.setArguments(bundle);         return mainMenuView2;     } } } 

回答1:

the Cursor-based Adapter implementation can look like this:

class SwipeAdapter extends FragmentStatePagerAdapter  {    private final Cursor cursor;    private final int count;    public SwipeAdapter( FragmentManager fm ) {     super( fm );     cursor = initCursorSomehow();     count = cursor.getCount();   }    @Override   public int getCount() {     return count;   }    @Override   public CharSequence getPageTitle( int position ) {     cursor.moveToPosition( position );     return cursor.getString( cursor.getColumnIndex( "title" ) );   }    @Override   public Fragment getItem( int position ) {     Bundle b = new Bundle();     cursor.moveToPosition( position );     b.putString( "someDate", cursor.getString( cursor.getColumnIndex( "someDate" ) ) );     b.putString( "someInt", cursor.getInt( cursor.getColumnIndex( "someInt" ) ) );     return Fragment.instantiate( getApp(), SomeFragment.class.getName(), b );   } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!