在要传参的Fragment里面定义一个接口,接口里面有个方法,然后再activity里面实现接口的方法,先看效果图:

package com.example.myandroid.Fragment.Interaction1;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.myandroid.R;
public class ListsFragment extends Fragment {
private ListView mListView;
private String[] names = new String[] { "人类", "猴子", "猩猩", "老虎", "阿凡达",
"德莱尼", "克林姆" };
private onSetText  monsettext;
public interface onSetText {
public void showMessage(String message);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
monsettext=(onSetText) activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.lists_fragment_layout, container,
false);
mListView = (ListView) v.findViewById(R.id.lists_list);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_expandable_list_item_1, names);
mListView.setAdapter(adapter);
// mListView.setEmptyView(mProgressBar);//记住setEmptyView()方法,当没内容时进度条又显示出来
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ArrayAdapter<String>adpter=(ArrayAdapter<String>) parent.getAdapter();
String itemmessage=adpter.getItem(position);
monsettext.showMessage(itemmessage);
}
});
}
}
package com.example.myandroid.Fragment.Interaction1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myandroid.R;
public class ContentsFragment extends Fragment {
private static TextView mTextView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.contents_fragment,container,false);
mTextView=(TextView) v.findViewById(R.id.contents_fragment_text);
mTextView.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
mTextView.setText("每项内容");
return v;
}
public static void setMessageToTextView(String message){
mTextView.setText(message);
}
}
package com.example.myandroid.Fragment.Interaction1;
import com.example.myandroid.R;
import com.example.myandroid.Fragment.Interaction1.ListsFragment.onSetText;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
public class FragmentActivity extends Activity implements onSetText {
ContentsFragment contentsFragment;
ListsFragment listsfragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_interaction_layout);
contentsFragment = new ContentsFragment();
listsfragment=new ListsFragment();
FragmentTransaction tran =getFragmentManager()
.beginTransaction();
tran.add(R.id.lists_fragment,listsfragment).commit();
//      getFragmentManager().beginTransaction()
//              .add(R.id.lists_fragment, new ListsFragment()).commit();
getFragmentManager().beginTransaction()
.add(R.id.contents_fragment, new ContentsFragment()).commit();
}
@Override
public void showMessage(String message) {
//      Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
contentsFragment.setMessageToTextView(message);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lists_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/contents_fragment_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/lists_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/contents_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>

Fragment交互,接口方式从一个Fragment传参到另一个Fragment相关推荐

  1. 一个json传参的错误:JSON parse error: Unrecognized token ‘xxx‘{ “timestamp“: “2022-03-06T16:06:29.866

    一个json传参的错误: JSON parse error: Unrecognized token 'xxx' {     "timestamp": "2022-03-0 ...

  2. C#进阶系列——WebApi 接口参数不再困惑:传参详解

    看这边文章时的疑惑是:WebApi中的参数加了[FromBody],不知所以然,就百度了下,看到了以下文章,和大家分享下: 原文链接:http://www.cnblogs.com/landeanfen ...

  3. WebApi 接口参数不再困惑:传参详解

    阅读目录 一.get请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4."怪异"的get请求 二.post请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4 ...

  4. 【转】C#进阶系列——WebApi 接口参数不再困惑:传参详解

    阅读目录 一.get请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4."怪异"的get请求 二.post请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4 ...

  5. 回调函数自定义传参_koroFileHeader:一个用于生成文件头部注释和函数注释的插件...

    小金子 读完需要 2分钟 速读仅需 1 分钟 大家好,我是你们的小金子. 今天给大家分享的这个工具呢?对于使用 VS Code 的同学来讲,是一个好东西. koroFileHeader,一个在 vsc ...

  6. 在qt中用c语言数组,在QT函数中返回一个数组/把一个数组传参给函数

    1.把数组传参给函数 可以定义一个QVector的一个数组 QVector num(10); for(int  i =0;i<10;i++) num [i] = i*i; fun(num); / ...

  7. vue路由传参两种方式;vue路由传参query与params区别;vue路由跳转的带参与不带参,路由跳转传参方式:name 、 path;

    vue项目的路由传参常用的有两种方式:query和params 1.query传参特点:1.1可以用path也可以用name传递路径 注意name是路由页面vue文件的名称 不需要/1.2跳转页面地址 ...

  8. sC#进阶系列——WebApi 接口参数不再困惑:传参详解

    原文:http://www.cnblogs.com/landeanfen/p/5337072.html 一.get请求 对于取数据,我们使用最多的应该就是get请求了吧.下面通过几个示例看看我们的ge ...

  9. 用pytest.fixture处理接口自动化跨文件token传参

    大部分的接口都需要在headers中传入token参数,原来的方式是在case文件的setup中调用gettoken方法拿到token,存入一个变量,然后在每个case中使用这个token变量 但后面 ...

最新文章

  1. 出行公司集体亮剑 自动驾驶花落谁家?
  2. Linux内核文件vmlinux 和压缩后的bzImage文件格式分析
  3. 优化网站设计方案提升网站用户回头率
  4. 互联网大厂算法面试题集合,看完我跪了!
  5. Python3打印当前系统时间
  6. TCP/IP(五):TCP 协议详解
  7. 缓冲区溢出漏洞攻击之用户登录
  8. JSP中 input type 用法
  9. android开启热点softap模式,[RK3288][Android6.0] Wifi开启热点(SoftAP)流程小结
  10. oracle主键从键怎么看,分析Oracle主键的跳号现象
  11. Leetcode有java版么_leetcode 1. 两数之和(Java版)
  12. 数理统计基本原理复习
  13. 复合索引的使用与创建时候的顺序有关 如果顺序颠倒 则不起作用
  14. 190410每日一句
  15. 2019中国云计算十一大趋势预测与分析
  16. usbcan、can分析仪、can卡的产品特点和功能特点
  17. Quick BI产品核心功能大图(三)电子表格:新手亦可表格自由
  18. 开源流媒体客户端EasyClient手机端控制摄像机EasyCamera云台PTZ控制实现
  19. Matlab影像像素坐标得到经纬度/经纬度转影像坐标(已知经纬度获取影像DN值)
  20. PLC抑制干扰电路的设计

热门文章

  1. passive的反义词是什么?
  2. 心之所向,你和远方。
  3. 照片编辑软件app有哪些?照片编辑软件分享。
  4. 技术员系统(x86/x64)装机版/纯净版 2017.06
  5. 电子结构计算方法:三维布里渊区均匀取点的一般性方法Monkhorst-Pack方法
  6. Xshell连接Ubuntu详细过程
  7. GORM 外键ASSOCIATION_FOREIGNKEY和FOREIGNKEY和references的区别
  8. react-节点更新与销毁
  9. 一个简单的音乐网站设计与实现(HTML+CSS)
  10. 【天坑< 四 >】快递 E 站 (持续更新)