点击下面不同的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. nyoj 99 单词拼接

    点击打开链接 单词拼接 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串. 前一个单词的结尾应该与下一个单词的道字母相同 ...

  2. 监控RAC中的临时表空间

    it is from metalink:Note:465840.1 1>Monitor the temp space allocation to make sure each instance ...

  3. Redis应用

    一.什么是Redis? Redis是一个高性能的key-value内存数据库. 二.为什么使用Redis? Redis是NoSQL数据库,相比传统关系型数据库,内存数据库读写更快. 三.Redis怎么 ...

  4. JQuery基础教程:选择元素(上)

    jQuery最强大的特性之一就是它能够简化在DOM中选择元素的任务,DOM中的对象网络与家谱有几分类似,当我们提到网络中元素之间的关系时,会使用类似描述家庭关系的术语,比如父元素.子元素,等等.通过一 ...

  5. UITableViewCell之微博篇

    微博篇 本应用所涉及的知识点: 1.UITableView 中的cell 2.模型的创建 3.MJExtension第三方框架的使用 需求分析 1.界面分析 微博界面 界面控件分析: 整个页面 1.不 ...

  6. Android双击Back退出应用

    前言 在app主界面点击back时,通常有以下几种方式进行退出 单击即退出 弹出确认Dialog 再次点击退出程序 个人最喜欢方式3,相比于方式2,不需要再把手指移动到屏幕中央点击Dialog,相比于 ...

  7. 《Head First 设计模式》ch.2 观察者(Observer)模式

    观察者模式 定义了对象之间一对多以来,这样一来,当一个对象改变状态时,它所有的依赖者都会收到通知并自动更新 设计原则-松耦合 松耦合将对象之间的互相依赖降到了最低——只要他们之间的接口仍被遵守 观察者 ...

  8. 测试一个域名DNS查询时间的shell脚本

    脚本内容: #!/bin/bash #目标域名 site=${site:-www.ptesting.com} for((i=1;i<=10000;i++)) do     #COUNTER='e ...

  9. 关于java.lang.String理解中的一些难点

    最近温习java的一些基础知识,发现以往对String对象认识上的一些不足.特汇总如下,主要是帮助记忆,如能对其他朋友有些启发,不胜欣喜. String在JVM中内存驻留问题 JVM的常量区(Cons ...

  10. The str method

    __str__ is a special method name, like __init__, that is supposed to return a string representation ...