静态添加的碎片中相互通信
来源:互联网 发布:网络女主播真的漂亮吗 编辑:程序博客网 时间:2024/06/07 11:59
在静态添加的碎片中通信要借助他们的宿主Activity,(插句题外话:在activity中findViewById()只能寻找到setContentView()加载的布局中的控件,不在里面的控件是找不到的
一个通信小例子,右边Fragment中包含一个Button,左边Fragment包含一个TextView,点击右边Fragment中的Button可以改变左边Fragment中文本中的文字,
右边Fragment中的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="replace" android:id="@+id/button"/></LinearLayout>
右碎片
public class RightFragment extends Fragment { private Button button; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, container,false); button =(Button) view.findViewById(R.id.button); return view; } public void changeContent(final TextView getView){ button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { getView.setText("change by right"); Log.d("test", "button click"); } }); }左边碎片的布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_red_light"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/left1" android:text="this is left fragment"/></LinearLayout>左边碎片public class leftFragment extends Fragment { RightFragmentListener callBack; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container); return view; }}宿主Activitypublic class MyActivity extends FragmentActivity { private TextView textView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content); //如果布局content中不包括控某件的话,用findViewById(R.id.left1)是找不到该控件的 /*TextView textView = (TextView) findViewById(R.id.left1); textView.setText("aa");*/ TextView textView = (TextView) findViewById(R.id.left1); RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right); rightFragment.changeContent(textView); }
宿主中的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/linearLayout"> <fragment android:layout_width="0dp" android:layout_height="match_parent" android:name="com.example.li.fragmentui.RightFragment" android:id="@+id/right" android:layout_weight="1"/> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/framelayout" android:layout_weight="1"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/left_fragment" android:name="com.example.li.fragmentui.leftFragment" /> </FrameLayout></LinearLayout>
0 0
- 静态添加的碎片中相互通信
- 静态添加碎片
- DOTNET开发过程中加入静态的文件碎片
- 碎片到碎片 碎片到活动 之间的通信
- Fragment的使用(静态,动态添加碎片;生命周期以及传值)
- Android开发中如何实现外部其他Activity类与指定fragment碎片的相互跳转
- Android开发中如何实现外部其他Activity类与指定fragment碎片的相互跳转
- Android开发中如何实现外部其他Activity类与指定fragment碎片的相互跳转
- android之碎片(fragment)一(静态添加)
- java中Socket相互通信
- 【Android】22、碎片之间的通信
- Fragment(碎片机制)的静态加载问题
- VC++中进程间相互通信的十一种方法
- VC++中进程间相互通信的十一种方法
- VC++中进程间相互通信的十一种方法
- VC++中进程间相互通信的多种方法
- VC++中进程间相互通信的十一种方法
- VC++中进程间相互通信的十一种方法
- Centos 挂载windows分区 出现mount: unknown filesystem type 'ntfs' 挂载大于2T硬盘
- UICollectionView UITableView 添加滑动动画
- 对于未来的IT计划
- Docker基础: Linux内核命名空间之(4)uts namespace
- Unity中添加组件的几种方法
- 静态添加的碎片中相互通信
- Windows10下Xmanager Enterprise 5 安装
- HTML meta属性中的名词解释
- 神经网络之激活函数(Activation Function)
- 被动扫描神器GourdScan搭建的正确姿势
- c++虚函数
- Hadoop实战演练:搜索数据分析----数据去重 (1)
- 【LeetCode】马三来刷题之Permutations
- 构建WEB前端开发环境