fragment的替换:是指一个Activity加载多个Fragment,当某些动作的时候在Activity替换Fragment显示;

昨天写的这几篇博客,Android-fragment简介-fragment的简单使用Activity-fragment-ListView展示Android-fragment生命周期,都是讲解了在Activity的Layout里面(使用fragment-class去指定加载Fragment)

注意:⚠️ 本篇博客,和昨天写的这几篇博客都是使用 android.app.Fragment 自身的Fragment,不是v4包的,两者是有不同之处的;

在使用Fragment开发过程中,有一个开发技巧:

              1.如果Activity显示的画面,不会切换画面,就直接在Activity-Layout-<fragment 指定加载Fragment

              2.如果Activity显示的画面,会切换,就在Activity-Layout-帧布局占位(把位置占住),然后在Activity代码中去替换fragment(fragment去替换帧布局)


Activity

package liudeli.activity.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; import liudeli.activity.R; public class MyTestFragmentActivity4 extends Activity implements View.OnClickListener { private Button msg;
private Button persons;
private Button my; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_fragment4); initView();
initChangeFragment();
initListener();
} private void initView() {
msg = findViewById(R.id.bt_msg);
persons = findViewById(R.id.bt_persons);
my = findViewById(R.id.bt_my);
} /**
* 初始化默认切换到 消息Fragment
*/
private void initChangeFragment() {
// 得到FragmentManager
android.app.FragmentManager manager = getFragmentManager();
// 开始事务 得到事务
FragmentTransaction fragmentTransaction = manager.beginTransaction();
// 替换操作
fragmentTransaction.replace(R.id.frame_layout, new MsgFragment());
// 提交
fragmentTransaction.commit(); setButton(0);
} private void initListener() {
msg.setOnClickListener(this);
persons.setOnClickListener(this);
my.setOnClickListener(this);
} @Override
public void onClick(View v) {
// 得到FragmentManager
android.app.FragmentManager manager = getFragmentManager();
// 开始事务 得到事务
FragmentTransaction fragmentTransaction = manager.beginTransaction(); Fragment fragment = null; switch (v.getId()) {
case R.id.bt_msg:
fragment = new MsgFragment();
setButton(0);
break;
case R.id.bt_persons:
setButton(1);
fragment = new PersonsFragment();
break;
case R.id.bt_my:
setButton(2);
fragment = new MyWoFragment();
break;
}
// 替换操作
fragmentTransaction.replace(R.id.frame_layout, fragment);
// 提交
fragmentTransaction.commit();
} /**
* 设置三个按钮的颜色
* @param value
*/
private void setButton(int value) {
switch (value) {
case 0:
msg.setTextColor(Color.RED);
persons.setTextColor(Color.BLACK);
my.setTextColor(Color.BLACK);
break;
case 1:
msg.setTextColor(Color.BLACK);
persons.setTextColor(Color.RED);
my.setTextColor(Color.BLACK);
break;
case 2:
msg.setTextColor(Color.BLACK);
persons.setTextColor(Color.BLACK);
my.setTextColor(Color.RED);
break;
} }
}

Activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- 帧布局 下面的LinearLayout已经先填充了,剩下的控件我全部来填充 -->
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
> </FrameLayout> <!-- 我的layout_weight默认为0,我先填充我的控件 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_below="@id/frame_layout"> <Button
android:id="@+id/bt_msg"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="消息"
android:gravity="center"
android:textColor="@android:color/black"
/> <Button
android:id="@+id/bt_persons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="好友"
/> <Button
android:id="@+id/bt_my"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="我的"
/> </LinearLayout> </LinearLayout>

消息的Fragment,MsgFragment

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast; public class MsgFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return new ListView(getActivity()); // Fragment不能使用this
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); final String[] data = new String[]{
"你有一条消息1",
"你有一条消息2",
"你有一条消息3",
"你有一条消息4",
"你有一条消息5",
"你有一条消息6",
"你有一条未读消息6",
"你有一条未读消息7",
"你有一条未读消息8",
}; ListView listView = (ListView)view;
ListAdapter listAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
data);
listView.setAdapter(listAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
}
});
}
}

