1

我有一個片段在我的ViewPager有一個DatePicker,如果用戶從選擇器中選擇一個日期,他通過回調到活動返回它。嵌套片段尋呼機.setCurrentItem不工作

活動獲取片段的ViewPagerContainer,並根據日期將尋呼機設置爲另一天。

容器片段的功能,成功地達到了它記錄天權數,但不更新尋呼機它保持在同一頁上......沒有崩潰或東西..

我的代碼:

MainAcitivty(調用containerfragment)

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

} 

fragmentje設置在navigationDrawer選擇..

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"); 
    } 
} 

公共類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; 
} 

}

(我更改此代碼出頭時,我張貼他人,但STIL同樣的問題..)

而且我用較舊的Android設備android.v4.Fragment等支持..

另外我相信int天不是零但是例如八。

+0

您可以發佈您BootstrapPagerAdapter? – adboco

+0

完成! setcurrentitem是一次在onccreateview事物中工作.. –

回答

0

不知怎的,這是因爲,如果忘了我兩次在我的抽屜式導航所以這是修復固定fragmentje笏沒有設置..

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;