编辑:与无尽的FragmentStatePagerAdapter相关的问题

时间:2016-10-09 03:48:22

标签: android calendar fragment android-adapter

我正在实现一个无限的FragmentStatePagerAdapter来显示几个月。我已经在SO上看到了关于内容的一些链接:

Calendar and PagerAdapter - Unlimited Views

Infinite ViewPager

我相信这两个问题都有很多变化才能得到结果。我的意思是用更少的代码获得相同的结果。

下面的第一个类是我的基于FragmentStatePagerAdapter的适配器:

class CalendarAdapter extends FragmentStatePagerAdapter {
    private ViewPager viewPager;
    private Calendar calendar = Calendar.getInstance();

public CalendarAdapter(FragmentManager fm) {
    super(fm);
}

public void setViewPager( ViewPager viewPager ){
    this.viewPager = viewPager;
}

@Override
public Fragment getItem( int position ) {
    MonthFragment month = null;
    if( position == viewPager.getCurrentItem() ) {
        month = createMonth( 0 );
        Log.d( "DEBUG", "NO POSITION: " );
    }
    else if( position < viewPager.getCurrentItem() ) {
        month = createMonth( -1 );
        Log.d( "DEBUG", "LESS POSITION: " );
    }
    else if( position > viewPager.getCurrentItem() ) {
        month = createMonth( 1 );
        Log.d( "DEBUG", "MORE POSITION: " );
    }

    return month;
}

@Override
public int getCount() {
    return Integer.MAX_VALUE;
}

@Override
public int getItemPosition( Object object ){
    return POSITION_NONE;
}

private MonthFragment createMonth( int whatPosition ){
    Bundle bundle = new Bundle();
    bundle.putInt( "calendarPosition", whatPosition );
    MonthFragment month = new MonthFragment();
    month.setCalendar( calendar );
    month.setArguments( bundle );
    return month;
}

这是与基于Fragment的月份相关的代码:

public class MonthFragment extends Fragment {
private Calendar calendar;
private String[] months = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
private String dateFormat = "yyyy";
private TextView monthAndYear;
private GridView monthDays;
private int monthColsAndRows, calendarPosistion, currentMonth;

@Override
public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    //Calendar has 7 columns and six rows
    monthColsAndRows = 7 * 6;
    Bundle args  = getArguments();
    calendarPosistion = args.getInt( "calendarPosition" );
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState ){
    calendar.set( Calendar.HOUR_OF_DAY, 0 );
    calendar.set( Calendar.MINUTE, 0 );
    calendar.set( Calendar.SECOND, 0 );
    calendar.set( Calendar.MILLISECOND, 0 );

    View view    = ( View ) inflater.inflate( R.layout.month_fragment, parent, false );
    monthAndYear = ( TextView ) view.findViewById( R.id.month_and_year );
    monthDays    = ( GridView ) view.findViewById( R.id.month_days );
    fillCalendar();
    return view;
}

public void setCalendar( Calendar calendar ){
    this.calendar = calendar;
}

private void fillCalendar(){
    ArrayList<Date> days = new ArrayList<>();

    Calendar tempCalendar = ( Calendar )calendar.clone();

    tempCalendar.set( Calendar.DAY_OF_MONTH, 1 );
    tempCalendar.add( Calendar.DAY_OF_MONTH, -( tempCalendar.get( Calendar.DAY_OF_WEEK ) -1 ) );

    tempCalendar.setTimeInMillis( calendar.getTimeInMillis() );

    currentMonth = tempCalendar.get( Calendar.MONTH ) + 1;

    while( days.size() < monthColsAndRows ){
        days.add( tempCalendar.getTime() );
        tempCalendar.add( Calendar.DAY_OF_MONTH, 1 );
    }

    int monthPos = tempCalendar.get( Calendar.MONTH ) - 1 < 0 ? 0 : tempCalendar.get( Calendar.MONTH ) - 1;

    //Log.e( "ERROR", "MONTH POS: "+monthPos);

    monthAndYear.setText( months[ monthPos ] + "/" + calendar.get( Calendar.YEAR ) );
    monthDays.setAdapter( new DaysAdapter( getContext(), days ) );
}

private class DaysAdapter extends ArrayAdapter< Date > {
    //Irrelevant to the question, code ommited
    ...
}

}

出现了一些问题:

  • 适配器的getItem方法在启动时被调用两次;

  • 非常下个月,第一个保持不变。它没有增加;

  • 我可以坚持几个月。但无限期地回去没有。它锁定在位置0;

编辑:第一个问题与适配器的本质有关。为了提供平滑过渡,它创建了第一个视图和下一个缓存视图。

0 个答案:

没有答案