hyp*_*kcb 5 java android android-fragments
在尝试从片段中获取值时,我遇到了一些问题。我的片段 A 将打开片段 B,当单击片段 B 中的按钮时,我想获取一个值并传回片段 A 以进行显示。但是,我不知道该怎么做。
片段A:
@Click(R.id.buttonAttach)
void buttonAttachClicked(View v){
SupportAttachFileFragment fragment = new SupportAttachFileFragment_();
fragment.show(getFragmentManager(), null);
// get value here and display
textViewLogCounter.setText();
}
}
Run Code Online (Sandbox Code Playgroud)
片段B:
当这个按钮 onClicked 时,我想将值传递回片段 A 并显示。
@Click(R.id.buttonAttach)
void buttonAttachClicked(View v){
System.out.println("TOTAL " + selectedRows.size());
dismiss();
}
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做。我在考虑 Bundle 但 Bundle 是将参数传递给新活动。在这种情况下,我的片段已经打开,但我想传回一些值。
有任何想法吗?谢谢!
您可以使用共享ViewModel
. 在活动范围内创建 ViewModel,它将可用于活动及其所有片段。
MyViewModel sharedViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel::class.java)
Run Code Online (Sandbox Code Playgroud)
然后观察你的欲望数据sharedViewModel
。如果您更改sharedViewModel
任何片段或活动中的数据,则所有片段和活动的数据都会更改。请参阅下图或阅读文档。
所有片段到片段的通信都是通过共享ViewModel或关联的 Activity 完成的。两个片段不应该直接通信。
片段间通信的不同方式:
在片段之间进行通信的推荐方法是创建共享 ViewModel 对象。两个片段都可以通过其包含的 Activity 访问 ViewModel。Fragment 可以更新 ViewModel 中的数据,如果使用LiveData公开数据,只要它从 ViewModel 观察 LiveData,新状态就会被推送到其他片段。要了解如何实现这种通信,请阅读ViewModel 指南中的“在片段之间共享数据”部分。
您可以在 Fragment 类中定义一个接口并在 Activity 中实现它。Fragment 在其 onAttach() 生命周期方法期间捕获接口实现,然后可以调用接口方法以便与 Activity 进行通信。
公共类 HeadlinesFragment 扩展 ListFragment { OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
Run Code Online (Sandbox Code Playgroud)
}
现在,片段可以通过使用 OnHeadlineSelectedListener 接口的 mCallback 实例调用 onArticleSelected() 方法(或接口中的其他方法)来向活动传递消息。
例如,当用户单击列表项时,将调用片段中的以下方法。片段使用回调接口将事件传递给父活动。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
Run Code Online (Sandbox Code Playgroud)
实现接口
为了从片段接收事件回调,承载片段的活动必须实现片段类中定义的接口。
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
Run Code Online (Sandbox Code Playgroud)
向片段传递消息
宿主 Activity 可以通过使用 findFragmentById() 捕获 Fragment 实例来向 Fragment 传递消息,然后直接调用 Fragment 的公共方法。
例如,假设上面显示的活动可能包含另一个片段,用于显示上面回调方法中返回的数据指定的项目。在这种情况下,活动可以将回调方法中收到的信息传递给将显示该项目的另一个片段:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4900 次 |
最近记录: |