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-20160422-snapshot-bin

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

  2. Python 开发轻量级爬虫01

    Python 开发轻量级爬虫 (imooc总结01--课程目标) 课程目标:掌握开发轻量级爬虫 为什么说是轻量级的呢?因为一个复杂的爬虫需要考虑的问题场景非常多,比如有些网页需要用户登录了以后才能够访 ...

  3. nyoj_299_Matrix Power Series_矩阵快速幂

    Matrix Power Series 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a n × n matrix A and a positive i ...

  4. Mathematics:Ultra-QuickSort(POJ 2299)

    极度快速排序 题目大意:在一个输入数组中找逆序数... 水题,求逆序数的很好的算法,就是MergeSort,和我之前发的DNA那个差不多,最后就是后台数据很大,答案要用long long #inclu ...

  5. python2.x和3.x的区别

    这个星期开始学习Python了,因为看的书都是基于 Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下 3. ...

  6. 【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句

    形式1 REGISTER("SAMPLE.DLL", "MyFunction", "AIC") 形式1等效 VBA语句 Declare Fu ...

  7. September 2nd 2016 Week 36th Friday

    How does the world look through your eyes? 你眼里的世界是什么样子的? How does the world look through your eyes? ...

  8. CABasicAnimation animationWithKeyPath 一些规定的值

    CABasicAnimation animationWithKeyPath Types When using the ‘CABasicAnimation’ from the QuartzCore Fr ...

  9. weblogic重置用户名密码。

    说明:%DOMAIN_HOME%:指WebLogic Server 域(Domain)目录 例如我的做测试的域的根目DOMAIN_HOME=D:\bea\user_projects\domains\b ...

  10. Android Native jni 编程入门

    在某些情况下,java编程已经不能满足我们的需要,比如一个复杂的算法处理,这时候就需要用到jni(java native interface)技术: jni 其实就是java和c/cpp之间进行通信的 ...