import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends FragmentActivity {
private LinearLayout mlinearlayout; //装载TextView的布局
private FragmentManager mFragmentManager;
private List<Fragment> mfragmentList; //fragment的集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
init();
//开启事务 将所有Fragment加入布局
mFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = mFragmentManager.beginTransaction();
//处理事务
XinWen_Fragment xinWenFragment= new XinWen_Fragment();
mfragmentList.add(xinWenFragment);
transaction.add(R.id.fragment_layout, xinWenFragment, "XINWEN");
transaction.hide(xinWenFragment); TuPian_Fragment tuPinFragment = new TuPin_Fragment();
mfragmentList.add(tuPinFragment);
transaction.add(R.id.fragment_layout, tuPinFragment, "TUPIN");
transaction.hide(tuPinFragment); ShiPin_Fragment shiPinFragment = new ShiPin_Fragment();
mfragmentList.add(shiPinFragment);
transaction.add(R.id.fragment_layout, shiPinFragment, "SHIPIN");
transaction.hide(shiPinFragment); WoDe_Fragment woDeFragment = new WoDe_Fragment();
mfragmentList.add(woDeFragment);
transaction.add(R.id.fragment_layout,woDeFragment,"WODE");
transaction.hide(woDeFragment);
//展示一个Fragment
transaction.show(xinWenFragment);
//提交事务
transaction.commit(); }
public void init(){
mlinearlayout = (LinearLayout)findViewById(R.id.main_layout);
mfragmentList = new ArrayList<Fragment>();
//设置默认选中的TextView
mlinearlayout.getChildAt(0).setSelected(true);
//外层for循环是为view添加标签
for (int i=0;i<4;i++){
TextView view = (TextView) mlinearlayout.getChildAt(i);
view.setTag(i);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index= (int) v.getTag();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
//内层循环是把Fragment和TextView交互起来
for(int i=0;i<4;i++){
if (i==index){
mlinearlayout.getChildAt(i).setSelected(true);
fragmentTransaction.show(mfragmentList.get(i)) ;
}else{
mlinearlayout.getChildAt(i).setSelected(false);
fragmentTransaction.hide(mfragmentList.get(i)) ;
} }
fragmentTransaction.commit();
}
});
}
}
}

Fragemnt和TextView的交互(TextView在LinearLayout中)的更多相关文章

  1. LinearLayout中组件右对齐

    在LinearLayout中,如果将其定位方向设为横向排列:android:orientation="horizontal",那么这个布局中的控件将自左向右排列. 但有时会有这样的 ...

  2. Android Oreo 8.0 新特性实战 Autosizing TextView --自动缩放TextView

    Android Oreo 8.0 新特性实战 Autosizing TextView --自动缩放TextView 8.0出来很久了,这个新特性已经用了很久了,但是一直没有亲自去试试.这几天新的需求来 ...

  3. Android的LinearLayout中orientation默认值为什么是HORIZONTAL

    在一个偶然(闲着无聊)的过程中,突然非常好奇为什么LinearLayout在不指定排列方向为垂直(VERTICAL)时就得是水平方向(HORIZONTAL)排列的.产生这个疑问的时候脑子里蹦出来的第一 ...

  4. 如何在多个LinearLayout中添加分隔线

      1.可以放置一个ImageView组件,然后将其设为分隔线的颜色或图形.分隔线View的定义代码如下:   2.在Android3.0及以上版本,LinearLayout支持直接显示分隔线. an ...

  5. 我的Android进阶之旅------&gt;怎样在多个LinearLayout中加入分隔线

    假设要适合于全部的Android版本号,能够在多个LinearLayout放置用于显示分隔线的View. 比如,放一个ImageView组件.然后将其背景设为分隔线的颜色或图像,分隔线View的定义代 ...

  6. 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线

    如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...

  7. TextView selector 在LinearLayout中获取焦点问题

    通常需要修改textview被选中时文字颜色,总是没效果,有以下几种方式可以实现: <?xml version="1.0" encoding="utf-8" ...

  8. android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果

    主类:IndexAnimationLinearLayout.java package com.yw.sortlistview; import java.util.ArrayList; import j ...

  9. Android UI: LinearLayout中layout_weight 属性的使用规则

    首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...

随机推荐

  1. ffmpeg-20160510-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  2. java入门 第三季2

    java中的字符串 java中的常用类

  3. ClassLoad的加载过程及分析

    -Xbootclasspath:bootclasspath 让jvm从指定路径(可以是分号分隔的目录.jar.或者zip)中加载bootclass,用来替换jdk的rt.jar:若非必要,一般不会用到 ...

  4. 【leetcode】Single Number II (medium) ★ 自己没做出来....

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  5. validation验证器指定action中某些方法不需要验证

    今天写代码时,遇到个问题,在一个输入数据的页面有一个按钮,单击会发出请求从数据库中取数据,在这里出现问题,单击该按钮,配置的validation起作用,该请求没有到达后台的action 点击按钮选择作 ...

  6. Qt 获取Mac地址

    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces(); foreach(QNetworkInterface ...

  7. July 7th, Week 28th Thursday, 2016

    The 79th Anniversary of Anti-Japan War Difficulties vanish when faced bodly. 勇敢面对困难,困难自会退让. The best ...

  8. rabbitmq_hearbeat

    heartbeat[心跳检测],用于报告客户端与服务器之间连接的一种检测机制.在rabbitmq中有Sender和consumer. 为了确保rabbitmq与客户端的状态,需启用heartbeat. ...

  9. 四、优化及调试--网站优化--SEO在网页制作中的应用

    SEO分类:白帽SEO.黑帽SEO 白帽SEO: 内容上的SEO: 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 前端SEO: 网站结构布局优化 扁平化结构(一 ...

  10. php的错误级别

    error_reporting() 用于设置 PHP 的报错级别并返回当前级别.(PHP 4, PHP 5) 函数能够在运行时设置 error_reporting 指令. PHP 有诸多错误级别,使用 ...