(一)、首先创建一个主应用
(二)、创建第一张碎片
(三)、创建第二张碎片
(四)、创建第三张碎片
2、将三张背景图片拷贝
到drawable目录
(一)、主布局activity_switch_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".switch_fragment">
</FrameLayout>
(二)、第一张碎片布局(fragment_first.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".FirstFragment"
android:background="@drawable/img1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/first_fragment"
android:textColor="#2196F3"
android:textSize="25sp"/>
<Button
android:id="@+id/btnNextFragmrnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNextFragmrnt"
android:text="@string/next_fragment" />
<Button
android:id="@+id/btnChampion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doChampion"
android:text="冠军" />
</LinearLayout>
(三)、第二张碎片布局(fragment_second.xml)
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/second_fragment"
android:textColor="#00BCD4"
android:textSize="25sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnNextFragmrnt"
android:onClick="doNextFragmrnt"
android:text="@string/next_fragment"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSecond"
android:onClick="doSecond"
android:text="亚军"/>
第三张碎片布局(fragment_third.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".ThirdFragment"
android:background="@drawable/img3"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/third_fragment"
android:textColor="#2196F3"
android:textSize="25sp"/>
<Button
android:id="@+id/btnNextFragmrnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNextFragmrnt"
android:text="@string/next_fragment"
/>
<Button
android:id="@+id/btnRunner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doRunner"
android:text="季军"
/>
</LinearLayout>
五、字符串资源文件strings.xml
六、主界面类
(一)、第一张碎片
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class FirstFragment extends Fragment {
private Button btnNextFragment;
private Button btnChampion;
public FirstFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/*
* @param inflater布局填充器
*@param container 容器
* @param saveInstanceState 保存实例状态
* @return
*
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 获取碎片视图
View view = inflater.inflate(R.layout.fragment_first,container,false);
//通过资源标识符获取控件实例
btnNextFragment= view.findViewById(R.id.btnNextFragmrnt);
btnChampion= view.findViewById(R.id.btnChampion);
//给按钮注册监听器
btnNextFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction().addToBackStack("next").replace(R.id.container,FragmentList.fragments.get(1)).commit();
}
});
btnChampion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),"我是冠军",Toast.LENGTH_SHORT).show();
}
});
//返回视图
return view;
}
}
(二)、第二张碎片
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class SecondFragment extends Fragment {
private Button btnNextFragment;
private Button btnSecond;
public SecondFragment() {
}
/*
* @param inflater布局填充器
*@param container 容器
* @param saveInstanceState 保存实例状态
* @return
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// 获取碎片视图
View view = inflater.inflate(R.layout.fragment_second,container,false);
//通过资源标识符获取控件实例
btnNextFragment= view.findViewById(R.id.btnNextFragmrnt);
btnSecond =view.findViewById(R.id.btnSecond);
//给按钮注册监听器
btnNextFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//跳转到第三个碎片
getFragmentManager().beginTransaction().addToBackStack("next").replace(R.id.container,FragmentList.fragments.get(2)).commit();
}
});
btnSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"我是亚军",Toast.LENGTH_SHORT).show();
}
});
//返回视图
return view;
}
}
(三)、第三张碎片
mport android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class ThirdFragment extends Fragment {
private Button btnNextFragment;
private Button btnRunner;
public ThirdFragment() {
}
/*
* @param inflater布局填充器
*@param container 容器
* @param saveInstanceState 保存实例状态
* @return
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 获取碎片视图
View view = inflater.inflate(R.layout.fragment_third,container,false);
//通过资源标识符获取控件实例
btnNextFragment= view.findViewById(R.id.btnNextFragmrnt);
btnRunner = view.findViewById(R.id.btnRunner);
//给按钮注册监听器
btnNextFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//跳转到第三个碎片
getFragmentManager().beginTransaction()
.addToBackStack("next")
.replace(R.id.container,FragmentList.fragments.get(0))
.commit();
}
});
btnRunner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"我是季军",Toast.LENGTH_SHORT).show();
}
});
//返回视图
return view;
}
}
(四)、主界面
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class switch_fragment extends AppCompatActivity {
private GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户 界面
setContentView(R.layout.activity_switch_fragment);
//获取碎片管理器
FragmentManager fm=getSupportFragmentManager();
//创建三个碎片,添加到碎片列表
FragmentList.fragments.add(new FirstFragment());
FragmentList.fragments.add(new SecondFragment());
FragmentList.fragments.add(new ThirdFragment());
// 在主容器里添加第一个碎片
fm.beginTransaction().add(R.id.container, FragmentList.fragments.get(0)).commit();
// 实例化手势侦测器
detector = new GestureDetector(new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
// 手势往左滑动10个像素,切换到下一个碎片
if (e1.getX() - e2.getX() > 10) {
// 获取返回栈条目个数
int count = getSupportFragmentManager().getBackStackEntryCount();
// 获取碎片索引
count = ++count % 3;
// 切换碎片
getSupportFragmentManager().beginTransaction()
.addToBackStack("" +count)
.replace(R.id.container, FragmentList.fragments.get(count))
.commit();
}
return true;
}
});
}
/**
* 将窗口的触摸事件交给收拾侦测器来处理
*
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
}
七、启动应用,查看效果