android 之Fragment的详解
友情链接:点击打开链接
1.将Activity传值到Fragment
具体步骤:
<1>声明碎片事务器对象
private FragmentManager fragmentManager;
<2>得到碎片事务器对象
FragmentTransaction fragmentTransaction = this.FragmentManager.beginTransaction();
<3>将我们需要添加的Fragment对象作为子元素添加到LinearLayout的Container容器中
fragmentTransaction.add(R.id.linearLayout_container,Fragment);
<4>将之前添加的Fragment对象也向Fragment的返回栈添加一份
fragmentTransaction.addBackStack(null);
<5>提交碎片事务,提交之后的操作才会生效。
fragmentTransaction.commit();
具体效果:
实例代码:
业务逻辑代码:
package com.example.tf.fragment_demo;import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.example.tf.fragment_demo.fragments.OneFragment;
import com.example.tf.fragment_demo.fragments.TwoFragment;public class MainActivity extends AppCompatActivity {/*** 声明碎片管理器对象*/private FragmentManager fragmentManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//1.得到碎片管理器对象this.fragmentManager=this.getFragmentManager();}//private OneFragment oneFragment=new OneFragment();//多次添加同一个地址报错private OneFragment oneFragment;/*** 添加Fragment* 注意:同一个fragment对象只能往同一个容器中添加一次,如果多次添加会报异常:java.lang.IllegalStateException: Fragment already added: OneFragment{5352c090 #0 id=0x7f0c0053}* @param view*/public void add(View view){oneFragment=new OneFragment();//2.通过碎片管理器对象得到碎片事务对象FragmentTransaction fragmentTransaction=this.fragmentManager.beginTransaction();//将oneFragment对象作为linearLayout_container的子元素添加到linearLayout_container容器里面fragmentTransaction.add(R.id.linearLayout_container,oneFragment);//将之前添加到容器linearLayout_container里面的fragment 也添加到fragment 返回栈中一份fragmentTransaction.addToBackStack(null);//提交碎片事务,提交之后之前的操作才会生效fragmentTransaction.commit();System.out.println("===add=====");}private TwoFragment twoFragment;/*** 修改Fragment* @param view*/public void update(View view){//2.通过碎片管理器对象得到碎片事务对象FragmentTransaction fragmentTransaction=this.fragmentManager.beginTransaction();twoFragment=new TwoFragment();//使用TwoFragment 替换LinearLayout 容器中的其它子元素,如果之前添加了多个子元素,则只替换其中部分子元素//replace 在替换指定Fragment 对象时会首先判断LinearLayout容器中是否有子元素,如果没有则直接添加一个子元素fragmentTransaction.replace(R.id.linearLayout_container,twoFragment);//提交碎片事务,提交之后之前的操作才会生效fragmentTransaction.commit();System.out.println("===update=====");}/*** 删除Fragment* @param view*/public void delete(View view){//2.通过碎片管理器对象得到碎片事务对象FragmentTransaction fragmentTransaction=this.fragmentManager.beginTransaction();//将twoFragment对象从碎片事务管理器中删除,即从指定容器中删除了fragmentTransaction.remove(twoFragment);//提交碎片事务,提交之后之前的操作才会生效fragmentTransaction.commit();System.out.println("===delete=====");}
}
主UI视图的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="添加"android:onClick="add"android:textSize="30sp"android:id="@+id/button_add"android:layout_alignParentTop="true"android:layout_alignParentStart="true"android:layout_marginTop="127dp" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="修改"android:onClick="update"android:textSize="30sp"android:id="@+id/button_update"android:layout_centerVertical="true"android:layout_alignParentStart="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="删除"android:onClick="delete"android:textSize="30sp"android:id="@+id/button_delete"android:layout_marginTop="52dp"android:layout_below="@+id/button_update"android:layout_alignParentStart="true" /><!--如果想同时实现水平和垂直两个方向的滚动操作,则可以使用ScrollView 嵌套HorizontalScrollView或者HorizontalScrollView嵌套ScrollView都可以实现水平和垂直两个方向的滚动操作--><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_toEndOf="@+id/button_add"><HorizontalScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_toEndOf="@+id/button_add"><LinearLayoutandroid:id="@+id/linearLayout_container"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentTop="true"android:layout_toEndOf="@+id/button_add"></LinearLayout></HorizontalScrollView></ScrollView></RelativeLayout>
<1>第一个Fragment.java
package com.example.tf.fragment_demo.fragments;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 com.example.tf.fragment_demo.R;import java.util.zip.Inflater;/*** Created by TF on 2018/6/13.*/
public class OneFragment extends Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_one,null);}
}
<2>第二个Fragment.java
同上,基本没有区别
[1]第一个的xml布局代码:
<?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"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="我是OneFragment we help each other and we love each other love me love my dog"android:singleLine="true"android:gravity="center"android:textSize="30sp"android:id="@+id/textView_one" />
</LinearLayout>
[2]第二个的xml布局代码:
同上,与第一个没有区别
2.将Fragment传值给Activity
1.得到Fragment所依附的Activity并通过findViewById()查找控件并赋值。
2.通过得到fragment所依附的Activity并给Activity提供setter函数传值到Activity或者让Activity实现传值的
接口来实现。
实例效果:
业务逻辑代码:
主类代码:
package com.example.lenovo.dialog_demo;import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import com.example.lenovo.dialog_demo.interfaces.PassValue;import org.w3c.dom.Text;import java.lang.reflect.Field;public class MainActivity extends AppCompatActivity implements PassValue{private TextView textView_name;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView_name = (TextView) this.findViewById(R.id.textView_name);}//通过setter函数传值到当前Activity@Overridepublic void setName(String name) {textView_name.setText(name);}
}
副类代码:
package com.example.lenovo.dialog_demo.fragments;import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;import com.example.lenovo.dialog_demo.R;
import com.example.lenovo.dialog_demo.interfaces.PassValue;public class DetailFragment extends Fragment{private EditText editText_userName;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_detail,container,false);editText_userName = (EditText) view.findViewById(R.id.editText_userName);Button button_pass = (Button) view.findViewById(R.id.button_pass);button_pass.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String userName = editText_userName.getText().toString();if(TextUtils.isEmpty(userName)){editText_userName.setError("用户名不能为空");editText_userName.requestFocus();return;}//得到当前Fragment所依附的Activity//MainActivity mainActivity = (MainActivity) getActivity();//方式一:找到Activity中的TextView控件//TextView textView_name = mainActivity.findViewById(R.id.textView_name);//textView_name.setText(userName);//方式二:给Activity增加setter函数传值给Activity//mainActivity.setName(userName);//使用接口完成通用传值操作PassValue passValue = (PassValue) getActivity();passValue.setName(userName);}});return view;}
}
定义一个接口:
package com.example.lenovo.dialog_demo.interfaces;public interface PassValue {void setName(String name);
}
用户界面的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/linearLayout_left"android:orientation="vertical"android:layout_weight="1"android:layout_height="match_parent"android:layout_width="match_parent"><fragmentandroid:id="@+id/fragment"android:layout_width="match_parent"android:layout_height="wrap_content"android:name="com.example.lenovo.dialog_demo.fragments.DetailFragment"/></LinearLayout><LinearLayoutandroid:id="@+id/linearLayout_right"android:orientation="vertical"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="姓名"android:textSize="30sp"android:id="@+id/textView_name"/></LinearLayout></LinearLayout>
用户输入界面的xml:
<?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"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入你的姓名"android:id="@+id/editText_userName"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="传值到Activity"android:id="@+id/button_pass"/>
</LinearLayout>
3.Fragment传值给Fragment(中心思想:并不是传值,而是通过查找目标控件,将要传的值"塞"给"目标控件")。
注意:一个Fragment的子类listFragment
继承listFragment有哪些好处?
A.自带界面并且界面上有一个ListView,这个ListView对象为mList
B.点击用户界面获取数据时,监听器不用自己写,ListFragment已经封装好了,可以直接使用
主要方式:一个Fragment得到FragmentManager查找到另一个Fragment。
1.并通过setter函数查找控件并赋值。
2.getView方法得到view查找控件并赋值。
FragmentLeft(要传值的):
package com.hsj.example.fragmentpassvaluetofragmentdemo05.fragments;import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;import com.hsj.example.fragmentpassvaluetofragmentdemo05.R;import java.util.ArrayList;
import java.util.List;/*** 继承ListFragment 有哪些好处?* A:自带界面并且界面上有一个ListView,通过查看源码发现这个ListView 对象为mList* B:点击用户条目获取数据时监听器不用自己编写,ListFragment 已经封装好了,我们直接使用*/
public class LeftFragment_bak01 extends ListFragment {private List<String> data;@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);data=new ArrayList<>();for(int i=0;i<20;i++){data.add("小丽"+i);}ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,data);setListAdapter(adapter);}/*** 当用户点击ListView控件上的条目时自动调用的方法* @param l* @param v* @param position* @param id*/@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {String item=data.get(position);//由于现在LeftFragment 和RightFragment 使用的是同一个FragmentManager 添加到MainActivity 界面上的,因此可以通过FragmentManager 找到需要的Fragment////TextView textView_name= (TextView) getActivity().getFragmentManager().findFragmentById(R.id.fragment_right).getView().findViewById(R.id.textView_name);//textView_name.setText(item);//Toast.makeText(getActivity(), "item="+item, Toast.LENGTH_SHORT).show();RightFragment rightFragment= (RightFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_right);//rightFragment.setName(item);}
}
FragmentRight(接受的):
package com.hsj.example.fragmentpassvaluetofragmentdemo05.fragments;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.AdapterView;
import android.widget.ListView;
import android.widget.TextView;import com.hsj.example.fragmentpassvaluetofragmentdemo05.R;/*** Created by hsjwcf on 2018/6/14.*/
public class RightFragment extends Fragment {private TextView textView_name;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view=inflater.inflate(R.layout.fragment_right,null);textView_name= (TextView) view.findViewById(R.id.textView_name);LeftFragment leftFragment= (LeftFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_left);ListView listView=leftFragment.getListView();listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//1.通过适配器控件获取数据//String item=parent.getItemAtPosition(position).toString();//textView_name.setText(item);//2.通过用户点击的条目获取数据String item=((TextView)view).getText().toString();textView_name.setText(item);}});return view;}}
3.注意:万能的接口回调(适合所有的处置方式):
使用步骤:
1.自定义传值的接口:
interface PassValue{
void setName(String name);
}
2.声明一个接口类型的对象:
private PassValue passvalue
3.给接口类型的对象提供setter函数:
public void setPassValue(PassValue passvalue){
this.passvalue = passvalue;
}
4.在传值的目标端(RightFragment)调用setter函数
5.在需要传值的地方调用PassValue接口类型的对象passValue.setValue(需要传递的值)
1.leftFragment:
package com.hsj.example.omnipotentinterfacecallbackdemo06.fragments;import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;import java.util.ArrayList;
import java.util.List;/*** 万能的接口回调的编写步骤:* 1.自定义传值的接口* interface PassValue{void setName(String name);}2.声明一个接口类型的对象private PassValue passValue;3.给接口类型的对象提供setter 函数public void setPassValue(PassValue passValue){this.passValue=passValue;}4.在需要传值的目标对象中调用setter函数,相当于注册监听器对象LeftFragment leftFragment= (LeftFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_left);//相当于注册监听器leftFragment.setPassValue(new LeftFragment.PassValue() {@Overridepublic void setName(String name) {textView_name.setText(name);}});5.在需要传值的地方调用传值对象的传值方法完成传值操作passValue.setName(item);**/
public class LeftFragment extends ListFragment {private List<String> data;//1.自定义传值的接口interface PassValue{void setName(String name);}//2.声明一个接口类型的对象private PassValue passValue;//3.给接口类型的对象提供setter 函数public void setPassValue(PassValue passValue){this.passValue=passValue;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);data=new ArrayList<>();for(int i=0;i<20;i++){data.add("小丽"+i);}ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,data);setListAdapter(adapter);}/*static class Add{int add(int num1,int num2){int sum=num1+num2;return sum;}}static class Test{public static void main(String[] args){Add a=new Add();a.add(5,6);}}*/@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {String item= data.get(position);//执行传值操作,相当于用户点击了按钮passValue.setName(item);}
}
2.rightFragment:
package com.hsj.example.omnipotentinterfacecallbackdemo06.fragments;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;import com.hsj.example.omnipotentinterfacecallbackdemo06.R;/*** Created by hsjwcf on 2018/6/14.*/
public class RightFragment extends Fragment {private TextView textView_name;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view=inflater.inflate(R.layout.fragment_right,null);textView_name= (TextView) view.findViewById(R.id.textView_name);LeftFragment leftFragment= (LeftFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_left);//相当于注册监听器/*leftFragment.setPassValue(new LeftFragment.PassValue() {@Overridepublic void setName(String name) {textView_name.setText(name);}});*/MyPassValue myPassValue=new MyPassValue();leftFragment.setPassValue(myPassValue);return view;}class MyPassValue implements LeftFragment.PassValue{@Overridepublic void setName(String name) {textView_name.setText(name);}}}
android 之Fragment的详解相关推荐
- Android 碎片fragment使用详解
作为一名专业的android开发人员,需要我们能够同时做到兼顾平板和手机.下面的内容会对你有一定帮助 目录 1.碎片介绍 2.碎片的基本使用 3.动态添加碎片 4.在碎片中模拟返回栈 5. 碎片和活动 ...
- Android中Fragment的详解和使用。
一.Fragment的基础知识介绍 1.1概述 1.1.1 特性 Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面 ...
- android默认exported_Android android:exported = true 用法详解
Android android:exported = true 用法详解 Android相关属性的介绍:android:exported = true 在Activity中该属性用来标示:当前Acti ...
- Android Activity 生命周期详解及监听
前言 系列文章: Android Activity 与View 的互动思考 Android Activity 生命周期详解及监听 Android onSaveInstanceState/onResto ...
- android jar 包 意见反馈功能,android重点jar包详解.docx
android重点jar包详解 深入理解View(一):从setContentView谈起 我们都知道?MVC,在Android中,这个?V?即指View,那我们今天就来探探View的究竟.在onCr ...
- Android应用坐标系统全面详解
Android应用坐标系统全面详解 原文链接:CSDN@工匠若水,http://blog.csdn.net/yanbober/article/details/50419117 1. 背景 去年有很多人 ...
- Android NFC开发实战详解
Android NFC开发实战详解 Android开发实战详解NFC国内第一本AndroidNFC开发书籍带你开启AndroidNFC开发的神秘之旅大综合案例帮助读者快速进入实战角色:WiFi快速连接 ...
- Android Gradle 自定义Task详解二:进阶
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78523958 本文出自[赵彦军的博客] 系列目录 Android Gradle使用 ...
- Android Gradle 自定义Task 详解
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/76408024 本文出自[赵彦军的博客] 系列目录 Android Gradle使用 ...
最新文章
- Opencv cv2.KalmanFilter 鼠标跟踪
- 快逸报表API直接生成v4统计图
- JavaScript 读书笔记(二)— Date类型
- 201506251117_《react——所需知识、主要技术》
- 天勤数据结构:前缀、中缀、后缀表达式的转换与计算
- 【Python】APScheduler定时任务
- oracle thread日志查看,请教归档日志的视图v$archived_log里的thread#的问题
- 技术分享 | jaeger链路日志实现
- EXC_BAD_ACCESS
- python爬取百度百科搜索结果_用Python抓取百度搜索结果,python,爬取,的
- 设计灵感图片,到哪里搜索!看这里!
- json标签批量转为xml标签(VOC数据集制作)
- 拓端tecdat|R语言中的隐马尔可夫HMM模型实例
- mysql 锁表 for update,MySQL中select * for update锁表的问题(转)
- nvm nodejs npm快速安装
- LBP特征详细原理-python代码复现
- snort实验(一)
- Android开发之监听或获取手机短信内容
- 名悦集团带你解析汽车底盘维护的那些小知识
- unity2D动画和图片切割
热门文章
- linux下使用split 来分割大文件
- 【树莓派】小空间树莓派镜像系统备份方法img镜像文件压缩方法
- 基于CASIA-GaitDatasetB步态图像轮廓数据库的步态周期检测与步态角度特征MATLAB源码
- python爬虫实战:利用scrapy,短短50行代码下载整站短视频
- 一个很简短的 JS 生成器入门和用法参考
- FPGA+DSP SRIO通信(一)——DSP端参数设置(通道)
- 基于CentOS 6.8平台最新源代码包编译安装企业版MariaDB数据库
- Bitbucket Pipelines在Atlassian的Bitbucket云上提供持续交付功能
- nagios安装与配置详解1
- 修改Oracle中的某一带有数据的列的数据类型