Android Fragment的使用 三 Fragment与Activity交互
2017-04-16 13:45
507 查看
我这次是在Fragment类里定义公共接口让Activity复写,然后Fragment通过getActivity来调用此接口,这样一来就可以达成activity自己自定义管理Fragment,减少了Fragment与Activity的耦合,而且使得activity统一管理Fragment
使用例子如下:
下次讲运行时配置改变,Fragment的布局如何也能够跟着改变,为了解决屏幕大小问题和横竖屏问题。
再见
使用例子如下:
public class MainActivity extends AppCompatActivity implements FragmentOne.FOneBtnClickListener,FragmentTwo.FTwoBtnClickListener{ private FragmentOne mFOne; private FragmentTwo mFTwo; private FragmentThree mFThree; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFOne = new FragmentOne(); FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.add(R.id.id_content,mFOne,"ONE"); tx.commit(); } @Override public void onFOneBtnClick() { if(mFTwo == null){ mFTwo = new FragmentTwo(); mFTwo.setfTwoBtnClickListener(this); } FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.replace(R.id.id_content,mFTwo,"TWO"); tx.addToBackStack(null); tx.commit(); } @Override public void onFTwoBtnClick() { if(mFThree == null){ mFThree = new FragmentThree(); } FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.hide(mFTwo); tx.add(R.id.id_content,mFThree,"THREE"); tx.addToBackStack(null); tx.commit(); } } public class FragmentOne extends Fragment implements View.OnClickListener { private Button button; public interface FOneBtnClickListener{ void onFOneBtnClick(); } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_one,container,false); button = (Button) view.findViewById(R.id.id_fragment_one_btn); button.setOnClickListener(this); return view; } @Override public void onClick(View v){ if(getActivity() instanceof FOneBtnClickListener){ ((FOneBtnClickListener)getActivity()).onFOneBtnClick(); } } } public class FragmentTwo extends Fragment implements View.OnClickListener { private Button button; private FTwoBtnClickListener fTwoBtnClickListener; public interface FTwoBtnClickListener{ void onFTwoBtnClick(); } public void setfTwoBtnClickListener(FTwoBtnClickListener fTwoBtnClickListener){ this.fTwoBtnClickListener = fTwoBtnClickListener; } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_two,container,false); button = (Button) view.findViewById(R.id.id_fragment_two_btn); button.setOnClickListener(this); return view; } @Override public void onClick(View v){ if(fTwoBtnClickListener != null){ fTwoBtnClickListener.onFTwoBtnClick(); } } } public class FragmentThree extends Fragment implements View.OnClickListener { private Button button; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_three,container,false); button = (Button) view.findViewById(R.id.id_fragment_three_btn); button.setOnClickListener(this); return view; } @Override public void onClick(View v){ Toast.makeText(getActivity(),"three",Toast.LENGTH_SHORT).show(); } } activity_main <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="android.com.myapplication.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/id_content"></FrameLayout> </RelativeLayout> fragment_one <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="one" android:id="@+id/id_fragment_one_btn" android:layout_gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> fragment_two <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="two" android:id="@+id/id_fragment_two_btn" android:layout_gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> fragment_three <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="three" android:id="@+id/id_fragment_three_btn" android:layout_gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
下次讲运行时配置改变,Fragment的布局如何也能够跟着改变,为了解决屏幕大小问题和横竖屏问题。
再见
相关文章推荐
- Android:Activity和Fragment交互、Fragment控件UI升级相关内容
- [转]Android笔记:Fragment与Activity之间的交互
- android基础内容介绍(XXXDrawable、Android动画、Android资源使用、Fragment与Activity)
- Android fragment和activity之间的数据传递交互
- Android Fragment和Activity的交互介绍
- Android基础——Fragment与Activity交互
- 深入分析:Fragment与Activity交互的几种方式(二,使用Bundle)
- Android应用开发:LoaderManager在Activity/Fragment中的使用分析
- 深入分析:Fragment与Activity交互的几种方式(三,使用接口)
- Android FragmentActivity+viewpager的使用
- Android基础——Fragment与Activity交互
- 深入分析:Fragment与Activity交互的几种方式(一,使用Handler)
- android中使用fragment时出现如下错误 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fra
- android使用viewPager和Fragment实现滑动切换activity!
- Android开发:使用Fragment改造TabActivity
- (转自红黑联盟)Android开发:使用Fragment改造TabActivity
- Android开发:使用Fragment改造TabActivity
- android service的使用(三)- 与activity的交互
- Android Activity中嵌套多个Fragment的使用
- Android Fragment和Activity的交互介绍