如何 return 从 BottomSheetDialogFragment 到 Parent Fragment 的数据
How to return data from BottomSheetDialogFragment to Parent Fragment
我在 bottomsheet 中填充 RecyclerView 并将 BottomSheet 显示为 BottomSheetDialogFragment。单击 RecyclerView 中的项目时,我想使用该项目的详细信息自动填写表单。
如何将包含来自 BottomSheetDialogFragment 的数据的 class(model) 对象传递到我在其上实例化 BottomSheetDialogFragment 的片段。这是我的代码
片段我想自动填充字段
public class FormFragment extends Fragment implements onItemSelected {public class fetchSimilarContacts extends AsyncTask<Void, Void, String> {
String result, res, search;
fetchSimilarContacts(String sear){
search = sear;
}
@Override
protected void onPostExecute(String aVoid) {
try {
if (res.equals("200")){
try {
// bring up bottom sheet
bottomSheetDialogFragment = new formBottomSheet();
Bundle bun = new Bundle();
bun.putParcelableArrayList("data", contItems);
bottomSheetDialogFragment.setArguments(bun);
bottomSheetDialogFragment.show(getActivity().getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
catch (Exception ex){
ex.printStackTrace();
}
}
else{
Toast.makeText(getActivity().getApplicationContext(), "Could not connect to server", Toast.LENGTH_LONG).show();
}
}
}
}}
我获取数据并使用上面的 asynctask 函数填充 RecyclerView。这是 formBottomSheet class
public class formBottomSheet extends BottomSheetDialogFragment{
similarContactAdapter mAdapter;
RecyclerView rec;
ArrayList<hash> contItems;
View v;
onItemSelected itemSelected;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//contItems = new ArrayList<hash>();
if(getArguments() != null){
contItems = getArguments().getParcelableArrayList("data");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/*return super.onCreateView(inflater, container, savedInstanceState);*/
v = inflater.inflate(R.layout.form_bottomsheet_recyclerview, container, false);
rec = (RecyclerView) v.findViewById(R.id.recycler);
itemSelected = (onItemSelected) getParentFragment();
//contItems = new ArrayList<hash>();
mAdapter = new similarContactAdapter(contItems, getActivity().getApplicationContext(), new similarContactAdapter.onItemClickListener() {
@Override
public void onItemClick(hash hsh) {
//after clicking on an item in the RecyclerView, i get the item's data here, how to i pass the data from here to the Form Fragment where i instantiated the bottom sheet fragment dialog?
Toast.makeText(getActivity().getApplicationContext(), "", Toast.LENGTH_LONG).show();
}
});
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
rec.setLayoutManager(mLayoutManager);
rec.setItemAnimator(new DefaultItemAnimator());
rec.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
return v;
}
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
}
public interface onItemSelected {
void SendHashItem(hash hsh);
} }
单击 RecyclerView 中的某个项目后,我会在此处获取该项目的数据,如何将数据从此处传递到我实例化 BottomSheetFragmentDialog 的表单片段?
这是我的 activity
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
// Hide the status bar.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_dashboard);
//initialize
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewp);
setupViewPager(viewPager);
title = findViewById(R.id.title);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getApplicationContext(), R.color.transparent));
setupTabIcons();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
View tabView = tab.getCustomView();
// get inflated children Views the icon and the label by their id
TextView tab_label = (TextView) tabView.findViewById(R.id.tab);
// change the label color, by getting the color resource value
tab_label.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
// change the image Resource
// i defined all icons in an array ordered in order of tabs appearances
// call tab.getPosition() to get active tab index.
tab_label.setCompoundDrawablesWithIntrinsicBounds(0, tabIconsActive[tab.getPosition()], 0, 0);
//"#4CAF50");
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
View tabView = tab.getCustomView();
// get inflated children Views the icon and the label by their id
TextView tab_label = (TextView) tabView.findViewById(R.id.tab);
// change the label color, by getting the color resource value
tab_label.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
// change the image Resource
// i defined all icons in an array ordered in order of tabs appearances
// call tab.getPosition() to get active tab index.
tab_label.setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[tab.getPosition()], 0, 0);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.setScrollPosition(position, 0 , true);
tabLayout.setSelected(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setCurrentItem(0);
tabLayout.getTabAt(0).select();
setToolbarTitle();
}
catch(Exception e){
e.printStackTrace();
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new VideoFragment(), "Video");
adapter.addFragment(new ThreeSixtyFragment(), "360");
adapter.addFragment(new FormFragment(), "Form");
adapter.addFragment(new SettingsFragment(), "Settings");
viewPager.setAdapter(adapter);
}
private void setupTabIcons() {
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabThree.setText("Video");
tabThree.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_video_red, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabThree);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabTwo.setText("360");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_360_white, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabOne.setText("Form");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_form_white, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabOne);
TextView tabFive = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabFive.setText("Settings");
tabFive.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_settings_white, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFive);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
你的 FormFragment
和你的 formBottomSheet
都是存在于同一个 Activity 实例中的片段。
有时一个片段会有"parent"或"child"个片段,但这里不是这种情况,因为你的FormFragment
使用getActivity().getSupportFragmentManager()
显示底部sheet,而不是 getChildFragmentManager()
。这完全没问题,但要记住这一点。
这意味着 formBottomSheet
到 "talk to" FormFragment
的最佳方式是与 Activity 交谈,然后让 Activity将其传递给 FormFragment
.
formBottomSheet
-> Activity
-> FormFragment
与 Activity 交谈非常简单。您可以调用 getActivity()
并将其转换为任何您的 Activity 的 class 名称:
mAdapter = new similarContactAdapter(contItems, getActivity().getApplicationContext(), new similarContactAdapter.onItemClickListener() {
@Override
public void onItemClick(hash hsh) {
MyActivity activity = (MyActivity) getActivity();
activity.onHashItem(hsh);
}
});
在您的 Activity 中,您可以使用 FragmentManager 获取您的 FormFragment
,然后将消息发送给它。这依赖于 FormFragment
具有 id
(如果您在 XML 中设置了 android:id
属性,或者如果您使用 replace(R.id.some_id, ...)
那么您可以使用id) 或 tag
(如果您在 XML 中设置了 android:tag
属性,或者如果您使用了 replace(..., "TAG")
,那么您可以使用标签)。
public void onHashItem(hash hsh) {
FragmentManager fm = getSupportFragmentManager();
// use only one of the two following lines
FormFragment fragment = (FormFragment) fm.findFragmentById(R.id.some_id); // if using id
FormFragment fragment = (FormFragment) fm.findFragmentByTag("TAG"); // if using tag
fragment.onHashItem(hsh);
}
最后,您可以在 FormFragment
:
中创建 onHashItem()
方法
public void onHashItem(hash hsh) {
// do whatever you want
}
我在 bottomsheet 中填充 RecyclerView 并将 BottomSheet 显示为 BottomSheetDialogFragment。单击 RecyclerView 中的项目时,我想使用该项目的详细信息自动填写表单。
如何将包含来自 BottomSheetDialogFragment 的数据的 class(model) 对象传递到我在其上实例化 BottomSheetDialogFragment 的片段。这是我的代码
片段我想自动填充字段
public class FormFragment extends Fragment implements onItemSelected {public class fetchSimilarContacts extends AsyncTask<Void, Void, String> {
String result, res, search;
fetchSimilarContacts(String sear){
search = sear;
}
@Override
protected void onPostExecute(String aVoid) {
try {
if (res.equals("200")){
try {
// bring up bottom sheet
bottomSheetDialogFragment = new formBottomSheet();
Bundle bun = new Bundle();
bun.putParcelableArrayList("data", contItems);
bottomSheetDialogFragment.setArguments(bun);
bottomSheetDialogFragment.show(getActivity().getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
}
catch (Exception ex){
ex.printStackTrace();
}
}
else{
Toast.makeText(getActivity().getApplicationContext(), "Could not connect to server", Toast.LENGTH_LONG).show();
}
}
}
}}
我获取数据并使用上面的 asynctask 函数填充 RecyclerView。这是 formBottomSheet class
public class formBottomSheet extends BottomSheetDialogFragment{
similarContactAdapter mAdapter;
RecyclerView rec;
ArrayList<hash> contItems;
View v;
onItemSelected itemSelected;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//contItems = new ArrayList<hash>();
if(getArguments() != null){
contItems = getArguments().getParcelableArrayList("data");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
/*return super.onCreateView(inflater, container, savedInstanceState);*/
v = inflater.inflate(R.layout.form_bottomsheet_recyclerview, container, false);
rec = (RecyclerView) v.findViewById(R.id.recycler);
itemSelected = (onItemSelected) getParentFragment();
//contItems = new ArrayList<hash>();
mAdapter = new similarContactAdapter(contItems, getActivity().getApplicationContext(), new similarContactAdapter.onItemClickListener() {
@Override
public void onItemClick(hash hsh) {
//after clicking on an item in the RecyclerView, i get the item's data here, how to i pass the data from here to the Form Fragment where i instantiated the bottom sheet fragment dialog?
Toast.makeText(getActivity().getApplicationContext(), "", Toast.LENGTH_LONG).show();
}
});
final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
rec.setLayoutManager(mLayoutManager);
rec.setItemAnimator(new DefaultItemAnimator());
rec.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
return v;
}
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
}
public interface onItemSelected {
void SendHashItem(hash hsh);
} }
单击 RecyclerView 中的某个项目后,我会在此处获取该项目的数据,如何将数据从此处传递到我实例化 BottomSheetFragmentDialog 的表单片段?
这是我的 activity
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
// Hide the status bar.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_dashboard);
//initialize
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewp);
setupViewPager(viewPager);
title = findViewById(R.id.title);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getApplicationContext(), R.color.transparent));
setupTabIcons();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
View tabView = tab.getCustomView();
// get inflated children Views the icon and the label by their id
TextView tab_label = (TextView) tabView.findViewById(R.id.tab);
// change the label color, by getting the color resource value
tab_label.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
// change the image Resource
// i defined all icons in an array ordered in order of tabs appearances
// call tab.getPosition() to get active tab index.
tab_label.setCompoundDrawablesWithIntrinsicBounds(0, tabIconsActive[tab.getPosition()], 0, 0);
//"#4CAF50");
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
View tabView = tab.getCustomView();
// get inflated children Views the icon and the label by their id
TextView tab_label = (TextView) tabView.findViewById(R.id.tab);
// change the label color, by getting the color resource value
tab_label.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
// change the image Resource
// i defined all icons in an array ordered in order of tabs appearances
// call tab.getPosition() to get active tab index.
tab_label.setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[tab.getPosition()], 0, 0);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.setScrollPosition(position, 0 , true);
tabLayout.setSelected(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setCurrentItem(0);
tabLayout.getTabAt(0).select();
setToolbarTitle();
}
catch(Exception e){
e.printStackTrace();
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new VideoFragment(), "Video");
adapter.addFragment(new ThreeSixtyFragment(), "360");
adapter.addFragment(new FormFragment(), "Form");
adapter.addFragment(new SettingsFragment(), "Settings");
viewPager.setAdapter(adapter);
}
private void setupTabIcons() {
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabThree.setText("Video");
tabThree.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_video_red, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabThree);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabTwo.setText("360");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_360_white, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabOne.setText("Form");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_form_white, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabOne);
TextView tabFive = (TextView) LayoutInflater.from(this).inflate(R.layout.cust_tab, null);
tabFive.setText("Settings");
tabFive.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_settings_white, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFive);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
你的 FormFragment
和你的 formBottomSheet
都是存在于同一个 Activity 实例中的片段。
有时一个片段会有"parent"或"child"个片段,但这里不是这种情况,因为你的FormFragment
使用getActivity().getSupportFragmentManager()
显示底部sheet,而不是 getChildFragmentManager()
。这完全没问题,但要记住这一点。
这意味着 formBottomSheet
到 "talk to" FormFragment
的最佳方式是与 Activity 交谈,然后让 Activity将其传递给 FormFragment
.
formBottomSheet
-> Activity
-> FormFragment
与 Activity 交谈非常简单。您可以调用 getActivity()
并将其转换为任何您的 Activity 的 class 名称:
mAdapter = new similarContactAdapter(contItems, getActivity().getApplicationContext(), new similarContactAdapter.onItemClickListener() {
@Override
public void onItemClick(hash hsh) {
MyActivity activity = (MyActivity) getActivity();
activity.onHashItem(hsh);
}
});
在您的 Activity 中,您可以使用 FragmentManager 获取您的 FormFragment
,然后将消息发送给它。这依赖于 FormFragment
具有 id
(如果您在 XML 中设置了 android:id
属性,或者如果您使用 replace(R.id.some_id, ...)
那么您可以使用id) 或 tag
(如果您在 XML 中设置了 android:tag
属性,或者如果您使用了 replace(..., "TAG")
,那么您可以使用标签)。
public void onHashItem(hash hsh) {
FragmentManager fm = getSupportFragmentManager();
// use only one of the two following lines
FormFragment fragment = (FormFragment) fm.findFragmentById(R.id.some_id); // if using id
FormFragment fragment = (FormFragment) fm.findFragmentByTag("TAG"); // if using tag
fragment.onHashItem(hsh);
}
最后,您可以在 FormFragment
:
onHashItem()
方法
public void onHashItem(hash hsh) {
// do whatever you want
}