Android之Fragment
16lz
2021-01-26
转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/51285961
本文出自【吴孝城的CSDN博客】
在Android中实现页面跳转有Activity,但用较为重量级的Activity速度会比Fragment慢点
下面用Fragment来实现页面跳转
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout 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:id="@+id/container" tools:context="cn.wuxiaocheng.fragment.MainActivity"/>
fragment_one.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"> <Button android:id="@+id/btnOne" android:text="one" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>fragment_two.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:background="#3c4f5e"> <Button android:id="@+id/btnWwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Two" /></LinearLayout>MainActivity.java
package cn.wuxiaocheng.fragment;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 OneFragment()) .commit(); } }}
OneFragment.java
package cn.wuxiaocheng.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class OneFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); view.findViewById(R.id.btnOne).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getFragmentManager().beginTransaction() //添加返回键功能 .addToBackStack(null) .replace(R.id.container, new TwoFragment()) .commit(); } }); return view; }}TwoFragment.java
package cn.wuxiaocheng.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TwoFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_two, container, false); view.findViewById(R.id.btnWwo).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getFragmentManager().popBackStack(); } }); return view; }}
更多相关文章
- Android实现RecyclerView显示json数据并点击跳转页面
- 【React Native开发】 - react-native-swiper的使用
- Android(安卓)锁屏状态下拉起某个页面
- Android(安卓)- 小问题 - ViewPager onPageChangeListener
- Android(安卓)WebView H5页面 input type =“file”解决方法
- Activity页面状态保存 持久化
- Android(安卓)之 TabHost 分页面板控件
- Toolbar+TabLayout+ViewPager达成Android最优导航栏
- Android(安卓)ViewPager轮播图