“无限"表示滚动viewpager,增加/减少天数 [英] "infinite" scrolling viewpager with days incrementing/decrementing
问题描述
因此,首先,我从此处捏住了代码,设置页面标题以显示日期,效果很好,但我在位置和捆绑中实际发送的日期方面遇到了麻烦,开始时是今天的日期,位置是2000,
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,
我向右滑动,位置和日期分别为2002年和今天之后的2天,但是PagerTitleStrip正确无误,并说了明天.
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?
此外,当应用程序加载到ViewPager屏幕时,PagerTitleStrip将如下所示(我认为这是引起问题的原因)
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;
}
}
}
推荐答案
基于Cursor的Adapter实现如下所示:
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 );
}
}
这篇关于“无限"表示滚动viewpager,增加/减少天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!