如图是效果图

当  点击下面 不同 的按钮 进入 不同的界面

其中 要一个 主布局当做容器 , 和3个不同的 布局来对应下面的3个按钮界面

主程序的 代码和布局如下

  1. import android.support.v4.app.Fragment;
  2. import android.support.v4.app.FragmentTransaction;
  3. import android.view.View;
  4. import android.widget.Button;
  5.  
  6. import com.example.huanshop.activity.R;
  7. import com.example.huanshop.activity.main.MyShopFragment;
  8. import com.example.huanshop.base.BaseFragmentActivity;
  9.  
  10. public class ProducteTimeLimit extends BaseFragmentActivity {
  11. private int index;
  12. // 当前fragment的index
  13. private int currentTabIndex;
  14. private TimeLimitShopFragment timeLimitShop;
  15. private TimeLimitQiangShop timeLimitQiangShop;
  16. private MyShopFragment myShopFragment;
  17. private Fragment[] fragments;
  18. private Button[] btnTabs;
  19.  
  20. @Override
  21. protected void setContentView() {
  22. // TODO Auto-generated method stub
  23. setContentView(R.layout.timeshop);
  24. }
  25.  
  26. @Override
  27. protected void setFindViewById() {
  28. // TODO Auto-generated method stub
  29. btnTabs = new Button[3];
  30. btnTabs[0] = (Button) findViewById(R.id.timelimited);
  31. btnTabs[1] = (Button) findViewById(R.id.lastshop);
  32. btnTabs[2] = (Button) findViewById(R.id.brandshop);
  33. // 把第一个设置成默认选中状态
  34. btnTabs[0].setSelected(true);
  35.  
  36. }
  37.  
  38. @Override
  39. protected void setControll() {
  40. // TODO Auto-generated method stub
  41. timeLimitShop = new TimeLimitShopFragment();
  42. timeLimitQiangShop = new TimeLimitQiangShop();
  43. myShopFragment = new MyShopFragment();
  44. fragments = new Fragment[] { timeLimitShop, timeLimitQiangShop,
  45. myShopFragment };
  46. // 添加显示第一个fragment
  47. getSupportFragmentManager().beginTransaction()
  48. .add(R.id.timeshopfragment, timeLimitShop)
  49. .add(R.id.timeshopfragment, timeLimitQiangShop)
  50. .add(R.id.timeshopfragment, myShopFragment)
  51. .hide(timeLimitQiangShop).hide(myShopFragment)
  52. .hide(myShopFragment).show(timeLimitShop).commit();
  53. }
  54.  
  55. @Override
  56. protected void setListener() {
  57. // TODO Auto-generated method stub
  58.  
  59. }
  60.  
  61. public void onTabClicked(View view) {
  62. switch (view.getId()) {
  63. case R.id.timelimited:
  64. index = 0;
  65. break;
  66. case R.id.lastshop:
  67. index = 1;
  68. break;
  69. case R.id.brandshop:
  70. index = 2;
  71. break;
  72. }
  73. if (currentTabIndex != 0) {
  74. btnTabs[0].setSelected(false);
  75. }
  76. if (currentTabIndex != index) {
  77. FragmentTransaction trx = getSupportFragmentManager()
  78. .beginTransaction();
  79. trx.hide(fragments[currentTabIndex]);
  80.  
  81. if (!fragments[index].isAdded()) {
  82. trx.add(R.id.timeshopfragment, fragments[index]);
  83. }
  84. trx.show(fragments[index]).commit();
  85. }
  86. btnTabs[currentTabIndex].setSelected(false);
  87. // 把当前tab设为选中状态
  88. btnTabs[index].setSelected(true);
  89. currentTabIndex = index;
  90. }
  91.  
  92. }

