Fragment   实现的 分类 效果

布局文件的信息:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:layout_margin="0dp"
  8. android:background="#fffef9"
  9. tools:context=".MianActivity" >
  10.  
  11. <!-- 1 替换的Fragment的内容! -->
  12. <LinearLayout
  13. android:id="@+id/contentfragment"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:orientation="vertical" >
  18.  
  19. </LinearLayout>
  20.  
  21. <!-- 2 底部的菜单 -->
  22.  
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:gravity="bottom"
  27. android:layout_margin="0dp"
  28. android:layout_gravity="bottom"
  29. android:orientation="horizontal" >
  30.  
  31. <Button
  32. android:id="@+id/articalmenu"
  33. android:layout_width="match_parent"
  34. android:layout_height="match_parent"
  35. android:layout_margin="0dp"
  36. android:layout_weight="1"
  37. android:text="文章"
  38. />
  39.  
  40. <Button
  41. android:id="@+id/forummenu"
  42. android:layout_width="match_parent"
  43. android:layout_height="match_parent"
  44. android:layout_weight="1"
  45. android:layout_margin="0dp"
  46. android:text="论坛"
  47. />
  48.  
  49. <Button
  50. android:id="@+id/gamemenu"
  51. android:layout_width="match_parent"
  52. android:layout_height="match_parent"
  53. android:layout_weight="1"
  54. android:layout_margin="0dp"
  55. android:text="游戏"
  56. />
  57.  
  58. </LinearLayout>
  59.  
  60. </LinearLayout>

