fragment實(shí)現(xiàn)隱藏及界面切換效果
在前文中的效果中(Android如何創(chuàng)建自定義ActionBar),點(diǎn)擊屏幕下方的 TextView 以此來(lái)實(shí)現(xiàn) 5 種 fragment 界面的切換。
由于網(wǎng)絡(luò)數(shù)據(jù)的加載存在于不同的界面之中,當(dāng)快速的切換界面時(shí),就會(huì)出現(xiàn)程序的出錯(cuò)。因?yàn)榭焖俚那袚Q時(shí),當(dāng)前界面的數(shù)據(jù)還在讀取,就切換到下一個(gè)界面,下一個(gè)界面也開(kāi)始加載數(shù)據(jù),每次界面的切換都會(huì)加載數(shù)據(jù)。這樣就會(huì)出錯(cuò)(在本文中,fragment 是使用 replace() 方法來(lái)加載界面的,)。所以可以使每個(gè) fragment 只加載一次來(lái)減少數(shù)據(jù)的加載次數(shù)。當(dāng)然可以使用緩存技術(shù)來(lái)解決問(wèn)題。
本文中只使用 fragment 的隱藏或者加載來(lái)實(shí)現(xiàn)每個(gè)界面只加載一次。這時(shí)需要多定義一個(gè) Fragment 變量,以充當(dāng)中間的變量,來(lái)實(shí)現(xiàn) fragment 的隱藏。
上文中界面切換的效果,其實(shí)很簡(jiǎn)單,即:點(diǎn)擊當(dāng)前 TextView 使其顏色改變,其他的 TextView 的顏色都變?yōu)橄嗤伾纯伞_@時(shí)可以把這些變化封裝為一個(gè)方法。減少代碼量。
MainActivity.java :
package com.crazy.gemi; import android.app.SearchManager; import android.content.Intent; import android.graphics.Color; import android.provider.SearchRecentSuggestions; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.TextView; import com.crazy.gemi.ui.cheaper.CheaperFragment; import com.crazy.gemi.ui.cheaper.SearchSuggestionSampleProvider; import com.crazy.gemi.ui.favor.FavorFragment; import com.crazy.gemi.ui.more.MoreFragment; import com.crazy.gemi.ui.near.NearFragment; import com.crazy.gemi.ui.pocket.PocketFragment; public class MainActivity extends FragmentActivity implements View.OnClickListener, CheaperFragment.SearchResult{ private TextView[] textView = new TextView[5]; private View[] views = new View[5]; // 其中的 firstFragment 相當(dāng)于是個(gè)中間變量 private Fragment firstFragment, nearFragment, cheaperFragment, favorFragment, pocketFragmnet, moreFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); initFragment(); } private void init() { textView[0] = (TextView)findViewById(R.id.near); textView[1] = (TextView)findViewById(R.id.search_cheaper); textView[2] = (TextView)findViewById(R.id.favor); textView[3] = (TextView)findViewById(R.id.pocket); textView[4] = (TextView)findViewById(R.id.more); views[0] = findViewById(R.id.near_top_line); views[1] = findViewById(R.id.cheaper_top_line); views[2] = findViewById(R.id.favor_top_line); views[3] = findViewById(R.id.pocket_top_line); views[4] = findViewById(R.id.more_top_line); textView[0].setOnClickListener(this); textView[1].setOnClickListener(this); textView[2].setOnClickListener(this); textView[3].setOnClickListener(this); textView[4].setOnClickListener(this); } private void initFragment() { firstFragment = FavorFragment.newInstance(); favorFragment = firstFragment; // 最先加載的 fragment getSupportFragmentManager().beginTransaction(). add(R.id.frame_layout, favorFragment).commit(); textView[2].setTextColor(Color.BLACK); views[2].setBackgroundColor(Color.parseColor("#FF6600")); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.near: // getSupportFragmentManager().beginTransaction(). // replace(R.id.frame_layout, NearFragment.newInstance()).commit(); if(nearFragment==null){ nearFragment= NearFragment.newInstance(); } switchContent(firstFragment, nearFragment, getSupportFragmentManager().beginTransaction()); firstFragment = nearFragment; selectStringAndBackgroundColor(0); break; case R.id.search_cheaper: if(cheaperFragment==null){ cheaperFragment= CheaperFragment.newInstance(); } switchContent(firstFragment, cheaperFragment, getSupportFragmentManager().beginTransaction()); firstFragment = cheaperFragment; selectStringAndBackgroundColor(1); break; case R.id.favor: if(favorFragment==null){ favorFragment= FavorFragment.newInstance(); } switchContent(firstFragment, favorFragment, getSupportFragmentManager().beginTransaction()); firstFragment = favorFragment; selectStringAndBackgroundColor(2); break; case R.id.pocket: if(pocketFragmnet==null){ pocketFragmnet= PocketFragment.newInstance(); } switchContent(firstFragment, pocketFragmnet, getSupportFragmentManager().beginTransaction()); firstFragment = pocketFragmnet; selectStringAndBackgroundColor(3); break; case R.id.more: if(moreFragment==null){ moreFragment= MoreFragment.newInstance(); } switchContent(firstFragment, moreFragment, getSupportFragmentManager().beginTransaction()); firstFragment = moreFragment; selectStringAndBackgroundColor(4); break; } } /** * 通過(guò) position 的位置改變文字和 View 的顏色 * @param position */ private void selectStringAndBackgroundColor(int position){ int sum = textView.length; for (int i = 0; i < sum; i++) { if (position == i) { textView[i].setTextColor(Color.BLACK); views[i].setBackgroundColor(Color.parseColor("#FF6600")); } else { textView[i].setTextColor(Color.GRAY); views[i].setBackgroundColor(Color.parseColor("#f0f0f0")); } } } /** * 判斷是否添加了界面,以保存當(dāng)前狀態(tài) */ public void switchContent(Fragment from, Fragment to, FragmentTransaction transaction) { if (!to.isAdded()) { // 先判斷是否被add過(guò) transaction.hide(from).add(R.id.frame_layout, to) .commit(); // 隱藏當(dāng)前的fragment,add下一個(gè)到Activity中 } else { transaction.hide(from).show(to).commit(); // 隱藏當(dāng)前的fragment,顯示下一個(gè) } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基礎(chǔ)之使用Fragment控制切換多個(gè)頁(yè)面
- Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果
- Android使用Fragment打造萬(wàn)能頁(yè)面切換框架
- Android Fragment中使用SurfaceView切換時(shí)閃一下黑屏的解決辦法
- 一個(gè)Activity中多個(gè)Fragment的切換
- Android中Fragment相互切換間不被回收的實(shí)現(xiàn)方法
- Android fragment實(shí)現(xiàn)多個(gè)頁(yè)面切換效果
- Android中使用TabHost 與 Fragment 制作頁(yè)面切換效果
- Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面效果
- Android開(kāi)發(fā)使用Activity嵌套多個(gè)Fragment實(shí)現(xiàn)橫豎屏切換功能的方法
相關(guān)文章
Android TabLayout實(shí)現(xiàn)京東詳情效果
這篇文章主要為大家詳細(xì)介紹了android TabLayout實(shí)現(xiàn)京東詳情效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android 中通過(guò)ViewDragHelper實(shí)現(xiàn)ListView的Item的側(cè)拉劃出效果
這篇文章主要介紹了 Android 中通過(guò)ViewDragHelper實(shí)現(xiàn)ListView的Item的側(cè)拉劃出效果,需要的朋友可以參考下2017-08-08Android應(yīng)用內(nèi)懸浮窗Activity的簡(jiǎn)單實(shí)現(xiàn)
懸浮窗相信大家應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Android應(yīng)用內(nèi)懸浮窗Activity簡(jiǎn)單實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01Android中使用GridView和ImageViewSwitcher實(shí)現(xiàn)電子相冊(cè)簡(jiǎn)單功能實(shí)例
本篇文章主要介紹了Android中使用GridView和ImageViewSwitcher實(shí)現(xiàn)電子相冊(cè)簡(jiǎn)單功能實(shí)例,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09android開(kāi)發(fā)中常用的Eclipse快捷鍵詳細(xì)整理
android開(kāi)發(fā)中常用的Eclipse快捷鍵詳細(xì)整理方便查找,需要的朋友可以了解下2012-12-12Android ViewFlipper簡(jiǎn)單應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android ViewFlipper簡(jiǎn)單應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01