嵌套片段分页器.setCurrentItem不起作用

时间:2014-06-19 11:59:33

标签: android android-activity android-fragments callback android-viewpager

我的ViewPager中有一个片段,它有一个DatePicker,如果用户从选择器中选择一个日期,他会通过回调来返回它。

活动获取片段的ViewPagerContainer,并根据日期将寻呼机设置为另一天。

容器片段中的功能已成功到达并记录正确的日期编号,但寻呼机未更新,它保留在同一页面上...没有崩溃或其他东西..

我的代码:

MainAcitivty (调用containerfragment)

 public void onDatumSelected(int days) {
    RoosterPagerContainer positionRooster = (RoosterPagerContainer) fragmentje;
    positionRooster.updatePager(days);

}

fragmentje在navigationDrawer select ..

上设置
 case 0:
                getSupportActionBar().setIcon(R.drawable.ic_action_time);
                fragment = new RoosterPagerContainer();//this is the contianer
                this.fragmentje = fragment;

ContainerFragment (设置寻呼机不工作)

    @InjectView(R.id.vp_pages) ViewPager pager;
    public CarouselActivity theActivitiy;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)       {
        View view = inflater.inflate(R.layout.roosterfragment_container, container, false);
        ButterKnife.inject(this, view);

        pager.setAdapter(buildAdapter());
        pager.setCurrentItem(5000, false);
        pager.getAdapter().notifyDataSetChanged();
        pager.setOffscreenPageLimit(0);

        return view;
    }
    public void updatePager(int days)
    {
        Log.e(String.valueOf(days), "This is logged but the pager dont switch???");
        //pager.post(new Runnable() {
          //  public void run() {
                pager.setCurrentItem((5000 + days), true);
                pager.getAdapter().notifyDataSetChanged();
         //   }
      //  });

    }
    private PagerAdapter buildAdapter() {
        return(new BootstrapPagerAdapter(theActivitiy, getChildFragmentManager()));
    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        try {
            theActivitiy = (CarouselActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " ik snap er niks van");
        }

    }

ChildofContainerFragment (调用mainacitivty)

 OnHeadlineSelectedListener mCallback;

public interface OnHeadlineSelectedListener {
    public void onDatumSelected(int days);
}

@Override
public void onDateSet(CalendarDatePickerDialog dialog, int year, int monthOfYear, int dayOfMonth) {
    DateTime selectednow = DateTime.forDateOnly(year, (monthOfYear+1), dayOfMonth);
    DateTime today = DateTime.now(TimeZone.getDefault());
    //if(!(selectednow.isSameDayAs(selected)))
    //

    int days = (selectednow.getModifiedJulianDayNumber() - today.getModifiedJulianDayNumber());
    mCallback.onDatumSelected(days);
}
@Override
public void onAttach(Activity activity) throws ClassCastException {
    super.onAttach(activity);
    try {
        mCallback = (OnHeadlineSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity
                + " must implement OnHeadlineSelectedListener");
    }
}

public class BootstrapPagerAdapter扩展了FragmentPagerAdapter {

public BootstrapPagerAdapter(final FragmentManager fragmentManager) {
    super(fragmentManager);
   // this.ctxt = ctxt;
}

@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);

    Log.e("Datepager",String.valueOf(days));

    Bundle bundle = new Bundle();
    bundle.putString("date", days.format("YYYY-MM-DD").toString());

    RoosterFragment roosterFragment = new RoosterFragment();
    roosterFragment.setArguments(bundle);

    return roosterFragment;
}

}

(当我发布其他代码时,我会更改此代码中的某些内容,但会遇到同样的问题..)

此外,我使用支持旧版Android设备android.v4.Fragment等..

此外,我确信int天不是零,而是例如八天。

1 个答案:

答案 0 :(得分:0)

不知怎的,这是固定的碎片没有设置因为如果忘了我在导航抽屉里有两次这样就是修复..

   case 0:
                getSupportActionBar().setIcon(R.drawable.ic_action_time);
                fragment = new RoosterPagerContainer();
                this.fragmentje = fragment;
                break;
            case 1:
                getSupportActionBar().setIcon(R.drawable.ic_action_time);
                fragment = new RoosterPagerContainer();
                this.fragmentje = fragment;//this was not before...
                break;