Android中Fragment的加载方式与数据通信详解

 更新时间:2017年03月23日 11:42:46   作者:Joah  
本文主要介绍了Android中Fragment的加载方式与数据通信的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧

一、加载方式

1. 静态加载

1.1 加载步骤

(1) 创建fragment:创建自定义Fragment类继承自Fragment类,同时将自定义Fragment类与Fragment视图绑定(将layout转换成View)

View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

inflater用于绑定Fragment的布局文件,同时将该布局转换成View对象并返回;container为Fragment的UI所在的父容器。返回值为Fragment显示的UI,若不显示,则返回null。

inflate(int resource, ViewGroup root, boolean attachToRoot)

resource为Fragment需要加载的布局文件;root为加载Fragment的父ViewGroup,也就是onCreateView传递进来的container;attachToRoot为是否返回父ViewGroup。

(2) 使用fragment:在父视图中引入fragment,静态加载必须指定name属性以及一个唯一标识符,标识符可以为id或者tag

<!--指定在layout中实例化的Fragment类,需要为“包名.类名”的完整形式-->
android:name
<!--唯一标识,id和tag可任选其一,不可两者都没有-->
android:id
android:tag

(3) 监听事件:若在父视图对应的类中设置监听事件,可以直接访问fragment中的子组件;若在Fragment的类中设置,则必须通过inflate()返回的View对象访问Fragment中的子组件(view.findViewById(id))。

1.2 简单范例

MyFragment视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/fragment_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>

MyFragment类:

public class MyFragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 //将layout布局转换成View对象
 View view = inflater.inflate(R.layout.myfragment, container, false);
 //必须通过view对象对其子组件进行访问
 TextView textView = (TextView) view.findViewById(R.id.fragment_text);
 textView.setText("这里是fragment");
 //返回Fragment显示UI
 return view;
 }
}

引用fragment的父视图:

<?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" tools:context="com.studying.StaticFragmentActivity">
 <fragment
 android:tag="fragment"
android:name="com.joahyau.studying.MyFragment"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
</LinearLayout>

父视图对应的类设置事件监听:

public class StaticFragmentActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_static_fragment);
 //可直接通过findViewById访问
 findViewById(R.id.fragment_text).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  Toast.makeText(StaticFragmentActivity.this, "点击了文本", Toast.LENGTH_SHORT).show();
  }
 });
 }
}

2. 动态加载

2.1 加载步骤

(1) 获取事务管理器:对Fragment进行的添加、移除、替换等操作,均为事务。需通过以下代码获取事务管理器,从而对fragment进行动态操作。

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

(2) 创建Fragment对象:创建需要加载的fragment,而后通过add或replace等方法实现动态加载。

2.2 简单范例

布局:

<?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="io.github.joahyau.studying.DynamicFragmentActivity">
 <Button
 android:id="@+id/load"
 android:text="加载"
 android:layout_width="match_parent"
 android:layout_height="80dp" />
 <LinearLayout
 android:id="@+id/container"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal" />
</LinearLayout>

Java:

public class DynamicFragmentActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_dynamic_fragment);
 findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  //获取事务管理器
  FragmentManager fragmentManager = getFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  //创建fragment,并将其动态加载到id位container的布局中
  MyFragment myFragment = new MyFragment();
  fragmentTransaction.add(R.id.container, myFragment);
  //提交事务
  fragmentTransaction.commit();
  }
 });
 }
}

二、数据通信

3. Activity向Fragment传递数据

3.1 Activity向动态加载的Fragment传递数据

(1)在Activity中获取Fragment对象;

(2)创建Bundle对象并传入数据;

(3)将Bundle对象传递给Fragment对象;

(4)在Fragment中获取Bundle对象并拆包得到数据。

范例:Activity中只有一个id为send的Button,MyFragment中只有一个TextView,这里就不再放布局代码了。

Activity:

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  //创建Fragment对象
  MyFragment myFragment = new MyFragment();
  //创建Bundle对象并传入数据
  Bundle bundle = new Bundle();
  bundle.putString("info", "这里是向Fragment传递的数据");
  myFragment.setArguments(bundle);
  //加载Fragment
  FragmentManager fragmentManager = getFragmentManager();
  FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  beginTransaction.add(R.id.layout, myFragment, "myfragment");
  beginTransaction.commit();
  }
 });
 }
}

Fragment:

public class MyFragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.my_fragment, container, false);
 TextView tv = (TextView) view.findViewById(R.id.text);
 //获取数据
 String text = getArguments().get("info") + "";
 tv.setText(text);
 return view;
 }
}

3.2 Activity向静态加载的Fragment传递数据

(1)在Fragment中创建作为容器的数据对象,并创建getter和setter;

(2)在Activity中获取FragmentManager;

(3)通过事务管理器的findFragmentById或findFragmentByTag方法,获得fragment对象;

(4)通过获得的fragment对象调用容器的setter方法进行传值。

范例:这里的布局与动态加载的布局唯一不同的就是将send按钮放在了Fragment里面,其它相同。

Fragment:

