我在我的应用中有两个选项卡片段,在我的父Activity中有一个DatePicker。我想根据DatePicker的年份和月份加载数据。我使用接口将年份和月份传递给这两个片段。最初我可以将数据发送给这两个片段。但在我使用DatePicker更改值并尝试传递年份和月份后,只有第二个选项卡片段获得值。第一个甚至没有命中。如果我从第二个片段中删除接口,那么第一个片段就可以工作。
分支机构盈利能力活动
public class BranchProfitabilityActivity extends AppCompatActivity {
private SendData sendData;
private Calendar calendar;
private int selectedMonth, selectedYear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_branch_profitability);
calendar = Calendar.getInstance(TimeZone.getDefault());
selectedMonth = calendar.get(Calendar.MONTH);
selectedYear = calendar.get(Calendar.YEAR);
String[] tabTiles = {"LIFE", "GENERAL"};
ViewPager2 viewPager = findViewById(R.id.view_pager);
TabLayout tabLayout = findViewById(R.id.profit_tabs);
ProfitabilityViewPagerAdapter myAdapter = new ProfitabilityViewPagerAdapter(this);
// add Fragments in your ViewPagerFragmentAdapter class
myAdapter.addFragment(new BranchProfitabilityFragmentLife());
myAdapter.addFragment(new BranchProfitabilityFragmentGeneral());
// set Orientation in your ViewPager2
viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
viewPager.setAdapter(myAdapter);
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> tab.setText(tabTiles[position])).attach();
}
public interface SendData {
void SendYearMonth(int iYear, int iMonth);
}
public void setSendData(SendData sendData) {
this.sendData = sendData;
LoadDataFromYearMonth(selectedYear, selectedMonth);
}
private void LoadDataFromYearMonth(int year, int month) {
if (sendData != null) {
sendData.SendYearMonth(year, month);
}
}
}
分支机构盈利能力碎片寿命
public class BranchProfitabilityFragmentLife extends Fragment implements BranchProfitabilityActivity.SendData {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View xView = inflater.inflate(R.layout.fragment_branch_profitability_life, container, false);
if (getActivity() instanceof BranchProfitabilityActivity) {
((BranchProfitabilityActivity) getActivity()).setSendData(this);
}
return xView;
}
@Override
public void SendYearMonth(int iYear, int iMonth) {
RequestData(iYear, iMonth);
}
}
分支机构盈利能力细分常规
public class BranchProfitabilityFragmentGeneral extends Fragment implements BranchProfitabilityActivity.SendData {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View xView = inflater.inflate(R.layout.fragment_branch_profitability_general, container, false);
if (getActivity() instanceof BranchProfitabilityActivity) {
((BranchProfitabilityActivity) getActivity()).setSendData(this);
}
return xView;
}
@Override
public void SendYearMonth(int iYear, int iMonth) {
RequestData(iYear, iMonth);
}
}
1条答案
按热度按时间vom3gejh1#
在您
Activity
中,您只有一个sendData
示例这两个片段都调用setSendData(this)
注册方法,因此Activity
将只保存后一个如果您需要这两个/所有这些都应该存储在某个集合中,例如HashSet
或ArrayList
当
Fragment
被取消时,不要忘记用removeSendData(this)
调用取消注册您的接口,例如在onDestroyView
中