首页 > 杂文归档 正文
Android Fragment 简介及使用示例(一)

 2021-05-17 19:47:17     


关于 Fragment 可以归纳出如下特征:

Fragment 总是作为 Activity 界面的组成部分。Fragment 可以调用 getActivity() 方法获取它所在的 Activity,Activity 可调用 Fr

关于 Fragment 可以归纳出如下特征:

  1. Fragment 总是作为 Activity 界面的组成部分。Fragment 可以调用 getActivity() 方法获取它所在的 Activity,Activity 可调用 FragmentManager 的 findFragmentById() 或 findFragmentByTag() 方法来获取 Fragment。在界面布局文件中使用 元素添加 Fragment 时,可以为 元素指定 android:id 或者 android:tag 属性,这两个属性都可以用于标识该 Fragment,之后可以使用 findFragmentById() 或 findFragmentByTag() 方法来获取指定的 Fragment;
  2. 在 Activity 运行的过程中,可以调用 FragmentManager 的 add()、remove()、replace() 方法动态地添加、删除或替换 Fragment;
  3. 一个 Activity 可以同时组合多个 Fragment;同时,一个 Fragment 也可以被多个 Activity 复用;
  4. Fragment 可以响应自己的输入事件,并拥有自己的生命周期,但是它们的生命周期直接被所属的 Activity 的生命周期控制。

为了在 Activity 中显示 Fragment,还必须将 Fragment 添加到 Activity 中。将 Fragment 添加到 Activity 中有如下两种方式:

  1. 在布局文件中使用 元素添加 Fragment, 元素的 android:name 属性指定 Fragment 的实现类;
  2. 在 Java 代码中通过 FragmentTransaction 对象的 add() 方法来添加 Fragment。Activity 的 getFragmentManager() 方法可以返回 FragmentManager 对象,FragmentManager 对象的 beginTransaction() 方法即可开启并返回 FragmentTransaction 对象。

Fragment 与 Activity 之间传递数据

  1. Activity 向 Fragment 传递数据:在 Activity 中创建 Bundle 数据包,并调用 Fragment 的 setArgument(Bundle bundle) 方法即可将 Bundle 数据包传给 Fragment。
  2. Fragment 向 Activity 传递数据 或 Activity 需要在 Fragment 运行中进行实时通信:在 Fragment 中定义一个内部回调接口,再让包含该 Fragment 的 Activity 实现该回调接口,这样 Fragment 即可调用该回调接口的方法将数据传给 Activity。

FragmentManager 可以完成如下的功能:

  1. 使用 findFragmentById() 或 findFragmentByTag() 方法来获取指定的 Fragment;
  2. 调用 popBackStack() 方法将 Fragment 从后台栈中弹出(类似用户按下 BACK 键);
  3. 调用 addOnBackStackChangeListener() 注册一个监听器,用于监听后台栈的变化。

如果我们需要添加、删除、替换 Fragment,则需要使用 FragmentTransaction 对象,它 代表 Activity 对 Fragment 执行多个改变。我们可以通过如下方法获取 FragmentTransaction 对象:

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

在调用 commit() 方法之前,我们也可以调用 addToBackStack() 方法将事务添加到 back 栈中,该栈由 Activity 负责管理,这样允许用户按下 BACK 键返回到上一个 Fragment 的状态。
示例代码:

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.book_detail_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

下面是源代码部分###

首先是 fragment_book_detail.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>


    

    


接下来是 activity_book_twopane.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>


    

    


接下来,是 BookContent.java 文件:

package com.toby.personal.testlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by toby on 17-3-29.
 */

class BookContent {

    static class Book {
        public Integer id;
        String title;
        String desc;

        Book(Integer id, String title, String desc) {
            this.id = id;
            this.title = title;
            this.desc = desc;
        }

        @Override
        public String toString() {
            return title;
        }
    }

    static List ITEMS = new ArrayList<>();
    static Map ITEM_MAP = new HashMap<>();

    static {
        addItem(new Book(1, "小狗钱钱", "理财入门读物"));
        addItem(new Book(2, "小狗钱钱的爸爸", "理财入门读物"));
        addItem(new Book(3, "穷爸爸和富爸爸", "理财入门读物"));
    }

    private static void addItem(Book book) {
        ITEMS.add(book);
        ITEM_MAP.put(book.id, book);
    }

}

接下来,是 BookListFragment.java 文件:

package com.toby.personal.testlistview;

import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by toby on 17-3-29.
 */

public class BookListFragment extends ListFragment {

    private Callbacks callbacks;

    interface Callbacks {
        void onItemSelected(Integer id);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1, BookContent.ITEMS));
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (!(context instanceof Callbacks)){
            throw new IllegalStateException("Callbacks interface must be implemented!");
        }

        callbacks = (Callbacks) context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        callbacks = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        callbacks.onItemSelected(BookContent.ITEMS.get(position).id);
    }

    public void setActivateOnItemClick(boolean activateOnItemClick) {
        getListView().setChoiceMode(activateOnItemClick ?
                ListView.CHOICE_MODE_SINGLE: ListView.CHOICE_MODE_NONE);
    }

}

接下来,是 BookDetailFragment.java 文件:

package com.toby.personal.testlistview;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by toby on 17-3-29.
 */

public class BookDetailFragment extends Fragment {

    final public static String ITEM_ID = "item_id";

    private BookContent.Book book;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ITEM_ID)) {
            book = BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_book_detail, container, false);

        if (book != null) {
            ((TextView) rootView.findViewById(R.id.book_title)).setText(book.title);
            ((TextView) rootView.findViewById(R.id.book_desc)).setText(book.desc);
        }

        return rootView;
    }
}

接下来,是 SelectBookActivity.java 文件:

package com.toby.personal.testlistview;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;

/**
 * Created by toby on 17-3-29.
 */

public class SelectBookActivity extends Activity implements BookListFragment.Callbacks {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_twopane);
    }

    @Override
    public void onItemSelected(Integer id) {
        Bundle arguments = new Bundle();
        arguments.putInt(BookDetailFragment.ITEM_ID, id);
        BookDetailFragment fragment = new BookDetailFragment();
        fragment.setArguments(arguments);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.book_detail_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

最后,调整 AndroidManifest.xml 文件:

        
            
                

                
            
        

程序在虚拟机上的运行效果:

Android Fragment 简介及使用示例(一)_第1张图片
程序在虚拟机上的运行效果

参考文献:《疯狂Android讲义(第2版)》

原文链接:http://www.yuepc.com/a/71946.html

http://www.yuepc.com 为 “沈一博客” 唯一官方服务平台,请勿相信其他任何渠道。

  •  标签: