我有一个Activity和两个Fragment用于包含一个tablayout
和一个viewpager
。现在,我可以通过实现Google的指南回调接口从Fragment向Activity进行通信。但是如何实现另一种方式从Activity到Fragment的通信呢?如果Activity中发生了某些事情(外部事件),我想要更新Fragment。我成功获取了Frag1 fragment。
MyFragmentPagerAdapter a = (MyFragmentPagerAdapter) viewPager.getAdapter();
Frag1 frag = (Frag1) a.getItem(0);
但是当我在frag上调用公共方法时,会收到一个IllegalStateException:Fragment not attached to Activity,这可能是因为getItem(0)返回Frag1的新实例,而此时尚未附加...是否有人可以提供整个Viewpager -> Activity到Fragment通信的清晰解决方案?以下是一些代码:
在Activity中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
if (tabLayout != null) {
tabLayout.setupWithViewPager(viewPager);
}
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"/>
</LinearLayout>
MyFragmentPagerAdapter:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[] { "tab1", "tab2" };
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Frag1.newInstance(position + 1);
case 1:
return Frag2.newInstance(position + 1);
default:
return null;
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
片段1:
public class Frag1 extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private onFrag1InteractionListener mListener;
public Frag1() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Frag1.
*/
public static Frag1 newInstance(int page) {
Frag1 fragment = new Frag1();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPage = getArguments().getInt(ARG_PAGE);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof onFrag1InteractionListener) {
mListener = (onFrag1InteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement onFrag1InteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface onFrag1InteractionListener {
// TODO: Update argument type and name
void onFrag1Interaction(Action action);
}