Activity  代码:

  1. package stu.love.game;
  2.  
  3. import stu.love.artical.ArticalListFragment;
  4. import stu.love.forum.ForumFragment;
  5. import stu.love.utils.ImageCacheSDUtils;
  6. import android.app.ActivityManager;
  7. import android.content.Context;
  8. import android.graphics.Bitmap;
  9. import android.os.Bundle;
  10. import android.support.v4.app.FragmentActivity;
  11. import android.support.v4.app.FragmentManager;
  12. import android.support.v4.app.FragmentTransaction;
  13. import android.util.Log;
  14. import android.util.LruCache;
  15. import android.view.Menu;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.Window;
  19. import android.widget.Button;
  20.  
  21. import com.android.volley.RequestQueue;
  22. import com.android.volley.toolbox.ImageLoader.ImageCache;
  23. import com.android.volley.toolbox.Volley;
  24.  
  25. /**
  26. * 3DM game 程序的 入口 測试类!
  27.  
  28. *
  29. * */
  30.  
  31. public class MianActivity extends FragmentActivity implements OnClickListener {
  32.  
  33. private static String Tag= "MianActivity";
  34.  
  35. // 菜单选项
  36. private Button articlemenu;
  37. private Button forummenu;
  38. private Button gamemenu;
  39.  
  40. // 图片缓冲
  41. public RequestQueue queue;
  42. public ImageCache imageCache;
  43. public LruCache<String, Bitmap> lruCache;
  44. public Context context;
  45.  
  46. // fragment
  47. private FragmentManager manager;
  48. private FragmentTransaction transaction;
  49. private ArticalListFragment articalListFragment;
  50. private ForumFragment forumFrgment;
  51. private GameFragment gameFragment;
  52.  
  53. public MianActivity() {
  54. super();
  55. }
  56.  
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. getWindow().requestFeature(Window.FEATURE_PROGRESS);
  61. setContentView(R.layout.activity_mian);
  62.  
  63. // 1 初始化组件
  64. initComponet();
  65. // 2 初始化 frgment
  66. manager = getSupportFragmentManager();
  67. initArticalListFragment();
  68.  
  69. }
  70.  
  71. @Override
  72. protected void onResume() {
  73. // TODO Auto-generated method stub
  74. super.onResume();
  75. }
  76.  
  77. /**
  78. * 1 初始化组件
  79. *
  80. * */
  81. private void initComponet() {
  82. articlemenu = (Button) this.findViewById(R.id.articalmenu);
  83. forummenu = (Button) this.findViewById(R.id.forummenu);
  84. gamemenu = (Button) this.findViewById(R.id.gamemenu);
  85. articlemenu.setOnClickListener(this);
  86. forummenu.setOnClickListener(this);
  87. gamemenu.setOnClickListener(this);
  88.  
  89. /**
  90. * 1 开启异步任务 去下载数据!
  91.  
  92. * 使用Volley框架!
  93. * 内部封装好了 异步任务!
  94. * 获取到数据之后 才干 设置 适配器!
  95. 自己定义Adapter
  96. * */
  97. // 获取请求队列
  98. context = getApplicationContext();
  99. queue = Volley.newRequestQueue(context);
  100. //获得系统的动态的剩余内存空间
  101. int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
  102. .getMemoryClass();
  103. //获取剩余的内存空间的/8
  104. final int cacheSize = 1024 * 1024 * memClass / 8;
  105. // 设置缓存的空间大小!
  106.  
  107. lruCache = new LruCache<String, Bitmap>(cacheSize);
  108. // 初始化 图片的缓存!
  109. imageCache = new ImageCache() {
  110. public void putBitmap(String url, Bitmap bitmap) {
  111. // TODO Auto-generated method stub
  112. //设置缓存的路径
  113. Log.i(Tag, "--- 一级缓存 存入图片");
  114. lruCache.put(url, bitmap);
  115.  
  116. if(ImageCacheSDUtils.getInstance().getBitmapData(url)== null)
  117. {
  118. byte[] data = ImageCacheSDUtils.getInstance().BitmapToByte(bitmap);
  119. Log.i(Tag, "--- 二级缓存 存入图片");
  120. ImageCacheSDUtils.getInstance().putBitmapData(url, data);
  121. }
  122. }
  123. public Bitmap getBitmap(String url) {
  124. // TODO Auto-generated method stub
  125. Log.i(Tag, "--- 一级缓存 取出图");
  126. Bitmap bitmap = lruCache.get(url);
  127.  
  128. return bitmap;
  129. }
  130. };
  131. }
  132.  
  133. /**
  134. * 2 事件的响应 替换 Fragment2 事件的响应 替换 Fragment
  135. */
  136. @Override
  137. public void onClick(View v) {
  138. // TODO Auto-generated method stub
  139. transaction = manager.beginTransaction();
  140. switch (v.getId()) {
  141. case R.id.articalmenu:
  142. if (articalListFragment == null)
  143. articalListFragment = new ArticalListFragment(queue, imageCache, lruCache, context);
  144. transaction.replace(R.id.contentfragment, articalListFragment,"articalListFragment");
  145. transaction.addToBackStack("articalListFragment");
  146. break;
  147.  
  148. case R.id.forummenu:
  149. if (forumFrgment == null)
  150. forumFrgment = new ForumFragment();
  151. transaction.replace(R.id.contentfragment, forumFrgment,"forumFrgment");
  152. transaction.addToBackStack("forumFrgment");
  153. break;
  154.  
  155. case R.id.gamemenu:
  156. if (gameFragment == null)
  157. gameFragment = new GameFragment(queue, imageCache, lruCache, context);
  158. transaction.replace(R.id.contentfragment, gameFragment,"gameFragment");
  159. transaction.addToBackStack("gameFragment");
  160. break;
  161. }
  162. // fragment 的替换!
  163. transaction.commit();
  164. }
  165.  
  166. // 3 初始化 Fragment 第一次载入的时候数据!
  167. public void initArticalListFragment()
  168. {
  169. transaction = manager.beginTransaction();
  170. articalListFragment = new ArticalListFragment(queue, imageCache, lruCache, context);
  171. transaction.add(R.id.contentfragment, articalListFragment,"articalListFragment");
  172. transaction.addToBackStack("articalListFragment");
  173. // 由于这里 ,没有 commit 所以 不能show data!
  174. transaction.commit();
  175. }
  176.  
  177. @Override
  178. public boolean onCreateOptionsMenu(Menu menu) {
  179. // Inflate the menu; this adds items to the action bar if it is present.
  180. getMenuInflater().inflate(R.menu.mian, menu);
  181. return true;
  182. }
  183.  
  184. }

