我的活动中有一个视图寻呼机。这个寻呼机装载两个片段(Fragment1和Fragment2)。我的活动有一个按钮从服务器获取数据,作为我的pojo类的列表。Fragment1和Fragment2包含recyclerView。
我的问题是,当在我的活动中获取信息时,如何刷新Fragment1的recyclerView适配器(来自我的活动)?
我在我的活动中创建了一个接口:
public interface IloadCallBack {
void onLoadAdapter(List<Suser> userList);
}我为此创造了一个策划人:
public void setIloadCallBack(IloadCallBack iloadCallBack) {
this.iloadCallBack = iloadCallBack;
}在此基础上:
iloadCallBack.onLoadAdapter(susers);现在,我已经在我的片段中引用了一些活动,但我认为这是错误的!是?我能做什么?
发布于 2017-12-25 12:32:53
当在活动中获取信息时,如何从活动中刷新片段1中的recyclerView适配器?
您不需要回调机制将数据传递给活动中托管的片段。
只需在片段refreshList中创建一个方法
// in fragment
public void refreshList(List<Suser> userList){
this.userList.clear();// empty list
this.userList.addAll(userList);
notifyDataSetChanged();
}保持对片段实例的全局引用,并从接收响应的位置调用refreshList。
public class YourActivity...{
private Fragment fragmentInstance;
void someMethodReceivedNewList(){
// where you receive new list in activity
if(fragmentinstance!=null)
fragmentinstance.refreshList(userList);
}
void someMethodToLoadFragment(){
fragmentInstance = new YourFragment1();
...
}
}发布于 2017-12-25 12:29:35
从活动到片段的交流:
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();
}
}
}从片段到活动的交流:
public class HeadlinesFragment extends 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");
}
}
...
}这两个都是从https://developer.android.com/training/basics/fragments/communicating.html取来的
请随便看看那里,他们解释得很好。
发布于 2017-12-25 12:26:14
如果您希望在其他地方发生任何特定事件时执行某些操作,比如在活动中发生事件时希望在片段中执行任何方法,或者相反,我将建议您使用EventBus。
https://github.com/greenrobot/EventBus
这是简单而直接的解决方案。
https://stackoverflow.com/questions/47968500
复制相似问题