Android-Fragment(二)

96 阅读2分钟

「这是我参与11月更文挑战的第8天,活动详情查看:2021最后一次更文挑战

动态添加碎片

上一章那个中我们学会了在布局文件中添加碎片,但是碎片真正强大的地方在于它可以在程序运行时动态的添加到活动中,根据情况来动态的添加碎片,这样就可以将程序界面定制的更加多样化。

我们继续在上一章的代码基础上进行完善,新建another_right_fragment.xml,代码如下,与right_fragment.xml类似:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00">
    <TextView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is another Fragment"
        android:textSize="20sp"/>
</LinearLayout>

然后在新建一个AnotherRightFragment作为另一个右侧碎片:

public class AnotherRightFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment,container,false);
        return view;
    }
}

这样我们就准备好了另一个碎片,然后我们修改activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.wenjiancunchu.LeftFragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        />
    <FrameLayout
        android:id="@+id/right_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        />
</LinearLayout>

可以看到,现在将右侧碎片替换成了一个FragmentLayout中,这是Android中的一种布局,所有控件都默认摆放在布局的左上角,。由于这里仅需要在布局中放入一个碎片,不需要定位,所以非常适合使用FragmentLayout。下面我们将在代码中向FragmentLayout里添加内容,从而实现动态添加碎片的功能。修改MainActivity中的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        replaceFragment(new RightFragment());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }
    }
    public void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.right_fragment,fragment);
        fragmentTransaction.commit();
    }

可以看到,首先我们给左侧按钮添加一个button的点击事件,然后调用replaceFragment()方法动态添加RightFragment这个碎片,当点击左侧碎片的按钮时,又会调用replaceFragment()方法将右侧碎片替换成AndroidRightFragment。结合replaceFragment()方法我们可以看出,动态添加碎片主要分为5步。

(1)创建待添加的碎片实例。

(2)获取FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法得到。

(3)开启一个事务,通过调用beginTransaction()方法开启。

(4)向容器内添加或替换碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。

(5)提交事务,调用commit()方法来完成。

这样就完成了在活动中动态添加碎片的功能。