fragment實現隱藏及界面切換效果

這篇文章主要爲大家詳細介紹了fragment實現隱藏及界面切換效果,具有一定的參考價值,感興趣的小夥伴們可以參考一下

在前文中的效果中(Android如何創建自定義ActionBar),點擊屏幕下方的 TextView 以此來實現 5 種 fragment 界面的切換。

       由於網絡數據的加載存在於不同的界面之中,當快速的切換界面時,就會出現程序的出錯。因爲快速的切換時,當前界面的數據還在讀取,就切換到下一個界面,下一個界面也開始加載數據,每次界面的切換都會加載數據。這樣就會出錯(在本文中,fragment 是使用 replace() 方法來加載界面的,)。所以可以使每個 fragment 只加載一次來減少數據的加載次數。當然可以使用緩存技術來解決問題。

       本文中只使用 fragment 的隱藏或者加載來實現每個界面只加載一次。這時需要多定義一個 Fragment 變量,以充當中間的變量,來實現 fragment 的隱藏。

       上文中界面切換的效果,其實很簡單,即:點擊當前 TextView 使其顏色改變,其他的 TextView 的顏色都變爲相同顏色即可。這時可以把這些變化封裝爲一個方法。減少代碼量。

 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 相當於是個中間變量
  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;
    }
  }
 
  /**
   * 通過 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"));
      }
    }
  }
 
  /**
   * 判斷是否添加了界面,以保存當前狀態
   */
  public void switchContent(Fragment from, Fragment to,
               FragmentTransaction transaction) {
 
    if (!to.isAdded()) { // 先判斷是否被add過
 
      transaction.hide(from).add(R.id.frame_layout, to)
          .commit(); // 隱藏當前的fragment,add下一個到Activity中
    } else {
      transaction.hide(from).show(to).commit(); // 隱藏當前的fragment,顯示下一個
    }
 
  }
 
  
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章