gpt4 book ai didi

android - 获取当前 fragment 变量值

转载 作者:行者123 更新时间:2023-11-30 05:05:11 26 4
gpt4 key购买 nike

我正在使用 FragmentViewPagerAdapter 创建日历 Activity 。每个 fragment 代表一天,我的主要 Activity 上有一个开关,允许用户过滤每个 fragment 的结果。 我正在使用 ViewModel 请求数据,所以当切换被选中时,我需要向我的服务器发出新请求以获取每个 fragment 的新数据,我的问题是当我发出请求时,发送的日期是下一个 fragment 的日期,不是现在的。

我已经尝试将屏幕页面限制为 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 它将加载星期二的数据。

最佳答案

您可以使用 supportFragmentManager 获取当前 fragment 。

下面的代码会帮助你。

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

希望对您有所帮助。

关于android - 获取当前 fragment 变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54688744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com