好友的Fragment,PersonsFragment

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast; public class PersonsFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return new ListView(getActivity()); // Fragment不能使用this
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); final String[] data = new String[]{
"张三",
"李四",
"王五",
"赵六",
"王八",
"朱九",
"厨十",
"阿名",
"雄霸",
}; ListView listView = (ListView)view;
ListAdapter listAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
data);
listView.setAdapter(listAdapter); // ListVIew 设置可以解决,Item长按无反应的问题: android:descendantFocusability="blocksDescendants"
// listView.setDescendantFocusability(2); /*listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
return true;
}
});*/ listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), data[position], Toast.LENGTH_SHORT).show();
}
});
}
}

我的Fragment,MyWoFragment

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView; public class MyWoFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return new GridView(getActivity()); // Fragment不能使用this
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); String[] data = new String[]{
"我的账号",
"我的社交",
"我的简洁",
"我的钱包",
"我的设置",
"退出账号",
"重置账号"
}; GridView gridView = (GridView)view; // 设置三列
gridView.setNumColumns(3); ListAdapter listAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
data);
gridView.setAdapter(listAdapter);
}
}

ListVIew 设置可以解决,Item长按无反应的问题:

效果:

Android-fragment的替换的更多相关文章

  1. Android Fragment 生命周期及其API使用(建议使用自定义View替换Fragment)

    我为什么不主张使用Fragment Fragment:( Fragment就相当于一个有生命周期的View,它的生命周期被所在的Activity的生命周期管理 ) 生命周期回调说明: onAttach ...

  2. Android Fragment 生命周期及其正确使用(建议使用自定义View替换Fragment)

    使用Fragment 官方例子中显示: 例如:一个学生Fragment,需要传入studentId,进行http请求显示,那么setArguments后防止杀掉Fragment后,参数为0,显示不了数 ...

  3. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  4. Android Fragment应用实战,使用碎片向ActivityGroup说再见

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...

  5. Android Fragment 解析和使用

    Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...

  6. Android Fragment 完全解析

    参考文章:http://blog.csdn.net/guolin_blog/article/details/8881711 http://blog.csdn.net/guolin_blog/artic ...

  7. Android Fragment详解

    一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...

  8. Android Fragment 真正的完全解析(上)--转

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...

  9. Xamarin Android Fragment的两种加载方式

    android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...

  10. Android Fragment碎片

    什么是碎片? 碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛.可以把Fragment当成Activity一个界面的一 ...

随机推荐

  1. Swagger与SpringMVC整合

    依赖管理   在整合之前,需要把所有使用到的依赖包全部引入.网上很多文章只是简单告诉读者引入swagger-springmvc-1.0.2.jar包,但是随后你发现这远远不够,还需要很多包,如下所示: ...

  2. Zabbix安装(server和agent)及基本配置

    简介 zabbix([`zæbiks])是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制 ...

  3. C语言实现大数四则运算

    一.简介 众所周知,C语言中INT类型是有限制,不能进行超过其范围的运算,而如果采用float类型进行运算,由于float在内存中特殊的存储形式,又失去了计算的进度.要解决整个问题,一种解决方法是通过 ...

  4. cf-Round542-Div2-C(暴力+DFS)

    题目链接:http://codeforces.com/contest/1130/problem/C 思路: 利用DFS搜索(r1,c1)和(r2,c2)可到达的点的集合,分别存在a1,a2中,若a1= ...

  5. 一些常用的c++系统函数

    数学<cmath><math.h>: 1 三角函数 double sin (double); double cos (double); double tan (double); ...

  6. phpStudy1——PHP文件获取html提交的参数

    示例代码: submit.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  7. 关于"undefined reference"错误

    这个错误换句话说: 链接的时候找不到实现的文件(谨记从这个入手!). 可能导致的原因有: 1. 没有链接库文件,包括静态库或动态库. 2. 链接文件的顺序问题,先后依赖问题,把被依赖的放后面. 3. ...

  8. day8:vcp考试

    Q141. An administrator is unable to upgrade a vCenter Server Appliance from version 5.1 Update 2 to ...

  9. php使用数组语法访问对象

    有一个对象,不过希望能用数组的语法来读写数据,可以使用 实现SPL的ArrayAccess接口来解决. 使用场景:加载配置文件类.larvel框架加载配置文件就这利用数组来操作对象. 数组式访问Obj ...

  10. IIS6.0创建新网站后,浏览显示需输入用户名和密码

    1.首先我们需要创建一个用于匿名访问的账号. 我的电脑右键,电脑管理->本地用户和组->用户->新用户 注意勾选(用户不能更改密码和密码永不过期这两项)   2.右键新创建的用户-& ...