public class MyFragment extends Fragment {
 private Button btn;
 private String received;//作为容器的对象
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.my_fragment, container, false);
 TextView tv = (TextView) view.findViewById(R.id.text);
 tv.setText("这里是Fragment");
 btn = (Button) view.findViewById(R.id.send);
 btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  Toast.makeText(getActivity(), "成功接收\"" + getReceived() + "\"", Toast.LENGTH_SHORT).show();
  }
 });
 return view;
 }
 public String getReceived() {
 return received;
 }
 public void setReceived(String received) {
 this.received = received;
 }
}

Activity:

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 FragmentManager fragmentManager = getFragmentManager();
 MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
 myFragment.setReceived("this is a test.");
 }
}

4. Fragment向Activity传递数据

(1)在Fragment中写一个回调接口;

(2)在activity中实现这个回调接口,实现的函数用于传值;

(3)重写Fragment中onAttach,在其中创建一个接口对象,得到传递过来的activity(我的理解是这个接口其实相当于传递过来的activity的一个父类,这一步是用到了多态的特性);

(4)用得到的接口对象进行传值。

Fragment:

public class MyFragment extends Fragment {
 private SendData sendData;
 @Override
 public void onAttach(Activity activity) {
 super.onAttach(activity);
 //获取实现的接口对象
 sendData = (SendData) activity;
 }
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.my_fragment, container, false);
 TextView tv = (TextView) view.findViewById(R.id.text);
 tv.setText("这里是Fragment");
 //通过接口对象传递数据
 sendData.sendMsg("this is a test.");
 return view;
 }
 //定义一个回调接口
 public interface SendData{
 void sendMsg(String str);
 }
}

Activity:

public class MainActivity extends Activity implements MyFragment.SendData{
 private Button btn;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 btn = (Button) findViewById(R.id.send);
 btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  MyFragment myFragment = new MyFragment();
  FragmentManager fragmentManager = getFragmentManager();
  FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  beginTransaction.add(R.id.layout, myFragment);
  beginTransaction.commit();
  }
 });
 }
 //实现SendData接口,接收数据
 @Override
 public void sendMsg(String str) {
 Toast.makeText(this, "成功接收\"" + str + "\"", Toast.LENGTH_SHORT).show();
 }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

相关文章

  • Android 实现ListView的点击变色的实例

    Android 实现ListView的点击变色的实例

    这篇文章主要介绍了Android 实现ListView的点击变色的实例的相关资料,主要实现Android listveiw ItemClickListener写入变色的功能,需要的朋友可以参考下
    2017-07-07
  • 使用Android WebSocket实现即时通讯功能

    使用Android WebSocket实现即时通讯功能

    即时通讯(Instant Messaging)最重要的毫无疑问就是即时,不能有明显的延迟,要实现IM的功能其实并不难,目前有很多第三方,比如极光的JMessage,都比较容易实现。本文通过实例代码给大家分享Android WebSocket实现即时通讯功能,一起看看吧
    2019-10-10
  • 基于Android实现仿QQ5.0侧滑

    基于Android实现仿QQ5.0侧滑

    本课程将带领大家通过自定义控件实现QQ5.0侧滑菜单,课程将循序渐进,首先实现最普通的侧滑菜单,然后引入属性动画与拖动菜单效果相结合,最终实现QQ5.0侧滑菜单效果。通过本课程大家会对侧滑菜单有更深层次的了解,通过自定义控件和属性动画打造千变万化的侧滑菜单效果
    2015-12-12
  • Eclipse工程转为兼容Android Studio模式的方法步骤图文详解

    Eclipse工程转为兼容Android Studio模式的方法步骤图文详解

    这篇文章主要介绍了Eclipse工程转为兼容Android Studio模式的方法步骤,本文图文并茂给大家介绍的非常详细,需要的朋友可以参考下
    2017-12-12
  • Android适配利用webview加载后图片显示过大的问题解决

    Android适配利用webview加载后图片显示过大的问题解决

    这篇文章主要给大家介绍了关于Android适配利用webview加载后图片显示过大问题的解决方法,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • Android开发使用WebView打造web app示例代码

    Android开发使用WebView打造web app示例代码

    这篇文章主要介绍了Android开发使用WebView打造web app的关键示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • Android开发中使用Intent打开第三方应用及验证可用性的方法详解

    Android开发中使用Intent打开第三方应用及验证可用性的方法详解

    这篇文章主要介绍了Android开发中使用Intent打开第三方应用及验证可用性的方法,结合实例形式分析了Android使用Intent打开第三方应用的三种常用方式及使用注意事项,需要的朋友可以参考下
    2017-11-11
  • Android开发实现自定义新闻加载页面功能实例

    Android开发实现自定义新闻加载页面功能实例

    这篇文章主要介绍了Android开发实现自定义新闻加载页面功能,结合具体实例形式分析了Android界面加载及页面布局相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • android 大图片拖拽并缩放实现原理

    android 大图片拖拽并缩放实现原理

    android 大图片拖拽缩放有利于用户体验,在开发过程中经常使用到,这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似的问题有帮助
    2013-01-01
  • Android使用setContentView实现页面的转换效果

    Android使用setContentView实现页面的转换效果

    这篇文章主要介绍了Android如何使用setContentView实现页面的转换效果,帮助大家更好的利用Android进行开发,感兴趣的朋友可以了解下
    2021-01-01

最新评论