我在主活动中有一个浮动操作按钮(FAB),下面有4个片段标签。Android:在主活动提示中输入数据后刷新当前显示的ViewPager片段标签
当单击FAB时,根据显示哪个选项卡,我有单独的对话框提示输入数据。
现在对于第一个选项卡,我可以在输入数据后立即刷新片段的内容,然后使用Interface
在对话框提示中单击确定。但是,对于制表符2-4,在每个选项卡2-4的相应对话框提示中单击确定后,我无法一次刷新数据。我必须点击第一个选项卡,然后返回到之前显示的选项卡才能刷新视图。
我想要做的是:
从第二/第三/第四个选项卡,输入数据,然后点击输入对话框提示输入相应的标签后,当前显示的选项卡,应立即更新里面的意见根据我在对话框中输入的内容并保存到数据库中。
请注意,在单击提示中的确定按钮后,我将输入保存到数据库中。
我该怎么做?先谢谢您,我感谢您的帮助!:)
这里是我的代码(我使用的接口):
public class MyActivity extends AppCompatActivity {
private PopupListener popupListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Your dialog code.
setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
popupListener.onDialogClick("first_tab"); //"second_tab" for second tab, and so on...
}
})
}
public void setPopupListener(PopupListener popupListener) {
this.popupListener = popupListener;
}
public interface PopupListener {
void onDialogClick(String value);
}
}
码在第2标签:
public class MyFragment extends Fragment {
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((MyActivity) getActivity()).setPopupListener(new MyActivity.PopupListener() {
@Override
public void onDialogClick(String value) {
//After clicking dialog ok button in Activity
setUserVisibleHint(true, view); // get value from the database here.
}
});
}
}
守则更新cu rrently显示的片段(以下样品为第二标签):所有4个片段的
@Override
public void setUserVisibleHint(boolean isVisibleToUser, final View view) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//get data from database and refresh view.
viewFunds(view) //method is for updating 2nd tab
}
}
内部的OnCreate():
public void onCreate(@Nullable Bundle savedInstanceState) {
setUserVisibleHint(false);
}
第一个标签代码:(加按注释要求)
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((Main2Activity) getActivity()).setPopupListener(new Main2Activity.PopupListener() {
@Override
public void onDialogClick(String value) {
//After clicking dialog ok button in Activity
// you will get value here.
viewBudget(view); //method to updated views inside first tab
}
});
}
FAB的OnClick听众:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButton_main2);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = tabLayout.getSelectedTabPosition();
switch (position) {
case 0:
// first tab is selected
//Toast.makeText(context,"0",Toast.LENGTH_LONG).show();
break;
case 1:
// second tab is selected
//Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_LONG).show();
break;
case 2:
// third tab is selected
//Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_LONG).show();
UpdateableFragment.showDialog(); //ERROR: non static method onDialogClick cannot be referenced from static context
break;
case 3:
// third tab is selected
//Toast.makeText(getApplicationContext(),"3",Toast.LENGTH_LONG).show();
break;
}
我已经使用问题中提到的'Interface'。你提到'广播',你能否通过添加答案来提供更多细节?谢谢。 –
添加第一个选项卡的代码(请参阅上面的更新问题)。谢谢:) –
第一个片段,onViewCreated。 –