首页 > 代码库 > Fragment间的替换和后退
Fragment间的替换和后退
一、新建项目
二、创建第一个Fragment
1.layout文件
<?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:id="@+id/btn_show_another_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示另一个Fragment" /> </LinearLayout>
2.class文件
package com.example.fragmentdemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; /** * Created by 袁磊 on 2017/2/7. * Fragment生命周期 */ public class PlaceHolderFragment extends Fragment { private static final String TAG = "PlaceHolderFragment"; public PlaceHolderFragment() { } private Button btnShowAnotherFragment; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, " a onCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); Log.d(TAG, "a onCreateView"); btnShowAnotherFragment = (Button) rootView.findViewById(R.id.btn_show_another_fragment); btnShowAnotherFragment.setOnClickListener(myOnClickListener); return rootView; } @Override public void onPause() { super.onPause(); Log.d(TAG, "a onPause"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "a onDestroyView"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "a onDestroy"); } private View.OnClickListener myOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_show_another_fragment: getFragmentManager().beginTransaction() .addToBackStack(null)//添加到后退栈,为了支持后退按钮 .replace(R.id.container, new AnotherFragment())//替换 .commit();//提交 break; } } }; }
三、创建第二个Fragment
1.layout文件
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是另一个Fragment" /> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="后退" /> </LinearLayout>
2.class文件
package com.example.fragmentdemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by 袁磊 on 2017/2/7. * Fragment常用生命周期 */ public class AnotherFragment extends Fragment { private static final String TAG = "AnotherFragment"; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_another, container, false); Log.d(TAG, "onCreateView"); root.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().popBackStack();//后退 } }); return root; } @Override public void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "onDestroyView"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } }
四、使用
1.activity_main中
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
2.MainActivity中
package com.example.fragmentdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceHolderFragment())//添加 .commit();//提交 } } }
五、运行效果
Fragment间的替换和后退
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。