android 开发 碎片Fragment布局例子(用按键切换碎片布局)

时间:2023-03-09 17:39:57
android 开发 碎片Fragment布局例子(用按键切换碎片布局)

实现思路

1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代)

2.写3个子布局,并且在写3个class继承Fragment布局

3.在MainActivity的class中写替换碎片布局的方法

(包含:FragmentManger(碎片管理器)、getSupportFragmentManager(得到支持碎片管理器)、FragmenTransaction(碎片交换器)、beginTransaction(开始碎片交换)、replace(替换)、commit(交付))

写一个父类布局,里面写一个按键和一个帧布局

<?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"
>
<Button
android:id="@+id/Button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="改变下面的碎片"/>
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8">
</FrameLayout> </LinearLayout>

写3个子布局,并且在写3个class继承Fragment布局

<?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"
android:padding="10dp">
<ImageView
android:id="@+id/fragment_1_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ace"/>
<TextView
android:id="@+id/fragment_1_TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/ace"
android:textColor="@color/colorBlack"
android:layout_marginTop="20dp"/>
</LinearLayout>

android 开发 碎片Fragment布局例子(用按键切换碎片布局)

相同布局在写2个

写一个class继承fragment实现碎片布局实例化

package com.example.lenovo.myfragmentdemo2.fragment;
// 注意Fragment的依赖库建议统一使用support-v4库中的
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.lenovo.myfragmentdemo2.R; /**
* Created by lenovo on 2018/5/7.
*/ public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1,container,false);
return view;
}
}

在MainActivity的class中写替换碎片布局的方法

package com.example.lenovo.myfragmentdemo2;
// 注意Fragment的依赖库
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; import com.example.lenovo.myfragmentdemo2.fragment.Fragment1;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment2;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment3; public class MainActivity extends AppCompatActivity {
private Button button;
private int i = 1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.Button1); button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (i){
case 1:
replaceFragment(new Fragment1());
i++;
break;
case 2:
replaceFragment(new Fragment2());
i++;
break;
case 3:
replaceFragment(new Fragment3());
i++;
break;
default:
replaceFragment(new Fragment1());
i=1;
break;
} }
});
} public void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();//注意!每次要执行commit()方法的时候都需要重新获取一次FragmentTransaction,否则用已经commit过的FragmentTransaction再次commit会报错!
transaction.replace(R.id.FrameLayout1,fragment);
transaction.commit(); }
}

运行效果:

android 开发 碎片Fragment布局例子(用按键切换碎片布局)点击后:android 开发 碎片Fragment布局例子(用按键切换碎片布局)