我的應用程式中有兩個選項卡片段,而在我的父活動中,我有一個 DatePicker。我想根據 DatePicker 的年份和月份加載資料。我正在使用介面將年份和月份傳遞給兩個片段。最初我可以將資料發送到兩個片段。但是在我使用 DatePicker 更改值并嘗試傳遞年月之后,只有第二個選項卡片段獲取值。第一個甚至沒有命中。如果我從第二個片段中洗掉介面,那么第一個片段就可以了。
BranchProfitabilityActivity
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);
}
}
}
BranchProfitabilityFragmentLife
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);
}
}
BranchProfitabilityFragmentGeneral
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);
}
}
uj5u.com熱心網友回復:
在您的情況下,Activity您只有一個sendData實體。兩個片段都在呼叫注冊方法setSendData(this),所以Activity只保存后一個。如果您想要兩者/所有這些都應該存盤在某個集合中,例如HashSet或ArrayList
private SendData sendDataSet = new HashSet<SendData>();
public void setSendData(SendData sendData) {
sendDataSet.add(sendData);
LoadDataFromYearMonth(selectedYear, selectedMonth);
}
private void LoadDataFromYearMonth(int year, int month) {
Iterator<SendData > it = sendDataSet.iterator();
while (it.hasNext()) {
SendData sendData = it.next()
sendData.SendYearMonth(year, month);
}
}
public void removeSendData(SendData sendData) {
sendDataSet.remove(sendData);
}
不要忘記在被殺死removeSendData(this)時取消注冊你的介面呼叫,例如在FragmentonDestroyView
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/521285.html
標籤:安卓安卓片段
上一篇:在特定片段的導航抽屜中的漢堡包圖示和后退箭頭圖示之間切換不起作用
下一篇:隱藏系統欄并在片段中全屏顯示
