介面回撥在fragment中的使用
阿新 • • 發佈:2019-02-10
我們知道在兩個fragment之間傳遞資料要通過activity,如果兩個fragment之間發生了資料的傳遞,如果這時我們的螢幕發生翻轉activity將會啟動onCreate()方法,這時fragment中的狀態和資料將會被重置而得不到儲存,那麼如果我們想要儲存fragment的資料和狀態,就需要用到介面回撥的設計思想。
我們寫一個例子一個activity中有兩個fragment,左邊的fragment有兩個按鈕,右邊的fragment有一個textview,當左邊的按鈕被點選,右邊的text的內容相應發生改變,左邊的如果點選music按鈕,右邊則顯示music,左邊如果點選news按鈕,右邊則顯示news。
xml如下:
<?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" android:orientation="horizontal" tools:context="com.wusen.myfragmentdemo.MainActivity"> <fragment android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:id="@+id/fragment_menu" android:name="com.wusen.myfragmentdemo.MenuFragment"></fragment> <fragment android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:id="@+id/fragment_content" android:name="com.wusen.myfragmentdemo.ContentFragment"></fragment> <p></LinearLayout></p><p> </p>
左邊的fragment的XML
右邊的xml的程式碼:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_news" android:text="news"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_music" android:text="music"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_text"
android:hint="this is a textView"/>
</LinearLayout>
因為是左邊的menufragment中的資料要傳給右邊的contentfragment,我們在menufragment的程式碼中定義一個介面,然後讓MainActivity實現這個介面。在fragment的生命週期中,有一個onAttach(Activity activity)方法,這個方法中,我們可以得到宿主Activity的例項也就是MainActivity,因為MainActivity是這個介面的實現類,所以Mainactivity也是這個介面的物件,這樣我們可以在menufragment中拿到這個介面的例項,因為MainActivity中可以通過new 的方式直接得到fragment的例項,從而操作contentfragment進行引數的修改,我們在menufragment中拿到MainActivity的例項,就可以對contentfragment進行操作了。
MenuFragment程式碼如下:
public class MenuFragment extends Fragment implements View.OnClickListener {
private Button buttonOfNews;
private Button buttonOfMusic;
private onChange change;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
change = (onChange) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_fragment,container,false);
buttonOfNews = (Button) view.findViewById(R.id.btn_news);
buttonOfMusic = (Button) view.findViewById(R.id.btn_music);
buttonOfMusic.setOnClickListener(this);
buttonOfNews.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
//Toast.makeText(getActivity(),"111111111",Toast.LENGTH_SHORT).show();
switch (v.getId()){
case R.id.btn_news:
change.onChangeValue("news");
break;
case R.id.btn_music:
change.onChangeValue("music");
break;
}
}
public static interface onChange{
public void onChangeValue(String value);
}
}
<span style="font-size:24px;">ContentFragment程式碼如下:</span>
public class ContentFragment extends Fragment {
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.content_fragment,container,false);
textView = (TextView) view.findViewById(R.id.content_text);
return view;
}
public void changeTextView(String value)
{
textView.setText(value);
}
}
<span style="font-size:24px;">MainActivity程式碼如下:</span>
public class MainActivity extends AppCompatActivity implements MenuFragment.onChange{
private MenuFragment menuFragment;
private ContentFragment contentFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentFragment = (ContentFragment) getFragmentManager().
findFragmentById(R.id.fragment_content);
menuFragment = (MenuFragment) getFragmentManager()
.findFragmentById(R.id.fragment_menu);
}
@Override
public void onChangeValue(String value) {
contentFragment.changeTextView(value);
}
}