获取当前片段变量值

时间:2019-02-14 10:55:21

标签: android android-fragments fragmentstatepageradapter

我正在使用FragmentViewPagerAdapter创建日历活动。每个片段代表一天,我在我的主要活动上有一个Switch,它允许用户过滤每个片段的结果。  我正在使用ViewModel请求数据,因此在检查开关后,我需要向服务器发出新请求以获取每个片段的新数据,我的问题是,当我请求时,发送的日期是下一个片段的日期,不是当前的。

我已经尝试将屏幕页面限制为0,但是没有任何变化。

public class PoiPlanningFragment extends Fragment implements InitApplicationListener{

MutableLiveData<Boolean> filtered = new MutableLiveData<>();
private Context mContext;
DateTime startTime;
DateTime  endTime;
switcher = getActivity().findViewById(R.id.app_switcher);

public PoiPlanningFragment() {
    // Required empty public constructor
}


public static Fragment getInstance(int position, String date, String resourceId, String resourceType,boolean filter) {
    Bundle bundle = new Bundle();
    bundle.putInt("pos", position );
    bundle.putString("date", date);
    bundle.putString(ARG_RESOURCE_ID, resourceId);
    bundle.putString(ARG_RESOURCE_TYPE, resourceType);
    PoiPlanningFragment tabFragment = new PoiPlanningFragment();
    tabFragment.setArguments(bundle);
    return tabFragment;
}

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    DateTimeFormatter horaire = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
    startTime = horaire.parseDateTime(date.concat(" " + STARTHOUR));
    endTime = horaire.parseDateTime(date.concat(" " + ENDHOUR));
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_poi_planning, container, false);
}

 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

//Switch my meetings
    switcher.setOnCheckedChangeListener(new SwitchClickedListener(this.filtered));

     //Observes whether the user has a filter on own meetings or not
    filtered.observe(this, filtered -> {
        //here startTime always shows next tab startTime and endTime
        BookingRepository.getUserPlanning(planning,resourceId,Utility.toTimeStamp(startTime),Utility.toTimeStamp(endTime), filtered);
        loader.setVisibility(View.VISIBLE);
    });

}

}

private class SwitchClickedListener implements CompoundButton.OnCheckedChangeListener{
    MutableLiveData<Boolean> listener;

    SwitchClickedListener(MutableLiveData<Boolean> listener){
        this.listener = listener;
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        //Set filter to true
        this.listener.setValue(b);
    }
}


public class PoiPlanningViewPagerAdapter extends FragmentStatePagerAdapter {

private final static String TAG = PoiPlanningViewPagerAdapter.class.getName();
private List<Date> dateList;
private Context context;
private String poiId;
private String resouceType;

public PoiPlanningViewPagerAdapter(FragmentManager fm, List<Date> dates, String poidId,String resourceType, Context context) {
    super(fm);
    this.dateList = dates;
    this.context = context;
    this.poiId = poidId;
    this.resouceType = resourceType;
}

@Override
public Fragment getItem(int position) {
    Date datetime = dateList.get(position);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String date = df.format(datetime);
    PoiPlanningFragment planningFragment = (PoiPlanningFragment) PoiPlanningFragment.getInstance(position, date, poiId,resouceType,false);
    return planningFragment;
}

@Override
public int getCount() {
    return dateList.size();
}

@Override
public CharSequence getPageTitle(int position) {
    Date datetime = dateList.get(position);
    DateFormat df = new SimpleDateFormat(context.getString(R.string.date_format));
    String date = df.format(datetime);
    return date;
}


}

在前面的代码中,如果我检查开关并且我在02月11日星期一,它将加载星期二的数据。

1 个答案:

答案 0 :(得分:0)

您可以使用supportFragmentManager获取当前片段。

下面的代码将为您提供帮助。

if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
        val frag = getSupportFragmentManager().findFragmentById(R.id.container);
        if (frag is HomeFragment) {
           Fragment currentFragment = (HomeFragment) frag;
            //do your code
            return
        }
    } 

我希望这会对您有所帮助。