所以首先我从here捏了代码,我设置了页面标题来显示日期,这很好用,但我在这些位置上遇到了麻烦,以及实际发送的日期是什么Bundle,它从今天的日期开始,位置是2000,
我向右滑动,位置和日期分别为2002年和今天的2天,但是PagerTitleStrip将是正确的并明天说明。
如果我向左滑动,除了减少之外,也会发生同样的情况。
所以我的问题是,"你知道为什么会发生这种奇怪的行为吗?我该如何解决?"
所以这是代码,我做错了什么?
此外,当应用程序加载到ViewPager屏幕时,PagerTitleStrip将如下(我认为这是导致问题的原因)
[昨天的日期] [今天的日期] [明天约会]
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;
}
}
}
答案 0 :(得分:0)
基于Cursor的Adapter实现可以如下所示:
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 );
}
}