Fragment 简介
作者:互联网
Fragment是依赖于Activity的,不能独立存在的。一个Activity里可以有多个Fragment。一个Fragment可以被多个Activity重用。Fragment有自己的生命周期,并能接收输入事件。
一、生命周期
二、创建Fragment
1、静态方法:
2、静态加载的方法
3、Fragment与Activity交互。
三、示例代码
使用了dataBinding
MainTivity.class
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
//设置页面显示
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager());
mBinding.viewPage.setAdapter(adapter);
mBinding.tabLayout.setupWithViewPager(mBinding.viewPage);
}
}
layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabIndicatorColor="@color/white"
app:tabBackground="@drawable/tab_background"
app:tabSelectedTextColor="@color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/view_page" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_page"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_constraintTop_toBottomOf="@id/tab_layout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
fagment.class
public class LightFragment extends Fragment {
private static final String TAG = "LightFragment";
private FragmentLightBinding mLightBinding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLightBinding = FragmentLightBinding.inflate(LayoutInflater.from(requireContext()));
return mLightBinding.getRoot();
}
}
fragment_light.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.LightFragment">
<TextView
android:id="@+id/tv"
android:text="Hello"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MyAdapter.class
public class MyPageAdapter extends FragmentPagerAdapter {
//头部栏标题
private String[] titles = new String[]{"小灯1", "小灯2", "小灯3", "小灯4"};
//存放Fragment布局的List
private static List<Fragment> fragments = new ArrayList<>();
static {
//添加页面布局
fragments.add(new LightFragment(StaticData.ONE));
fragments.add(new LightFragment(StaticData.TWO));
fragments.add(new LightFragment(StaticData.THREE));
fragments.add(new LightFragment(StaticData.FOUR));
}
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
标签:Fragment,简介,class,LightFragment,new,fragments,public 来源: https://blog.csdn.net/LX_52Hz/article/details/119298731