Android | “碎片”笔记

本文详细介绍了Android中的Fragment,包括其概念、用法、在返回栈中的使用、与Activity的通信、生命周期以及动态加载布局的技巧。通过具体的实践案例,如NewsContentFragment和NewsTitleFragment,展示了如何在不同场景下运用Fragment。
摘要由CSDN通过智能技术生成

一、碎片概念

可以将碎片理解成一个迷你型活动,碎片通常在平板开发中使用

二、用法

(1)自定义一个类继承自Fragment,建议选择support-v4库中的Fragment,因为它可以让碎片在所有Android系统版本中保持功能一致性。
(2)新建碎片布局
(3)重写Fagment中的onCreateView()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的碎片布局动态加载进来(引用书中例子)

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

(4)在活动的布局中通过fragment标签引入要添加的碎片(通过其中的android:name属性指定自定义的Fragment类)(引用书中例子)

<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.wuze.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

(5)replaceFragment()方法动态添加/更换碎片(引用书中例子)

    private void replaceFragment(Fragment fragment){
   
        FragmentManager fragmentManager=getSupportFragmentManager();//创建待添加的碎片实例,调用getSupportFragmentManager()方法获取FragmentManager
        FragmentTransaction transaction=fragmentManager.beginTransaction();//通过调用beginTransaction()方法开启一个事务
        transaction.replace(R.id.right_layout,fragment);//replace()向容器内添加或替换碎片,需要传入容器的id和待添加的碎片实例
        transaction.addToBackStack(null);//返回栈,按下back回到上一个碎片,参数一般为null
        transaction.commit();//调用commit()提交事务
    }

(6)创建待添加的碎片实例作为参数传入replaceFragment()

replaceFragment(new AnotherRightFragment());

三、在碎片中使用返回栈

在事务提交之前调用FragmentTransaction的addToBackStack()方法,它可以接收一个名字用于描述返回栈的状态,一般传入null即可

transaction.addToBackStack(null);//返回栈,按下back回到上一个碎片,参数一般为null

四、碎片和活动之间的通信

(1)在活动中调用碎片:
在活动中调用FragmenrManager的findFragmentById()方法,可以在活动中得到相应碎片的实例,然后就可以调用碎片里的方法。
(2)在碎片中调用活动里的方法:
在碎片中调用getActivity()方法可以获得和当前碎片相关联的活动实例。
(3)碎片间通信:
在碎片中可以的到与它相关联的活动,再通过这个活动去获取另外一个碎片的实例,从而实现了碎片间的通信。

五、碎片的生命周期

碎片的生命周期与活动的生命周期类似,有四个状态:
(1)运行状态:当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态。
(2)暂停状态:当一个活动进入暂停状态时,与它相关联的可见碎片就会进入到暂停状态。
(3)停止状态:当一个活动进入停止状态时,与它相关联的碎片就会进入停止状态。
(4)销毁状态:活动被销毁,与它相关联的碎片也被销毁。

六、动态加载布局的技巧

(1)使用限定符
(2)使用最小宽度限定符

七、碎片的实践

目录结构如下:
在这里插入图片描述
下面按顺序对每个文件做一个解读:

.java

  1. MainActivity : 显示主布局,直接在onCreate方法中显示activity_main布局
  2. News : 新闻类,规范每一条新闻(这是一个List列表的泛型类,用来创建适配器)
  3. NewsContentActivity : 单页模式,创建一个Activity,显示手机端新闻内容
  4. NewsContentFragment : 新闻内容碎片(继承Fragment)
  5. NewsTitleFragment : 新闻列表碎片(加载news_title_frag布局)

.xml

  1. layout / activity_main : 单页模式下的布局,只加载一个新闻标题列表的的碎片
  2. news_content : 单页模式下的新闻文本内容布局,引入news_content_frag布局(在NewsContentActivity中显示新闻内容)
  3. news_content_frag : 新闻文本内容布局,单双页共用
  4. news_item : 标题子项布局
  5. news_title_frag : 新闻标题列表(RecyclerView)
  6. layout-sw600dp / activity_main : 双页模式下的布局,加载两个碎片(标题滑动列表+新闻文本内容)

下面放上具体代码:

来源:郭霖《第一行代码》

  1. MainActivity.java

加载activity_main布局

public class MainActivity extends AppCompatActivity {
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
  1. News.java

新闻类,set 和 get 方法

public class News {
   
    private String title;
    private String content;

    public void setTitle(String title) {
   
        this.title = title;
    }

    public void setContent(String content) {
   
        this.content = content;
    }

    public String getContent() {
   
        return co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值