Fragment的创建以及与activity的参数传递
点击下面不同的TextView变化不同的Fragment
avtivity与Fragment之间传递消息不能使用构造器传递,用bunder传递
首先写一个含有FrameLayout(这个布局最佳),里面最好没有其他的控件的xml:
<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="com.zzw.testfragment.MainActivity" > <FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:id="@+id/weixin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="军事"
android:textColor="@android:color/holo_red_light"
android:textSize="30sp" /> <TextView
android:id="@+id/tongxinlu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="IT"
android:textColor="@android:color/holo_green_light"
android:textSize="30sp" /> <TextView
android:id="@+id/faxian"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐"
android:textColor="@android:color/holo_orange_light"
android:textSize="30sp" /> <TextView
android:id="@+id/me"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="音乐"
android:textColor="@android:color/holo_blue_light"
android:textSize="30sp" />
</LinearLayout> </LinearLayout>
写一个每一个界面要显示item.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" > <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold" /> </RelativeLayout>
写一个继承Fragment的类:
package com.zzw.testfragment; import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; public class TestFragment extends Fragment {
private static final String TAG = "TestFragment";
int image_id;
String content;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = this.getArguments();
image_id = b.getInt("IMAGE");
content = b.getString("CONTENT");
Log.d(TAG, image_id+"--"+content);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_start, null);
return view;
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) { ImageView image = (ImageView) view.findViewById(R.id.image); image.setImageResource(image_id); TextView textView = (TextView) view.findViewById(R.id.textView); textView.setText(content);
} }
MainActivity:
package com.zzw.testfragment; import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import junit.framework.Test;
import junit.framework.TestResult; public class MainActivity extends Activity implements OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); load(setFragmentData(R.drawable.a, "000")); findViewById(R.id.weixin).setOnClickListener(this);
findViewById(R.id.tongxinlu).setOnClickListener(this);
findViewById(R.id.faxian).setOnClickListener(this);
findViewById(R.id.me).setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.weixin:
load(setFragmentData(R.drawable.a, "000"));
break;
case R.id.tongxinlu:
load(setFragmentData(R.drawable.d, "1111"));
break;
case R.id.faxian:
load(setFragmentData(R.drawable.zzzz, "222"));
break;
case R.id.me:
load(setFragmentData(R.drawable.ic_launcher, "333"));
break;
}
} // 加载Fragment
private void load(Fragment fragment) {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelayout, fragment);
// addToBackStack添加到回退栈,addToBackStack与ft.add(R.id.fragment, new
// MyFragment())效果相当
// ft.addToBackStack("test");
ft.commit();
} // 设置要传递给Fragment的参数
private Fragment setFragmentData(int image_id, String content) {
Fragment f = new TestFragment(); Bundle b = new Bundle();
b.putInt("IMAGE", image_id);
b.putString("CONTENT", content); f.setArguments(b);
return f;
}
}
Fragment的创建以及与activity的参数传递的更多相关文章
- Fragment基础----创建
1,Fragment的目的及应用场景 fragment 是3.0后引入的类,其字面翻译为“碎片”. 目的是将activity划分成许多单元再进行组合,可以根据不同分辨率屏幕,在不同状态下,灵活创建优化 ...
- android studio 2.2.2下fragment的创建和跳转
一,首先,Fragment是android应用中十分重要的一个功能,十分轻量化,也类似于activity一样,是一个个布局,可以相互跳转和传递参数.但是,它运行起来十分流畅,而且易于管理,下面是在学习 ...
- android fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...
- Android成长日记-Fragment的生命周期与Activity通信
1. public void onAttach(Activity activity) 当Fragment被添加到Activity时候会回调这个方法,并且这个方法只会被回调一次 2. public vo ...
- android开发之Fragment加载到一个Activity中
Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...
- Fragment的生命周期和Activity之间的通信以及使用
Fragment通俗来讲就是碎片,不能单独存在,意思就是说必须依附于Activity,一般来说有两种方式把Fragment加到Activity,分为静态,动态. 静态即为右键单击,建立一个Fragme ...
- Android为TV端助力 fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...
- android开发(2):多页面的实现 | Fragment的创建与使用
APP中出现多个页面再常见不过了.使用activity与fragment都能实现多页面,这里使用fragment来实现.延续“知音”这个APP的开发,之前已经创建了底部导航条与mainactivity ...
- Fragment的创建与通信
由于这里涉及到接口回调的问题,所以先来看一看什么是接口回调: 这就好比老板和员工的微妙关系,老板需要员工去工作,员工挣钱了以后还要告诉老板自己挣了多少钱,然后由老板来处理这些钱. 首先创建一个接口: ...
随机推荐
- ASP.Net软件工程师基础(四)
1.接口 (1)接口是一种规范.协议,定义了一组具有各种功能的方法(属性.索引器本质是方法). (2)接口存在的意义:多态.多态的意义:程序可扩展性. (3)接口解决了类的多继承的问题. (4)接口解 ...
- MapReduce从输入文件到Mapper处理之间的过程
1.MapReduce代码入口 FileInputFormat.setInputPaths(job, new Path(input)); //设置MapReduce输入格式 job.waitForCo ...
- vc 取windows系统信息 版本 cpu信息 内存信息 ie版本信息 office版本
头文件: /*! Copyright (C) *---------------------------------------------------------------------------- ...
- 解决error C2059: 语法错误:“::”问题
错误代码提示: >f:\opencv\opencv\build\include\opencv2\flann\lsh_table.h(): error C2059: 语法错误:“::”f:\ope ...
- 浅谈Java的包装类
一.什么是Java包装类 所谓Java包装类,就是将Java中的8种基本数据类型分别包装成为类的形式.包装类与基本数据类型的对应关系如下表所示. 基本数据类型 包装类 byte Byte short ...
- JDK源码分析之集合03LinkedList
一.前言 LinkedList是双向列表,实现方式是使用链表的方式保存元素:除了第一个和最后一个元素外,每一个节点都包含一个指向前一个和指向后一个节点的指针和元素的值.其特点是插入删除效率高,而随机访 ...
- tip浮动提示框
今天工作中碰到要弹出tip浮动提示框,如服务器控件的ToolTip属性. 通过GOOGLE搜到了一个很好用的tip浮动提示框:TipTip jQuery Plugin. 例子如下: <!DOCT ...
- .NET MVC4.0与CA对接
1.改web.confog 2.引用CA提供的 dll 3.在controller层加个方法,记得加上授权认证的特性,获取信息 [Authorize] publi void calogin() { H ...
- rsync安装使用
安装 yum install rsync mkdir /etc/rsyncd cd /etc/rsyncd vi rsyncd.conf pid file = /var/run/rsyncd.pid ...
- chrome 浏览器命令
地址栏中输入如下命令可以得到相应的信息: 命令 作用 备注 chrome://dns/ 查看 Chromium 的DNS预取的域名 chrome://net-internals Capture E ...