如何从DialogFragment中的Fragment获取数据?

时间:2017-12-08 16:24:14

标签: android android-fragments android-recyclerview android-dialogfragment

我有一个DialogFragment,它使用FragmentPagerAdapter来显示标签。每个选项卡都有一个使用RecyclerView的不同Fragment。我可以将单击的项目传递给Fragment,但是如何将Fragment中的数据传递给DialogFragment以将其传递给调用Activity?

TabDialog扩展了DialogFragment:

public class TabDialog extends DialogFragment {

public interface OnCompleteListener{
    void onCompleteListener(String test);
}

private OnCompleteListener mListener;

TabLayout tabLayout;
ViewPager viewPager;
FragmentTabPositive tabPositive = new FragmentTabPositive();
FragmentTabNegative tabNegative = new FragmentTabNegative();
TabAdapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    getDialog().setCanceledOnTouchOutside(true);
    View rootView = inflater.inflate(R.layout.dialog_layout, container, false);
    tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout1);
    viewPager = (ViewPager) rootView.findViewById(R.id.masterViewPager);
    adapter = new TabAdapter(getChildFragmentManager());
    adapter.addFragment("Positive", tabPositive);
    adapter.addFragment("Negative", tabNegative);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);

    return rootView;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        this.mListener = (OnCompleteListener)context;
    } catch(ClassCastException e){
        throw new ClassCastException(context.toString() +
                " must Implement OnCompleteListener");
    }

}

TabAdapter扩展了FragmentPagerAdapter

public class TabAdapter extends FragmentPagerAdapter {

List<Fragment> mFragmentCollection = new ArrayList<>();
List<String> mTitleCollection = new ArrayList<>();

public TabAdapter(FragmentManager fm){
    super(fm);
}

public void addFragment(String title, Fragment fragment){

    mTitleCollection.add(title);
    mFragmentCollection.add(fragment);
}

@Override
public CharSequence getPageTitle(int position){
    return mTitleCollection.get(position);
}

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

@Override
public Fragment getItem(int position) {
    return mFragmentCollection.get(position);
}

FragmentTabPos中的onCreateView

popupAdapter = new PopupAdapter(view.getContext(), list);
    recyclerView.setAdapter(popupAdapter);

    popupAdapter.setmListener(new PopupAdapter.OnPointClickedListener() {
        @Override
        public void OnItemClicked(String test) {
            //This works
            Toast.makeText(FragmentTabPositive.this.getContext(), test + " Inside FragTabPos", Toast.LENGTH_LONG).show();
        }
    });

2 个答案:

答案 0 :(得分:0)

为避免过于紧密地耦合碎片,我个人会选择使用新的架构组件传输数据。

您可以使用AndroidViewModel传递数据。

以下是Android开发者网站上的介绍:https://developer.android.com/topic/libraries/architecture/index.html

或者,您可以使用事件总线将数据从一个片段发布到其父片段。

一个这样的图书馆:https://github.com/greenrobot/EventBus

使用架构组件的好处在于它遵循Google的最新应用程序设计标准。

答案 1 :(得分:0)

已解决:必须在Fragment中实现我自己的侦听器并在DialogFragment中触发它。从那里,我使用OnCompleteListener将数据传递回调用Activity。