主代码的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. style="@style/all_layout_style" >
  4.  
  5. <LinearLayout
  6. android:id="@+id/timebtton1"
  7. android:layout_width="match_parent"
  8. android:layout_height="60dp"
  9. android:layout_alignParentBottom="true"
  10. android:background="@color/bottom_bar_normal_bg"
  11. android:gravity="center_vertical"
  12. android:orientation="vertical" >
  13.  
  14. <View
  15. android:layout_width="match_parent"
  16. android:layout_height="1dp"
  17. android:background="#999999" />
  18.  
  19. <LinearLayout
  20. android:id="@+id/main_bottom"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:background="@color/bottom_bar_normal_bg"
  24. android:gravity="center_vertical"
  25. android:orientation="horizontal" >
  26.  
  27. <RelativeLayout
  28. android:layout_width="0dp"
  29. android:layout_height="match_parent"
  30. android:layout_weight="1" >
  31.  
  32. <Button
  33. style="@style/main_btn"
  34. android:id="@+id/timelimited"
  35. android:clickable="true"
  36. android:drawableTop="@drawable/tab_home_bg"
  37. android:text="限时购" />
  38. </RelativeLayout>
  39.  
  40. <RelativeLayout
  41. android:layout_width="0dp"
  42. android:layout_height="match_parent"
  43. android:layout_weight="1" >
  44.  
  45. <Button
  46. style="@style/main_btn"
  47. android:id="@+id/lastshop"
  48. android:drawableTop="@drawable/tab_classify_bg"
  49. android:text="最后疯抢" />
  50. </RelativeLayout>
  51.  
  52. <RelativeLayout
  53. android:layout_width="0dp"
  54. android:layout_height="match_parent"
  55. android:layout_weight="1" >
  56.  
  57. <Button
  58. style="@style/main_btn"
  59. android:id="@+id/brandshop"
  60. android:drawableTop="@drawable/tab_classify_bg"
  61. android:text="品牌促销" />
  62. </RelativeLayout>
  63. </LinearLayout>
  64. </LinearLayout>
  65. <!-- -->
  66.  
  67. <RelativeLayout
  68. android:id="@+id/timeshopfragment"
  69. android:layout_width="match_parent"
  70. android:layout_height="match_parent"
  71. android:layout_above="@id/timebtton1" >
  72. </RelativeLayout>
  73.  
  74. </RelativeLayout>

其中3个fragment这里值给出一个

  1. import java.util.List;
  2.  
  3. import com.android.http.LoadControler;
  4. import com.android.http.RequestManager;
  5. import com.android.http.RequestManager.RequestListener;
  6. import com.example.huanshop.activity.R;
  7. import com.example.huanshop.activity.main.HomeFragment;
  8. import com.example.huanshop.adapter.ProducteTimeAdapter;
  9. import com.example.huanshop.entity.Product;
  10. import com.example.huanshop.util.HttpUtil;
  11. import com.example.huanshop.widget.MyListView;
  12. import com.google.gson.Gson;
  13. import com.google.gson.reflect.TypeToken;
  14. import com.nostra13.universalimageloader.core.ImageLoader;
  15. import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  16.  
  17. import android.content.Intent;
  18. import android.os.Bundle;
  19. import android.support.v4.app.Fragment;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.AdapterView;
  24. import android.widget.AdapterView.OnItemClickListener;
  25. import android.widget.Toast;
  26.  
  27. public class TimeLimitQiangShop extends Fragment implements RequestListener {
  28. private MyListView listView;
  29. private ProducteTimeAdapter listAdapter;
  30. private List<Product> products;
  31. private LoadControler load;
  32. private ImageLoader loader;
  33. private String urlString = HttpUtil.BASEURL + "Product/GetByIsSpecial/4";
  34.  
  35. @Override
  36. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  37. Bundle savedInstanceState) {
  38. // TODO Auto-generated method stub
  39. return inflater.inflate(R.layout.producttimeqiang, container, false);
  40. }
  41.  
  42. @Override
  43. public void onActivityCreated(Bundle savedInstanceState) {
  44. super.onActivityCreated(savedInstanceState);
  45. setFindViewById();
  46. setListener();
  47. setControll();
  48. }
  49.  
  50. private void setFindViewById() {
  51. // TODO Auto-generated method stub
  52. loader = ImageLoader.getInstance();
  53. loader.init(ImageLoaderConfiguration.createDefault(getActivity()));
  54. listView = (MyListView) getActivity().findViewById(R.id.timeqiang);
  55. }
  56.  
  57. private void setControll() {
  58. // TODO Auto-generated method stub
  59. load = RequestManager.getInstance().get(urlString, this, 2);
  60. }
  61.  
  62. private void setListener() {
  63. // TODO Auto-generated method stub
  64. listView.setOnItemClickListener(new OnItemClickListener() {
  65.  
  66. @Override
  67. public void onItemClick(AdapterView<?> parent, View view,
  68. int position, long id) {
  69. // TODO Auto-generated method stub
  70. HomeFragment.kucun = products.get(position).getKucun() + "";
  71. HomeFragment.Title = products.get(position).getTitle();
  72. HomeFragment.SmallPic = products.get(position).getSmallPic();
  73. HomeFragment.Contens = products.get(position).getContens();
  74. HomeFragment.Price = products.get(position).getPrice() + "";
  75. HomeFragment.SalePrice = products.get(position).getSalePrice()
  76. + "";
  77. HomeFragment.BuyCount = products.get(position).getBuyCount()
  78. + "";
  79. HomeFragment.producteId = products.get(position).getId() + "";
  80. Intent intent = new Intent(getActivity(),
  81. ProducteDetailsActivity.class);
  82. // intent.putExtra("positon", position);
  83. startActivity(intent);
  84. }
  85. });
  86. }
  87.  
  88. @Override
  89. public void onError(String arg0, String arg1, int arg2) {
  90. // TODO Auto-generated method stub
  91.  
  92. }
  93.  
  94. @Override
  95. public void onRequest() {
  96. // TODO Auto-generated method stub
  97.  
  98. }
  99.  
  100. @Override
  101. public void onSuccess(String arg0, String arg1, int arg2) {
  102. // TODO Auto-generated method stub
  103. switch (arg2) {
  104. case 2:
  105. products = new Gson().fromJson(arg0,
  106. new TypeToken<List<Product>>() {
  107. }.getType());
  108. if (products.size() == 0) {
  109. Toast.makeText(getActivity(), "没有产品", 1000).show();
  110. } else {
  111. listAdapter = new ProducteTimeAdapter(getActivity(), products);
  112. listView.setAdapter(listAdapter);
  113. listAdapter.notifyDataSetChanged();
  114. }
  115. }
  116. }
  117. }

