静态以及动态添加Fragment以及基于接口的Fragment通信

首先,什么是Fragment:

Fragment也可以叫为“片段”,它可以表示Activity中的行为或用户界面部分。我们可以在一个Activity中用多个Fragment组合来构建多窗格的UI,以及在多个Activity中重复使用某个Fragment。它有自己的生命周期,能接受自己的输入,并且可以在 Activity 运行时添加或删除Fragment(有点像在不同 Activity 中重复使用的“子 Activity”)。

例如:

这里对回调方法做一个简要的描述

onAttach()在Fragment对象添加到Activity中时调用
onCreate() 创建Fragment对象时调用
onCreateView() 在Fragment绘制用户界面时调用
onActivityCreated()所在Activity和Fragment的UI界面创建时调用。
onStart()在任何UI变化时,Fragment开始变为可视状态时调用
onResume()在Fragment开始变为运行状态时调用
onPause()在Fragment运行状态结束线程挂起所在Activity不再是前台界面时调用
onSaveInstanceState() 在Fragment运行状态结束,保存UI状态时调用时
onStop()在可见状态结束时调用
onDestroyView() 在Fragment视图被删除时调用
onDestroy() 在Fragment生命周期结束时调用
onDetach() 当Fragment被所在Activity删除时调用

好了有了这些知识我们正式开始写
这是效果图


横屏显示效果

静态添加的Fragment

首先写两个fragment.xml

fragment_tiny_show:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView 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"
    android:scrollbars="none"
    tools:context=".TinyShowFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/image1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p1"/>
        <ImageView
            android:id="@+id/image2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p2"/>
        <ImageView
            android:id="@+id/image3"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p3"/>
        <ImageView
            android:id="@+id/image4"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p4"/>
        <ImageView
            android:id="@+id/image5"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p5"/>
        <ImageView
            android:id="@+id/image6"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p6"/>
        <ImageView
            android:id="@+id/image7"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p7"/>
        <ImageView
            android:id="@+id/image8"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_margin="15dp"
            android:scaleType="centerCrop"
            android:src="@drawable/p8"/>
    </LinearLayout>

</HorizontalScrollView>

fragment_big_show

<?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"
    android:orientation="vertical"
    tools:context=".BigShowFragment">

    <!-- TODO: Update blank fragment layout -->
    <ImageView
        android:id="@+id/ImageShow"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:scaleType="centerCrop"
        android:src="@drawable/p1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="25dp"
        android:text="this is fragment1"/>

</LinearLayout>

将两个fragment静态添加到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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.staticfragment.BigShowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        tools:layout="@layout/fragment_big_show"/>

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.staticfragment.TinyShowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:layout="@layout/fragment_tiny_show"/>

</LinearLayout>

在TinyShowFragment中onActivityCreated添加点击事件处理


public class TinyShowFragment extends Fragment {

    int imgId[]={R.id.image1,R.id.image2,R.id.image3,R.id.image4,R.id.image5,R.id.image6,R.id.image7,R.id.image8};
    ImageView tinyimage;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final ImageView imageshow=(ImageView)getActivity().findViewById(R.id.ImageShow);
        for(int i=0;i<imgId.length;i++)
        {
            tinyimage=(ImageView)getView().findViewById(imgId[i]);
            if(tinyimage!=null)
            {
                tinyimage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageView view=(ImageView)v;
                        imageshow.setImageDrawable(view.getDrawable());
                    }
                });
            }
        }
    }
}

这样静态的Fragment就成功了!

动态添加的Fragment

基于接口的Fragment通信

(有点事,过段时间再更新)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态添加 Fragment 到 ViewPager 中,需要使用 FragmentPagerAdapter 或 FragmentStatePagerAdapter。以下是添加 Fragment 的步骤: 1. 创建 FragmentPagerAdapter 或 FragmentStatePagerAdapter 的子类: ``` public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> mFragmentList = new ArrayList<>(); public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment) { mFragmentList.add(fragment); notifyDataSetChanged(); } } ``` 在这个子类中,我们需要重写 getItem() 方法,返回指定位置的 Fragment 对象;重写 getCount() 方法,返回 Fragment 的数量;还需要添加一个 addFragment() 方法,用于添加 Fragment。 2. 创建 ViewPager 对象,并设置 Adapter: ``` ViewPager viewPager = findViewById(R.id.viewpager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); ``` 3. 创建 Fragment 对象: ``` MyFragment fragment = new MyFragment(); ``` 4. 添加 Fragment 到 Adapter: ``` adapter.addFragment(fragment); ``` 5. 刷新 Adapter: ``` adapter.notifyDataSetChanged(); ``` 这样就添加了一个 Fragment 到 ViewPager 中。如果需要替换 ViewPager 中的 Fragment,可以使用 setItem() 方法,方法参数是要替换的 Fragment 对象和位置。如果需要从 ViewPager 中移除 Fragment,可以使用 removeItem() 方法,方法参数是要移除的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值