日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

fragment的基本使用

發(fā)布時(shí)間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fragment的基本使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

fragment 基本使用方法
?? ?1、繼承Fragment類或者子類并重寫
?? ??? ??? ?public View onCreateView(LayoutInflater inflater, ViewGroup container,
?? ??? ??? ??? ?Bundle savedInstanceState)
?? ??? 方法。把xml文件包裹成View對(duì)象,并對(duì)vie進(jìn)行操作,與activity操作View相同。
?? ?2、把fragment添加到activity中:
?? ?? 1)、在xml中添加,也即靜態(tài)添加:
?? ??? ? ??? ?<fragment
?? ???????? android:layout_below="@id/tv"
?? ???????? android:id="@+id/left_fragment"
?? ???????? android:name="com.example.test.LeftFragment"
?? ???????? android:layout_width="wrap_content"
?? ???????? android:layout_height="wrap_content"/>
?? ??? 2)、在代碼中添加,也即動(dòng)態(tài)添加:
?? ??? ???? FragmentManager LeftFragment fragment=new LeftFragment();
?? ??? ??? ?fragmentManager=getFragmentManager();
?? ??? ??? ?FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
?? ??? ??? ?fragmentTransaction.add(R.id.ll, fragment);
?? ??? ??? ?fragmentTransaction.commit();
?? ??? ??? ?注:需要commit().
?? ?3、基本操作,增、刪、改、查。
?? ????? 重要類:
?? ??? ??? FragmentManager: Fragment管理
?? ??? ??? FragmentTransaction:Fragment事務(wù)
?? ??? ??? Fragment:具體Fragment
?? ????? 增:add方法
?? ????? 刪:remove方法
?? ????? 改:replace方法
?? ????? 查:findFragmentByTag 或者 findFragmentById
?? ?4、回退棧
?? ????? 使用FragmentTransaction中的addToBackStack方法
?? ?5、與activity的通訊
?? ??????? 在Fragment內(nèi)定義一個(gè)接口,其作用是讓activity使用。
?? ??? ?1)、
?? ??? ??? ?public class FragmentB_1 extends Fragment implements OnClickListener {
?? ??? ??? ??? ?private FragmentBIF fragmentBIF;
?? ??? ??? ??? ?public interface FragmentBIF{
?? ??? ??? ??? ??? ?public void setB();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public View onCreateView(LayoutInflater inflater, ViewGroup container,
?? ??? ??? ??? ??? ??? ?Bundle savedInstanceState) {
?? ??? ??? ??? ??? ?View view = inflater.inflate(R.layout.b, null);
?? ??? ??? ??? ??? ?Button button=(Button) view.findViewById(R.id.but_b);
?? ??? ??? ??? ??? ?button.setOnClickListener(this);
?? ??? ??? ??? ??? ?return view;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?public void setFragmentBIF(FragmentBIF fragmentBIF) {
?? ??? ??? ??? ??? ?this.fragmentBIF = fragmentBIF;
?? ??? ??? ??? ?}
?? ??? ??? ???? //button的點(diǎn)擊事件作用到接口的setB方法
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public void onClick(View v) {
?? ??? ??? ??? ??? ?if(fragmentBIF!=null){
?? ??? ??? ??? ??? ??? ?fragmentBIF.setB();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ? 2)、
?? ??? ??? ?public class FragmentA_1 extends Fragment implements OnClickListener {
?? ??? ??? ?
?? ??? ??? ??? ?private FragmentIF fragmentIF;
?? ??? ??? ??? ?public interface FragmentIF{
?? ??? ??? ??? ??? ?public void setA();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public View onCreateView(LayoutInflater inflater, ViewGroup container,
?? ??? ??? ??? ??? ??? ?Bundle savedInstanceState) {
?? ??? ??? ??? ??? ? View view = inflater.inflate(R.layout.a, null);
?? ??? ??? ??? ??? ? Button button = (Button) view.findViewById(R.id.but_a);
?? ??? ??? ??? ??? ? button.setOnClickListener(this);
?? ??? ??? ??? ??? ? return view;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?//button的點(diǎn)擊事件作用到接口的setA方法上
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public void onClick(View v) {
?? ??? ??? ??? ??? ?if(getActivity() instanceof FragmentIF){
?? ??? ??? ??? ??? ??? ?((FragmentIF)getActivity()).setA();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ?????? ?
?? ??? ?? ?
?? ?? ?
?? ???

轉(zhuǎn)載于:https://blog.51cto.com/cbg23/1627006

總結(jié)

以上是生活随笔為你收集整理的fragment的基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。