Fragment 实现的 分类 效果的更多相关文章

  1. 使用ViewPager+Fragment实现选项卡切换效果

    实现效果 本实例主要实现用ViewPage和Fragment实现选项卡切换效果,选项卡个数为3个,点击选项卡或滑动屏幕会切换Fragment并实现选项卡下方下边框条跟随移动效果. 本程序用androi ...

  2. Android - FragmentTabHost 与 Fragment 制作页面切换效果

    使用 FragmentTabHost 与 Fragment 制作页面切换效果 API 19 TabHost已经不建议使用了.用 FragmentTabHost 来代替TabHost.实际上 Fragm ...

  3. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

  4. python + sklearn ︱分类效果评估——acc、recall、F1、ROC、回归、距离

    之前提到过聚类之后,聚类质量的评价: 聚类︱python实现 六大 分群质量评估指标(兰德系数.互信息.轮廓系数) R语言相关分类效果评估: R语言︱分类器的性能表现评价(混淆矩阵,准确率,召回率,F ...

  5. CNN结构:图片风格分类效果已成(StyleAI)

    CNN结构:图片风格分类效果已成.可以在色彩空间对图片风格进行分类,并进行目标分类. StyleAI构架:FasterRCnn + RandomTrees 为何不使用MaskRCNN? MaskRCN ...

  6. NavigationDrawer+Fragment实现侧滑菜单效果

    学习了NavigationDrawer 官方Support包中的SlidingMenu版本,练了下手.用到了ListView中item不同的布局 以后会升级加上ViewPager和GridView实现 ...

  7. Android下fragment切换的动画效果

    网上看到的例子,转过来记录一下,学习一下,感谢原作者的辛勤编码,效果非常好 基于Android3.0新增的动画api,效果很赞 共21种动画效果: <item>X轴缩放</item& ...

  8. django实现日期分类效果

    日期分类效果图 实现功能:能够按照月份进行分类,统计每个月份的文章数量,没有文章的月份不显示.点击每栏可以链接的当月的文章列表. 每月文章列表可以使用django的通用视图MonthArticleVi ...

  9. Android viewPager+fragment实现滑页效果

    先上图,手指在手机向左或者向右滑就可以实现相应的页面切换. 先看activity_main.xml文件,非常简单,主要是三个标题TextView和viewpager <?xml version= ...

随机推荐

  1. 【Linux】JDK+Eclipse 搭建C/C++开发环境

    注:本文所提供的参考示例是在CentOS Linux环境下的安装,不保证适用于其他版本的Linux系统. ·    安装前的注意事项 编译源代码是需要对应的代码编译工具的,本文中安装的Eclipse只 ...

  2. 如何保证对象线程内唯一:数据槽(CallContext)

    CallContext 是类似于方法调用的线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽.数据槽不在其他逻辑线程上的调用上下文之间共享.当 CallContext 沿执行代码路径 ...

  3. 1.9 Python基础知识 - 数值运算

      一.数值运算 在Python中有丰富的算术运算,这使得Python在科学计算领域有着很高的地位,Python可以提供包括四则运算在内的各种算术运算. 算术运算符 运算符 含义 说明 优先级 实例 ...

  4. mpvue 开发小程序

    转换成vue语法, 小程序中原生的事件用@ 原生的属性用:

  5. nginx 实现跨域

    nginx 添加头部跨域. location / { add_header 'Access-Control-Allow-Origin' '*'; //允许的域 add_header 'Access-C ...

  6. JAVA基础数据类型

    JAVA的数据类型粗略分两种 1.基本数据类型 整数类型: byte,short,int,long 浮点类型: float,double 字符类型: char 布尔类型: boolean 基本语法格式 ...

  7. linux 配置全局jdk环境

    1.在usr/local下新建software文件夹(mkdir software),将下载的jdk和tomcat放在此文件夹下 2.解压 tar -xzvf jdk-8u191-linux-x64. ...

  8. 带你玩转Visual Studio——性能分析与优化

    找到性能瓶颈 二八法则适合很多事物:最重要的只占其中一小部分,约20%,其余80%的尽管是多数,却是次要的.在程序代码中也是一样,决定应用性能的就那20%的代码(甚至更少).因此优化实践中,我们将精力 ...

  9. EBS OAF开发中实体对象和视图对象的属性设置器

    EBS OAF开发中实体对象和视图对象的属性设置器 (版权声明.本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 源文: Home > Oracle ...

  10. ActiveReports 9实战教程(2): 准备数据源(设计时、执行时)

    在上讲中<ActiveReports 9实战教程(1): 手把手搭建好开发环境Visual Studio 2013 社区版>,我们已经结合Visual Studio 2013搭建好了Act ...