如何从 android 中的 BottomSheetDialogFragment 调用片段
How to call fragment from the BottomSheetDialogFragment in android
我发现很难从 BottomSheetDialog 调用 MainFragment 中的 InsertSlider。
任何帮助和感谢
在底页中:
public class AdminBottomSheetMainSave extends BottomSheetDialogFragment
{
// One Method I don.t Know How To Work
//((MainFragment)getContext()).InsertSliders(new ImageSlider(0,Name,Image,Price,Description));
// Not Work Too
MainFragment fragment = new MainFragment();
fragment.InsertSliders(new ImageSlider(0,Name,Image,Price,Description));
}
在片段中:
public class MainFragment extends Fragment
{
public void InsertSliders(ImageSlider imageSlider)
{
imageSliderViewModel.insert(imageSlider);
Toast.makeText(getActivity(), "Done Insert"+imageSlider, Toast.LENGTH_SHORT).show();
}
联系方式是什么我如何到达InsertSliders
你应该使用接口在两个片段之间进行通信,你需要一个 activity 来实现接口。
在 bottomsheetdialogfragment 中定义界面:
public class AdminBottomSheetMainSave extends BottomSheetDialogFragment
{
public Callback mCallback;
public interface Callback{
void insertSlider(ImageSlider slider);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mCallback = (Callback) activity;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//...
mCallback.insertSlider(new ImageSlider(0,Name,Image,Price,Description));
}
}
然后在您的 activity 中,在我们的例子中,MainActivity 实现回调接口并覆盖 inserSlider 方法:
public class MainActivity extends AppCompatActivity implements Callback{
MainFragment mainFragment;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...
if(savedInstanceState == null){
mainFragment = MainFragment.newInstance(new Bundle()); // use real bundle here
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_holder, mainFragment, "FragMain").commit();
}
}
// Interface method
@Override
public void insertSlider(ImageSlider slider){
if(mainFragment != null){
mainFragment.insertSlider(slider);
}
}
现在在您的 MainFragment 中:
public class MainFragment extends Fragment{
public static MainFragment newInstance(Bundle args){
MainFragment fragment = new MainFragment();
fragment.setArguments(args);
return fragment;
}
//... Class overrides here onCreateView etc..
// define insertSlider method
public void insertSlider(ImageSlider slider){
imageSliderViewModel.insert(slider);
Toast.makeText(getActivity(), "Done Insert"+slider, Toast.LENGTH_SHORT).show();
}
我发现很难从 BottomSheetDialog 调用 MainFragment 中的 InsertSlider。 任何帮助和感谢
在底页中:
public class AdminBottomSheetMainSave extends BottomSheetDialogFragment
{
// One Method I don.t Know How To Work
//((MainFragment)getContext()).InsertSliders(new ImageSlider(0,Name,Image,Price,Description));
// Not Work Too
MainFragment fragment = new MainFragment();
fragment.InsertSliders(new ImageSlider(0,Name,Image,Price,Description));
}
在片段中:
public class MainFragment extends Fragment
{
public void InsertSliders(ImageSlider imageSlider)
{
imageSliderViewModel.insert(imageSlider);
Toast.makeText(getActivity(), "Done Insert"+imageSlider, Toast.LENGTH_SHORT).show();
}
联系方式是什么我如何到达InsertSliders
你应该使用接口在两个片段之间进行通信,你需要一个 activity 来实现接口。
在 bottomsheetdialogfragment 中定义界面:
public class AdminBottomSheetMainSave extends BottomSheetDialogFragment
{
public Callback mCallback;
public interface Callback{
void insertSlider(ImageSlider slider);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mCallback = (Callback) activity;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//...
mCallback.insertSlider(new ImageSlider(0,Name,Image,Price,Description));
}
}
然后在您的 activity 中,在我们的例子中,MainActivity 实现回调接口并覆盖 inserSlider 方法:
public class MainActivity extends AppCompatActivity implements Callback{
MainFragment mainFragment;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...
if(savedInstanceState == null){
mainFragment = MainFragment.newInstance(new Bundle()); // use real bundle here
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_holder, mainFragment, "FragMain").commit();
}
}
// Interface method
@Override
public void insertSlider(ImageSlider slider){
if(mainFragment != null){
mainFragment.insertSlider(slider);
}
}
现在在您的 MainFragment 中:
public class MainFragment extends Fragment{
public static MainFragment newInstance(Bundle args){
MainFragment fragment = new MainFragment();
fragment.setArguments(args);
return fragment;
}
//... Class overrides here onCreateView etc..
// define insertSlider method
public void insertSlider(ImageSlider slider){
imageSliderViewModel.insert(slider);
Toast.makeText(getActivity(), "Done Insert"+slider, Toast.LENGTH_SHORT).show();
}