Fragment 實現的 分類 效果,fragment分類
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;// fragmentprivate FragmentManager manager;private FragmentTransaction transaction;private ArticalListFragment articalListFragment;private ForumFragment forumFrgment;private GameFragment gameFragment;public MianActivity() {super();}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().requestFeature(Window.FEATURE_PROGRESS);setContentView(R.layout.activity_mian);//1 初始化組件initComponet();//2 初始化 frgmentmanager = getSupportFragmentManager();initArticalListFragment();}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.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();//擷取剩餘的記憶體空間的/8final 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 stubLog.i(Tag, "--- 一級緩衝 取出圖");Bitmap bitmap = lruCache.get(url);return bitmap;}};}/** * 2 事件的響應 替換 Fragment2 事件的響應 替換 Fragment */@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtransaction = 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();}@Overridepublic 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;}}
fragment裡listfragment怎實現動畫效果?
他兩個不太一樣。import android.app.Fragment;是在3.0開始的,他把之前的哪個包有區別的加入到系統的架構層了。
android的fragment的作用
fragment 的有點是靈活,而且在平板裝置上 會有更好的使用者體驗。建議你還是嘗試用fragment來實現吧