Fragmetn有关
来源:互联网 发布:如何安装c语言 编辑:程序博客网 时间:2024/06/06 09:04
第一行代码中的基础
静态用一个自定的fragment,rightfragment的xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical"> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is right fragment" /></LinearLayout>rightfragment的代码把之前布局加载进来
public class right_fragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment,container,false); return view; }}MainActivity的xml布局,用定义好的fragment静态引入,注意碎片类名,一定要加上类的包名
<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=".MainActivity"> <fragment android:id="@+id/left_fragment" android:name="com.example.sks.fragmenttest.activity.fragment.left_fragment" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> <fragment android:id="@+id/right_fragment" android:name="com.example.sks.fragmenttest.activity.fragment.right_fragment" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /></LinearLayout>动态添加碎片,left上按钮点击right的fragment换为另一个,记得根布局设为容器
<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=".MainActivity"> <fragment android:id="@+id/left_fragment" android:name="com.example.sks.fragmenttest.activity.fragment.Left_fragment" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> <FrameLayout android:id="@+id/fl_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <fragment android:id="@+id/right_fragment" android:name="com.example.sks.fragmenttest.activity.fragment.Right_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout></LinearLayout>点击跳,跟下面视屏里总结的一样
public void onClick(View v) { Another_right_fragmetn another_right_fragmetn = new Another_right_fragmetn(); getFragmentManager().beginTransaction().replace(R.id.fl_container, another_right_fragmetn).commit();}
视频中的动态添加
**继承一个Fragment是导入support4下的,谨记
把一个activity根布局为FrameLayout,作为一个容器,往里面添加fragment
if(savedInstanceState==null){ getSupportFragmentManager().beginTransaction().add(R.id.container,new FragmentAAA()).commit();}在FragmentAAA()的onCreateView中的view的一个按钮点击按钮跳转到另一个fragment中 getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container,new FragmentBBB()).commit();getFragmentManager().beginTransaction().addToBackStack(null)为点击后退键放回前一页面getFragmentManager().popBackStack(); 为点击按钮主动返回前一页面
fragment为根布局,有两imageview,共三id
给根布局设置监听,如果a图在,显示b,否则显示a 实现点击切换图片
活动与碎片进行通信
在活动中得到碎片,用其中的方法,FragmentManager提供此方法
RightFragment rightFragment = getFragmentManager().findFragmentById(R.id.right_fragment);
在碎片中获得活动实例,可直接调用getActivity()方法获取。有时用到Context也可以用此方法
MainActivity activity = (MainActivity)getActivity();
0 0
- Fragmetn有关
- 5.0系统使用Slidingmenu和Fragmetn冲突
- Android 做fragmetn回退栈测试遇到的问题
- 有关->
- 有关翻译。
- 有关注册码
- 有关接口
- 有关VHDL
- 有关wiki
- 有关codesmith
- 有关结构图
- 有关离职
- 有关基带!!
- 有关Eclipse
- 有关绘图
- 有关JPCAP
- 有关态度
- 有关TagPrefix
- C/C++ 内存分配方式,堆区,栈区,new/delete/malloc/free
- hdu 5635 LCP Array(BC第一题)
- gridview position =0多次调用getView, 如果有异步加载,那会导致程序效率低,并且导致position=0的控件显示有问题
- Jquery中get函数
- ADAM学习之4集群安装(未成功)
- Fragmetn有关
- mac 隐藏管理员账户
- ubuntu里面如何以root身份使用图形界面管理文件?
- LeetCode94——Binary Tree Inorder Traversal
- 12.1 字段数据类型(Field datatypes)
- Android实现随触摸移动而变化的效果
- HDU 5635
- Activity的生命周期全面分析
- CF 604 A Uncowed Forces(强制类型转换会有精度损失)