FragmentActivity的简单使用的更多相关文章

  1. 简单研究Loader笔记

    2015-11-11 18:25:34 1. Loader是什么? /** * Static library support version of the framework's {@link and ...

  2. TabLayout 简单使用。

    先上效果图 在使用TabLayout 之前需要导入design包. 我使用的是android studio 只要在build.gradle中加入 compile 'com.android.suppor ...

  3. android FragmentActivity+FragmentTabHost+Fragment框架布局

    这周比较闲,计划系统的学习一下android开发,我本是一名IOS程序员,对手机开发还是有自己的一套思路的, 固这套思路用到我当前学android上了,先选择从Main页面的tabbar部分代码入手, ...

  4. 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现

    TabActivity在Android4.0以后已经被完全弃用,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Acti ...

  5. ☆ fragment和fragmentactivity解析 (转)

    一.为什么要使用Fragment  1.当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一 个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单 ...

  6. 【原创】StickHeaderListView的简单实现,解决footerView问题

    1.前言: 前几天用了GitHub上se.emilsjolander.stickylistheaders这个组件,然后发现这个组件的listview不能添加footerView,加了footer后,滑 ...

  7. Android FragmentActivity+viewpager的使用

    使用场景,打算设计一个“底部菜单栏+其余可滑动的页面”的简单的功能. package com.lanyuweng.mibaby; import android.content.Intent; impo ...

  8. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

  9. Fragment的简单使用

    最近一直有点忙,刚刚看到一个朋友的留言提到Fragment中加载ListView的问题,这里写了一个非常简单的测试,至于对Fragment的增.删.改实现动态布局构建灵活的UI,以后有时间在讨论: M ...

随机推荐

  1. Java ExecutorService 四种线程池

    1.new Thread的弊端 new Thead(new Runnable(){ @Override public void run() { // TODO Auto-generated metho ...

  2. jquery实现下拉菜单

    需要实现的效果如图: <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...

  3. DHCP协议及基本实现原理

    DHCP(Dynamic Host Configuration Protocol):动态主机配置协议. DHCP的优缺点 DHCP服务优点:网络管理员可以验证IP地址和其它配置参数,而不用去检查每个主 ...

  4. 训练DCGAN(pytorch官网版本)

    将pytorch官网的python代码当下来,然后下载好celeba数据集(百度网盘),在代码旁新建celeba文件夹,将解压后的img_align_celeba文件夹放进去,就可以运行代码了. 输出 ...

  5. Convert DataTable to List<T> where Class of List is Dynamic

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  6. UESTC - 1137 数位DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  7. Java中filter内处理重定向遇到的问题

    这是在Java中filter内处理重定向遇到的问题.本意是写一个做URL rewrite 的filter,来重写URL,同时在处理登陆过程中要杀掉当前session,创建新session来代替. 1. ...

  8. sha1.js

    function encodeUTF8(s) { var i, r = [], c, x; for (i = 0; i < s.length; i++) if ((c = s.charCodeA ...

  9. PIE SDK专题制图下屏幕坐标转地图坐标

    1.    功能简介 PIESDK提供了专题制图下鼠标屏幕坐标转地图坐标功能. 2. 功能实现说明 2.1屏幕坐标转地图坐标 此功能用到了IPageLayout.ToMapPoint()方法,它的主要 ...

  10. PIE SDK常用滤波

    1. 算法功能简介 空间域滤波实在图像空间( x. y)对输入图像应用滤波函数(核.模板)来改进输出图像的处理方法,主要包括平滑和锐化处理,强调像素与其周围相邻像素的关系,常用的方法是卷积运算. 空间 ...