接口回调在fragment中的使用
来源:互联网 发布:java oop编程题 编辑:程序博客网 时间:2024/06/15 02:52
我们知道在两个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 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); }}
0 0
- 接口回调在fragment中的使用
- Fragment接口回调 三 使用接口
- Fragment接口回调 二 使用Bundle
- Handler在Fragment中的使用
- Fragment在Android中的使用
- GridView在Fragment中的使用
- okhttp3在Fragment中的使用
- 浅谈接口回调以及在Android中的使用
- acitvity与Fragment之间使用接口进行数据回调
- 关于fragment在layout中的使用
- 在Viewpager中的Fragment使用ContextMenu问题
- 在Viewpager中的Fragment使用ContextMenu问题
- Android BroadcastReceiver在Fragment中的使用
- fragment在app中的使用总结1
- Web Fragment在项目中的使用
- EventBus在Activity、Fragment、Service中的使用
- Fragment的接口回调
- Fragment学习之使用接口回调的方式实现Fragment与Activity通信
- Android 6.0 权限管理(permission)
- 第六届_楼房间距
- 安装pip有感
- 使用Ida动态调试Android 中的so文件的方式以及技巧
- 面向对象程序设计-复习C++
- 接口回调在fragment中的使用
- MFC 对话框伸缩 初始收缩
- 我的安卓回顾之旅:安卓自定义view 的练习
- 2-2 Tutorials with python -> Work Queues
- springMVC读取请求参数
- sql server 触发器
- Exynos4412启动过程分析
- 1097. Deduplication on a Linked List (25)
- C++中虚函数功能的实现机制