Android Fragment使用和切换 笔记
程序员文章站
2022-05-14 21:08:10
...
Demo下载
MainActivity代码
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Fragment1 fg1 =new Fragment1();
private Fragment2 fg2 = new Fragment2();
private TextView tv1;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwithFragment(fg1,this,R.id.fg).commit();
findViewById(R.id.fg1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SwithFragment(fg1,MainActivity.this,R.id.fg).commit();
}
});
findViewById(R.id.fg2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SwithFragment(fg2,MainActivity.this,R.id.fg).commit();
}
});
}
Fragment cacheFragment;
public FragmentTransaction SwithFragment(Fragment fragment, FragmentActivity fma, int idResources){
FragmentManager fm= fma.getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (!fragment.isAdded()){//还没添加 Fragment
if (cacheFragment !=null){
transaction.hide(cacheFragment);
}
transaction.add(idResources,fragment,fragment.getClass().getName());
}else{//已经添加过了 Fragment
transaction.hide(cacheFragment).show(fragment);
}
cacheFragment =fragment;
return transaction;
}
}
Fragment1 和 Fragment2 代码
package com.manggai.fragmentswith;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1,container,false);
return view;
}
}
fragment1.xml 和 fragment2.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment1"
android:textSize="100px"
></TextView>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/fg"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80px"
android:orientation="horizontal"
>
<TextView
android:gravity="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="50px"
android:id="@+id/fg1"
android:textSize="15px"
android:text="fg1"
android:layout_weight="1"
></TextView>
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_height="50px"
android:id="@+id/fg2"
android:textSize="15px"
android:text="fg2"
></TextView>
</LinearLayout>
</LinearLayout>
推荐阅读
-
Android编程使用Fragment界面向下跳转并一级级返回的实现方法
-
Android学习笔记--Activity中使用Intent传值示例代码
-
Android学习笔记--使用剪切板在Activity中传值示例代码
-
Android中Fragment相互切换间不被回收的实现方法
-
android组件化架构(速览android组件和使用技巧)
-
Android使用TabLayout+Fragment实现顶部选项卡
-
Android编程使用LinearLayout和PullRefreshView实现上下翻页功能的方法
-
Android笔记之:App自动化之使用Ant编译项目多渠道打包的使用详解
-
Android基础之使用Fragment控制切换多个页面
-
Android基础之使用Fragment适应不同屏幕和分辨率(分享)