Fragment 实现的 分类 效果
布局文件的信息:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="0dp" android:background="#fffef9" tools:context=".MianActivity" > <!-- 1 替换的Fragment的内容! --> <LinearLayout android:id="@+id/contentfragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <!-- 2 底部的菜单 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="bottom" android:layout_margin="0dp" android:layout_gravity="bottom" android:orientation="horizontal" > <Button android:id="@+id/articalmenu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0dp" android:layout_weight="1" android:text="文章" /> <Button android:id="@+id/forummenu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="0dp" android:text="论坛" /> <Button android:id="@+id/gamemenu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="0dp" android:text="游戏" /> </LinearLayout> </LinearLayout>
Activity 代码:
package stu.love.game; import stu.love.artical.ArticalListFragment; import stu.love.forum.ForumFragment; import stu.love.utils.ImageCacheSDUtils; import android.app.ActivityManager; import android.content.Context; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.util.LruCache; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader.ImageCache; import com.android.volley.toolbox.Volley; /** * 3DM game 程序的 入口 測试类!* * */ public class MianActivity extends FragmentActivity implements OnClickListener { private static String Tag= "MianActivity"; // 菜单选项 private Button articlemenu; private Button forummenu; private Button gamemenu; // 图片缓冲 public RequestQueue queue; public ImageCache imageCache; public LruCache<String, Bitmap> lruCache; public Context context; // fragment private FragmentManager manager; private FragmentTransaction transaction; private ArticalListFragment articalListFragment; private ForumFragment forumFrgment; private GameFragment gameFragment; public MianActivity() { super(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.activity_mian); // 1 初始化组件 initComponet(); // 2 初始化 frgment manager = getSupportFragmentManager(); initArticalListFragment(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } /** * 1 初始化组件 * * */ private void initComponet() { articlemenu = (Button) this.findViewById(R.id.articalmenu); forummenu = (Button) this.findViewById(R.id.forummenu); gamemenu = (Button) this.findViewById(R.id.gamemenu); articlemenu.setOnClickListener(this); forummenu.setOnClickListener(this); gamemenu.setOnClickListener(this); /** * 1 开启异步任务 去下载数据!
* 使用Volley框架! * 内部封装好了 异步任务! * 获取到数据之后 才干 设置 适配器! 自己定义Adapter * */ // 获取请求队列 context = getApplicationContext(); queue = Volley.newRequestQueue(context); //获得系统的动态的剩余内存空间 int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)) .getMemoryClass(); //获取剩余的内存空间的/8 final int cacheSize = 1024 * 1024 * memClass / 8; // 设置缓存的空间大小!
lruCache = new LruCache<String, Bitmap>(cacheSize); // 初始化 图片的缓存! imageCache = new ImageCache() { public void putBitmap(String url, Bitmap bitmap) { // TODO Auto-generated method stub //设置缓存的路径 Log.i(Tag, "--- 一级缓存 存入图片"); lruCache.put(url, bitmap); if(ImageCacheSDUtils.getInstance().getBitmapData(url)== null) { byte[] data = ImageCacheSDUtils.getInstance().BitmapToByte(bitmap); Log.i(Tag, "--- 二级缓存 存入图片"); ImageCacheSDUtils.getInstance().putBitmapData(url, data); } } public Bitmap getBitmap(String url) { // TODO Auto-generated method stub Log.i(Tag, "--- 一级缓存 取出图"); Bitmap bitmap = lruCache.get(url); return bitmap; } }; } /** * 2 事件的响应 替换 Fragment2 事件的响应 替换 Fragment */ @Override public void onClick(View v) { // TODO Auto-generated method stub transaction = manager.beginTransaction(); switch (v.getId()) { case R.id.articalmenu: if (articalListFragment == null) articalListFragment = new ArticalListFragment(queue, imageCache, lruCache, context); transaction.replace(R.id.contentfragment, articalListFragment,"articalListFragment"); transaction.addToBackStack("articalListFragment"); break; case R.id.forummenu: if (forumFrgment == null) forumFrgment = new ForumFragment(); transaction.replace(R.id.contentfragment, forumFrgment,"forumFrgment"); transaction.addToBackStack("forumFrgment"); break; case R.id.gamemenu: if (gameFragment == null) gameFragment = new GameFragment(queue, imageCache, lruCache, context); transaction.replace(R.id.contentfragment, gameFragment,"gameFragment"); transaction.addToBackStack("gameFragment"); break; } // fragment 的替换! transaction.commit(); } // 3 初始化 Fragment 第一次载入的时候数据! public void initArticalListFragment() { transaction = manager.beginTransaction(); articalListFragment = new ArticalListFragment(queue, imageCache, lruCache, context); transaction.add(R.id.contentfragment, articalListFragment,"articalListFragment"); transaction.addToBackStack("articalListFragment"); // 由于这里 ,没有 commit 所以 不能show data! transaction.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.mian, menu); return true; } }