碎片:活动当中的UI的一个片段,让程序更加合理和充分的利用大屏幕的空间。
碎片的简单示例:
建立碎片类继承Fragment
Fragment:①android.app.Fragment ②:android :app.support.v4.app.Fragment
建议使用第二个库中的Fragment可以保持功能的一致性
RightFragment.java
package com.example.fragmenttest;
import android.os.Bundle;
import android.support.annotation.NonNull;
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 RightFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
左侧碎片一致
在各自的.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:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="40dp"
android:text="This is right fragment."
tools:ignore="MissingConstraints" />
</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"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</LinearLayout>
com.example.fragmenttest.LeftFragment对应java包中的类
运行示例图
动态添加碎片
步骤:
(1)创建待添加的碎片实例(再添加一个AnotherRightFragment用作后续变换)
(2)获取FragmentManager,在活动中通过调用getSupportFragmentManager()方法得到
(3)开启一个事务,通过beginTransaction方法开启
(4)像容器内添加或者替换碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片的实例
(5)提交事务,调用commit()方法来完成。
package com.example.fragmenttest;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button1);
button.setOnClickListener(this);
replaceFragment(new RightFragment());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
{
Log.d("TAG","111");
replaceFragment(new AnotherRightFragment());
Log.d("TAG","222");
break;
}
default:break;
}
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}
除此之外还需要修改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"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
仔细找不同:如果使用fragment 设置了android:name的属性,就会将碎片固定为某一个固定的碎片,没有办法进行变换。
运行截图:
变换前: 变换后:
注:可以给按钮通过开关设计多次触发事件,使每一次点击按钮可以进行不同界面的变换