Fragment的用法
来源:互联网 发布:淘宝店能贷款吗 编辑:程序博客网 时间:2024/05/22 07:59
XML文件
<FrameLayout
android:id="@+id/ft1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fistrl"
android:layout_weight="1">
</FrameLayout>
java文件
public class MainActivity extends FragmentActivity {
private RelativeLayout bt_rl1,bt_rl2,bt_rl3,bt_rl4,top_rlt1;private ImageView bt_iv1,bt_iv2,bt_iv3,bt_iv4;
private TextView bt_tv1,bt_tv2,bt_tv3,bt_tv4,main_tv,main_tvl,main_tvr;
private FragmentHome home;
private FragmentList listl;
private FragmentMydoctor mydoctor;
private FragmentMore more;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initBt();
enabledFalse();
show();
}
//初始化id
public void initBt() {
bt_rl1 = (RelativeLayout) findViewById(R.id.bt_rl1);
bt_rl2 = (RelativeLayout) findViewById(R.id.bt_rl2);
bt_rl3 = (RelativeLayout) findViewById(R.id.bt_rl3);
bt_rl4 = (RelativeLayout) findViewById(R.id.bt_rl4);
top_rlt1 = (RelativeLayout) findViewById(R.id.toprlt1);
bt_iv1 = (ImageView) findViewById(R.id.bt_iv1);
bt_iv2 = (ImageView) findViewById(R.id.bt_iv2);
bt_iv3 = (ImageView) findViewById(R.id.bt_iv3);
bt_iv4 = (ImageView) findViewById(R.id.bt_iv4);
bt_tv1 = (TextView) findViewById(R.id.bt_tv1);
bt_tv2 = (TextView) findViewById(R.id.bt_tv2);
bt_tv3 = (TextView) findViewById(R.id.bt_tv3);
bt_tv4 = (TextView) findViewById(R.id.bt_tv4);
main_tv = (TextView) findViewById(R.id.main_tv);
main_tvl = (TextView) findViewById(R.id.main_tvl);
main_tvr = (TextView) findViewById(R.id.main_tvr);
}
public void show(){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
bt_iv1.setEnabled(true);
bt_tv1.setTextColor(Color.rgb(39,174,96));
main_tv.setText("丁香医生");
if (home == null) {
home = new FragmentHome();
transaction.replace(R.id.ft1, home);
}else{
transaction.replace(R.id.ft1, home);
}
transaction.commit();
}
public void ck1(View view){
top_rlt1.setVisibility(View.VISIBLE);
show();
}
public void ck2(View view){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
top_rlt1.setVisibility(View.GONE);
bt_iv2.setEnabled(true);
bt_tv2.setTextColor(Color.rgb(39,174,96));
main_tv.setText("健康科普");
if (listl == null) {
listl = new FragmentList();
transaction.replace(R.id.ft1, listl);
}else{
transaction.replace(R.id.ft1, listl);
}
transaction.commit();
}
public void ck3(View view){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
top_rlt1.setVisibility(View.GONE);
bt_iv3.setEnabled(true);
bt_tv3.setTextColor(Color.rgb(39,174,96));
main_tv.setText("家人健康");
main_tvl.setText("同步");
main_tvr.setText("编辑");
if (mydoctor == null) {
mydoctor = new FragmentMydoctor();
transaction.replace(R.id.ft1, mydoctor);
}else{
transaction.replace(R.id.ft1, mydoctor);
}
transaction.commit();
}
public void ck4(View view){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
top_rlt1.setVisibility(View.GONE);
bt_iv4.setEnabled(true);
bt_tv4.setTextColor(Color.rgb(39,174,96));
main_tv.setText("更多");
if (more == null) {
more = new FragmentMore();
transaction.replace(R.id.ft1, more);
}else{
transaction.replace(R.id.ft1, more);
}
transaction.commit();
}
//设置一个隐藏所有Fragmnet的方法
public void hideFragment() {
if (home != null) {
transaction.hide(home);
}
if (listl != null) {
transaction.hide(listl);
}
if (mydoctor != null) {
transaction.hide(mydoctor);
}
if (more != null) {
transaction.hide(more);
}
}
//初始化按键
public void enabledFalse(){
bt_iv1.setEnabled(false);
bt_iv2.setEnabled(false);
bt_iv3.setEnabled(false);
bt_iv4.setEnabled(false);
bt_tv1.setTextColor(Color.rgb(153,153,153));
bt_tv2.setTextColor(Color.rgb(153,153,153));
bt_tv3.setTextColor(Color.rgb(153,153,153));
bt_tv4.setTextColor(Color.rgb(153,153,153));
main_tv.setText("");
main_tvl.setText("");
main_tvr.setText("");
}
}
0 0
- Android fragment的用法
- fragment的用法笔记
- -----fragment的用法
- fragment的用法笔记
- fragment的用法笔记
- Fragment的用法
- Fragment的部分用法
- Fragment的简单用法
- 关于Fragment的用法
- Fragment的用法
- Fragment的用法简介
- Fragment的用法
- Fragment的初步用法
- fragment的基本用法
- Fragment的简单用法
- fragment回退栈的用法
- Fragment的用法
- fragment 的简单用法
- Android 6.0 运行时权限处理完全解析
- 温故而知新,now复习一下Foundation中的字符串对象
- Deep Learning(深度学习)之(六)【深度神经网络压缩】Deep Compression (ICLR2016 Best Paper)
- 因为未能找到程序集“Microsoft.SqlServer.Types”版本 10 或更高版本。
- 理解Twisted的Deferred机制(一)
- Fragment的用法
- linux 查看磁盘空间大小
- 阿里云+wordpress搭建个人博客网站【小白专用的图文教程】
- std::function与std::bind 函数指针
- [poj2387] Til the Cows Come Home
- VS2013打开项目提示"Asp.net4.5未在web服务器上注册 "
- 确定学习方向规划自己学习计划
- Java程序员应该了解的10个面向对象设计原则
- js笔记--函数表达式