SoFunction
Updated on 2025-03-01

fragment realizes hidden and interface switching effects

In the previous effect (How to create a custom ActionBar on Android), click the TextView at the bottom of the screen to switch 5 fragment interfaces.

Since the loading of network data exists in different interfaces, when switching interfaces quickly, program errors will occur. Because during quick switching, the data in the current interface is still being read, so it switches to the next interface, and the next interface also starts to load data, and data will be loaded every time the interface switches. This will make 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 loads of data. Of course, caching technology can be used to solve the problem.

In this article, only fragment hiding or loading is used to achieve that each interface is loaded only once. At this time, you need to define an additional 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 TextViews will all change to the same color. These changes can be encapsulated as a method at this time. Reduce the amount of code.

  :

package ;
 
import ;
import ;
import ;
import ;
import .;
import .;
import ;
import .;
import ;
import ;
 
import ;
import ;
import ;
import ;
import ;
import ;
 
public class MainActivity extends FragmentActivity
    implements , {
 
  private TextView[] textView = new TextView[5];
  private View[] views = new View[5];
  // The firstFragment is equivalent to an intermediate variable  private Fragment firstFragment, nearFragment, cheaperFragment, favorFragment, pocketFragmnet, moreFragment;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
 
    init();
    initFragment();
  }
 
  private void init() {
 
    textView[0] = (TextView)findViewById();
    textView[1] = (TextView)findViewById(.search_cheaper);
    textView[2] = (TextView)findViewById();
    textView[3] = (TextView)findViewById();
    textView[4] = (TextView)findViewById();
 
    views[0] = findViewById(.near_top_line);
    views[1] = findViewById(.cheaper_top_line);
    views[2] = findViewById(.favor_top_line);
    views[3] = findViewById(.pocket_top_line);
    views[4] = findViewById(.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 = firstFragment;
    // The first fragment loaded    getSupportFragmentManager().beginTransaction().
        add(.frame_layout, favorFragment).commit();
    textView[2].setTextColor();
    views[2].setBackgroundColor(("#FF6600"));
  }
 
  @Override
  public void onClick(View v) {
    switch (()) {
      case :
//        getSupportFragmentManager().beginTransaction().
//            replace(.frame_layout, ()).commit();
 
        if(nearFragment==null){
          nearFragment= ();
        }
        switchContent(firstFragment, nearFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = nearFragment;
 
        selectStringAndBackgroundColor(0);
        break;
      case .search_cheaper:
        if(cheaperFragment==null){
          cheaperFragment= ();
        }
        switchContent(firstFragment, cheaperFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = cheaperFragment;
 
        selectStringAndBackgroundColor(1);
        break;
      case :
        if(favorFragment==null){
          favorFragment= ();
        }
        switchContent(firstFragment, favorFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = favorFragment;
 
        selectStringAndBackgroundColor(2);
        break;
      case :
        if(pocketFragmnet==null){
          pocketFragmnet= ();
        }
        switchContent(firstFragment, pocketFragmnet, getSupportFragmentManager().beginTransaction());
        firstFragment = pocketFragmnet;
 
        selectStringAndBackgroundColor(3);
        break;
      case :
        if(moreFragment==null){
          moreFragment= ();
        }
        switchContent(firstFragment, moreFragment, getSupportFragmentManager().beginTransaction());
        firstFragment = moreFragment;
 
        selectStringAndBackgroundColor(4);
        break;
    }
  }
 
  /**
    * Change text and view color through position position
    * @param position
    */
  private void selectStringAndBackgroundColor(int position){
    int sum = ;
    for (int i = 0; i < sum; i++) {
      if (position == i) {
        textView[i].setTextColor();
        views[i].setBackgroundColor(("#FF6600"));
      } else {
        textView[i].setTextColor();
        views[i].setBackgroundColor(("#f0f0f0"));
      }
    }
  }
 
  /**
    * Determine whether the interface has been added to save the current state
    */
  public void switchContent(Fragment from, Fragment to,
               FragmentTransaction transaction) {
 
    if (!()) { // First determine whether it has been added 
      (from).add(.frame_layout, to)
          .commit(); // Hide the current fragment, add the next one to the Activity    } else {
      (from).show(to).commit(); // Hide the current fragment and display the next one    }
 
  }
 
  
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.