Fragemnt和TextView的交互(TextView在LinearLayout中)
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中)的更多相关文章
- LinearLayout中组件右对齐
在LinearLayout中,如果将其定位方向设为横向排列:android:orientation="horizontal",那么这个布局中的控件将自左向右排列. 但有时会有这样的 ...
- Android Oreo 8.0 新特性实战 Autosizing TextView --自动缩放TextView
Android Oreo 8.0 新特性实战 Autosizing TextView --自动缩放TextView 8.0出来很久了,这个新特性已经用了很久了,但是一直没有亲自去试试.这几天新的需求来 ...
- Android的LinearLayout中orientation默认值为什么是HORIZONTAL
在一个偶然(闲着无聊)的过程中,突然非常好奇为什么LinearLayout在不指定排列方向为垂直(VERTICAL)时就得是水平方向(HORIZONTAL)排列的.产生这个疑问的时候脑子里蹦出来的第一 ...
- 如何在多个LinearLayout中添加分隔线
1.可以放置一个ImageView组件,然后将其设为分隔线的颜色或图形.分隔线View的定义代码如下: 2.在Android3.0及以上版本,LinearLayout支持直接显示分隔线. an ...
- 我的Android进阶之旅------>怎样在多个LinearLayout中加入分隔线
假设要适合于全部的Android版本号,能够在多个LinearLayout放置用于显示分隔线的View. 比如,放一个ImageView组件.然后将其背景设为分隔线的颜色或图像,分隔线View的定义代 ...
- 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线
如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...
- TextView selector 在LinearLayout中获取焦点问题
通常需要修改textview被选中时文字颜色,总是没效果,有以下几种方式可以实现: <?xml version="1.0" encoding="utf-8" ...
- android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果
主类:IndexAnimationLinearLayout.java package com.yw.sortlistview; import java.util.ArrayList; import j ...
- Android UI: LinearLayout中layout_weight 属性的使用规则
首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...
随机推荐
- Silverlight 动画详解
Animation规则 基于时间:你设置动画的初始状态,最终状态,及持续时间,Silverlight会计算帧速率. 作用于属性(properties):一个Silverlight动画只能做一件事情,在 ...
- IOS - 消息推送原理和实现
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...
- Redis事件管理(二)
Redis的定时器是自己实现的,不是很复杂.说说具体的实现吧. 定时器的存储维护采用的是普通的单向链表结构,具体节点定义为: /*时间定时器结构体*/ typedef struct aeTimeEve ...
- python基础——定制类
python基础——定制类 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方 ...
- volatile和const
volatile可理解为“编译器警告指示字” volatile用于告诉编译器必须每次去内存中取变量值 volatile主要修饰可能被多个线程访问的变量 volatile也可以修饰可能被未知因数更改的变 ...
- poj2492(种类并查集/各种解法)
题目链接: http://poj.org/problem?id=2492 题意: 有t组测试数据, 对于每组数据,第一行n, m分别表示昆虫的数目和接下来m行x, y, x, y表示教授判断x, y为 ...
- hdu2030 汉字统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2030 解题思路:主要考察汉字的编码方式, 汉字机内码在计算机的表达方式的描述是,使用二个字节,汉字的每 ...
- C/C++学习笔记---高地址、低地址、大段字节序、小段字节序
字节顺序是指占内存多于一个字节类型的数据在内存中的存放顺序,通常有小端.大端两种字节顺序. 小端字节序指低字节数据存放在内存低地址处,高字节数据存放在内存高地址处: 大端字节序是高字节数据存放在低地址 ...
- OID View
http://oid-info.com/get/1.3.6.1.2.1.17.1.4.1.2
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...