Fragment classification effect

Source: Internet
Author: User

Fragment classification effect
Classification effect implemented by Fragment

Layout file information:

<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 replace the Fragment content! --> <LinearLayout android: id = "@ + id/contentfragment" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_weight = "1" android: orientation = "vertical"> </LinearLayout> <! -- 2 bottom menu --> <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 = "article"/> <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 = "Forum"/> <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 = "games"/> </LinearLayout>




Activity Code:

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. fragmentTransa Ction; 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;/*** entry test class of the 3DM game program! * **/Public class MianActivity extends FragmentActivity implements OnClickListener {private static String Tag = "MianActivity"; // menu options 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; private FragmentTransa Ction transaction; private ArticalListFragment articalListFragment; private ForumFragment forumFrgment; private GameFragment gameFragment; public MianActivity () {super () ;}@ define void onCreate (Bundle parent) {super. onCreate (savedInstanceState); getWindow (). requestFeature (Window. FEATURE_PROGRESS); setContentView (R. layout. activity_mian); // 1 initialize the component initComponet (); // 2 initialize frgmentmanager = Getsuppfrfragmentmanager (); initArticalListFragment () ;}@ Overrideprotected void onResume () {// TODO Auto-generated method stubsuper. onResume ();}/*** 1 initialize component ***/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); f Orummenu. setOnClickListener (this); gamemenu. setOnClickListener (this);/*** 1 enable the asynchronous task to download data! * Use the Volley framework! * Asynchronous tasks are encapsulated internally! * The adapter can be set only after data is obtained! Custom Adapter ** // GET Request queue context = getApplicationContext (); queue = Volley. newRequestQueue (context); // obtain the system's dynamic residual memory space int memClass = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE )). getMemoryClass (); // get the remaining memory space/8 final int cacheSize = 1024*1024 * memClass/8; // set the cache space size! LruCache = new LruCache <String, Bitmap> (cacheSize); // initialize the image cache! ImageCache = new ImageCache () {public void putBitmap (String url, Bitmap bitmap) {// TODO Auto-generated method stub // set the cache path Log. I (Tag, "--- storing images in the first-level cache"); lruCache. put (url, bitmap); if (ImageCacheSDUtils. getInstance (). getBitmapData (url) = null) {byte [] data = ImageCacheSDUtils. getInstance (). bitmapToByte (bitmap); Log. I (Tag, "--- secondary cache saved to image"); ImageCacheSDUtils. getInstance (). putBitmapData (url, data) ;}} public Bitmap getBitmap (String url) {// TODO Auto-generated method stubLog. I (Tag, "--- level-1 cache fetch image"); Bitmap bitmap = lruCache. get (url); return bitmap; }};}/*** 2 Event Response replace Fragment2 Event Response replace Fragment */@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubtransaction = manager. beginTransaction (); switch (v. getId () {case R. id. articalmenu: if (articalListFragment = null) articalListFragment = n Ew 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"); brea K; 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;} // replace fragment! Transaction. commit () ;}// 3 initialize the data when Fragment is loaded for the first time! Public void initArticalListFragment () {transaction = manager. beginTransaction (); articalListFragment = new ArticalListFragment (queue, imageCache, lruCache, context); transaction. add (R. id. contentfragment, articalListFragment, "articalListFragment"); transaction. addToBackStack ("articalListFragment"); // because there is no commit, you cannot 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 ;}}



How does listfragment in fragment achieve the animation effect?

The two are not the same. Import android. app. Fragment; started at 3.0. He added the previous package to the framework layer of the system.

Role of android fragment

Fragment is flexible and provides a better user experience on a tablet. We suggest you try fragment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.