点击下面不同的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的参数传递的更多相关文章

  1. Fragment基础----创建

    1,Fragment的目的及应用场景 fragment 是3.0后引入的类,其字面翻译为“碎片”. 目的是将activity划分成许多单元再进行组合,可以根据不同分辨率屏幕,在不同状态下,灵活创建优化 ...

  2. android studio 2.2.2下fragment的创建和跳转

    一,首先,Fragment是android应用中十分重要的一个功能,十分轻量化,也类似于activity一样,是一个个布局,可以相互跳转和传递参数.但是,它运行起来十分流畅,而且易于管理,下面是在学习 ...

  3. android fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...

  4. Android成长日记-Fragment的生命周期与Activity通信

    1. public void onAttach(Activity activity) 当Fragment被添加到Activity时候会回调这个方法,并且这个方法只会被回调一次 2. public vo ...

  5. android开发之Fragment加载到一个Activity中

    Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...

  6. Fragment的生命周期和Activity之间的通信以及使用

    Fragment通俗来讲就是碎片,不能单独存在,意思就是说必须依附于Activity,一般来说有两种方式把Fragment加到Activity,分为静态,动态. 静态即为右键单击,建立一个Fragme ...

  7. Android为TV端助力 fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...

  8. android开发(2):多页面的实现 | Fragment的创建与使用

    APP中出现多个页面再常见不过了.使用activity与fragment都能实现多页面,这里使用fragment来实现.延续“知音”这个APP的开发,之前已经创建了底部导航条与mainactivity ...

  9. Fragment的创建与通信

    由于这里涉及到接口回调的问题,所以先来看一看什么是接口回调: 这就好比老板和员工的微妙关系,老板需要员工去工作,员工挣钱了以后还要告诉老板自己挣了多少钱,然后由老板来处理这些钱. 首先创建一个接口: ...

随机推荐

  1. Scala模式匹配语言,java的替代者

    1.建立的JVM之上 2.强大的集合工具类,增强模式匹配 3.函数式编程模型(链式编程模式) 4.线程池与消息机制的增强 5.面向对象,运行在jvm之上

  2. Redis多机功能介绍

    Redis多机功能目的:以单台Redis服务器过渡到多台Redis服务器 Redis单机在生产环境中存在的问题 1.内存容量不足 Redis使用内存来存书数据库中的数据,但是对于一台机器来说,硬件的内 ...

  3. update field

    UPDATE dbo.HotelPolicy  SET HPFactorMark=TB.MarkValue FROM (select HPF.HPFRPolicyId AS ID ,CONVERT(i ...

  4. 慕课网-安卓工程师初养成-2-10 Java中的强制类型转换

    来源:http://www.imooc.com/code/1241 相信小伙伴们也发现了,尽管自动类型转换是很方便的,但并不能满足所有的编程需要. 例如,当程序中需要将 double 型变量的值赋给一 ...

  5. JAVA编写WEB服务器

    一.超文本传输协议  1.1 HTTP请求  1.2 HTTP应答  二.Socket类  三.ServerSocket类  四.Web服务器实例  4.1 HttpServer类  4.2 Requ ...

  6. 重拾HTML(一)

    一.网页组成 HTML:网页的具体内容和结构,由N个标签(节点,元素,标记)组成 CSS:网页的样式,美化网页 JavaScript:网页的交互效果,比如用户鼠标的点击事件作出响应   二.常见的HT ...

  7. socket学习笔记——并发服务器与I/O程序分割客户端

    client.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <u ...

  8. ubuntu解压zip文件乱码问题

    我的zip文件里的内容是有中文名,也有密码,在网上找到几种解决办法只有一种可以,所以在这里记录一下: 首先是安装7zip来解压,7zip的解决办法在这里,但是无法解决我的问题,仍然有乱码问题 最后是在 ...

  9. flask-admin

    初始化 class Admin(app=None, name=None, url=None, subdomain=None, index_view=None, translations_path=No ...

  10. OpenGL函数解析之glLoadIdentity()

    函数原型:void glLoadIdentity(void) 函数说明:调用glLoadIdentity()函数可以恢复初始坐标系,用一个4x4的单位矩阵来代替当前矩阵,实际上就是对当前矩阵进行初始化 ...