fragment realizes hiding and interface switching effect

  • 2021-10-24 23:46:13
  • OfStack

In the previous effect (Android how to create custom ActionBar), click TextView at the bottom of the screen to switch between five fragment interfaces.

Because the loading of network data exists in different interfaces, when switching interfaces quickly, there will be program errors. Because when switching quickly, the data of the current interface is still being read, you will switch to the next interface, and the next interface will start loading data, and the data will be loaded every time the interface is switched. This leads to an error (in this article, fragment uses the replace () method to load the interface). Therefore, each fragment can be loaded only once to reduce the number of data loads. Of course, caching technology can be used to solve the problem.

In this article, we only use fragment hiding or loading to realize that each interface only loads once. At this time, it is necessary to define one more Fragment variable to act as an intermediate variable to realize the hiding of fragment.

The effect of interface switching above is actually very simple, that is, click on the current TextView to change its color, and the colors of other TextView will all become the same color. At this time, these changes can be encapsulated as a method. Reduce the amount of code.

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];
  //  Of which  firstFragment  It is equivalent to an intermediate variable 
  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;
    //  First loaded  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;
    }
  }
 
  /**
   *  Pass  position  Change the position of the text and  View  Color of 
   * @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"));
      }
    }
  }
 
  /**
   *  Determine whether an interface has been added to save the current state 
   */
  public void switchContent(Fragment from, Fragment to,
               FragmentTransaction transaction) {
 
    if (!to.isAdded()) { //  First judge whether it is add Pass 
 
      transaction.hide(from).add(R.id.frame_layout, to)
          .commit(); //  Hide the current fragment , add Under 1 Arrive Activity Medium 
    } else {
      transaction.hide(from).show(to).commit(); //  Hide the current fragment , shown below 1 A 
    }
 
  }
 
  
}

Related articles: