FragmentPagerAdapterを使用してタブを表示しているDialogFragmentがあります。各タブには、RecyclerViewを使用する別のフラグメントがあります。どのアイテムがFragmentに渡されたのかを知ることができますが、FragmentからDialogFragmentにデータを渡して呼び出し側のアクティビティに渡すにはどうしたらいいですか?DialogFragmentの中にあるフラグメントからデータを取得するには?
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がFragmentTabPos内onCreateViewの内部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);
}
を拡張
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();
}
});