android-fragments 通过接口将数据从父活动传递到选项卡片段

nkoocmlb  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(140)

我在我的应用中有两个选项卡片段,在我的父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);
    }

}
vom3gejh

vom3gejh1#

在您Activity中,您只有一个sendData示例这两个片段都调用setSendData(this)注册方法,因此Activity将只保存后一个如果您需要这两个/所有这些都应该存储在某个集合中,例如HashSetArrayList

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

Fragment被取消时,不要忘记用removeSendData(this)调用取消注册您的接口,例如在onDestroyView

相关问题