Android uses setContentView to achieve page conversion effect

  • 2021-12-11 08:57:24
  • OfStack

1 When it comes to page switching in Android, do you only think that startActivity starts another Activity?
In fact, in Android, you can directly use setContentView to achieve similar page conversion effect! The implementation idea is as follows:

Add an Button to the layout of the first Activity to implement the click event Click on the Button, call setContentView, pass in Layout on the second page, and the second page will be displayed There is still 1 Button in the layout of the second page, and its click event is still implemented Click on the Button, call setContentView, pass in Layout on the first page, and the first page will be displayed back

Therefore, it is somewhat similar to nested calls, and the source code is as follows:


public class ExampleActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_page_layout);
    
    Button button = findViewById(R.id.buttonGoToLayout2);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //  Jump to the 2 Pages 
        jumpToLayout2();
      }
    });
  }

  private void jumpToLayout2() {
    //  Set the 2 Layout of pages 
    setContentView(R.layout.layout2);
    Button button2 = findViewById(R.id.buttonGoToLayout1);
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //  In the first 2 On the page, click Button , jump to the 1 Pages 
        jumpToLayout1();
      }
    });
  }

  private void jumpToLayout1() {
    //  Set the 1 Pages d Layout of 
    setContentView(R.layout.main_page_layout);
    Button button = findViewById(R.id.buttonGoToLayout2);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //  Click the number 1 Pages of Button , jump to the 2 Pages 
        jumpToLayout2();
      }
    });
  }
}

The two layout files are as follows:

1. Layout of the first page: main_page_layout. xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is Layout One"
        android:paddingTop="20dp"
        android:textSize="30sp"/>
    <Button
        android:text="Go to Layout Two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonGoToLayout2"
        android:layout_marginTop="20dp"
        android:layout_below="@id/textView1"/>
</RelativeLayout>

2. Layout of the second page: layout2.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@android:color/black" >
  <TextView
      android:id="@+id/textView2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is Layout Two"
      android:paddingTop="20dp"
      android:textColor="@android:color/white"
      android:textSize="30sp"/>
  <Button
      android:text="Go to Layout One"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/buttonGoToLayout1"
      android:layout_marginTop="20dp"
      android:layout_below="@id/textView2"/>
</RelativeLayout>

Page switching through setContentView has a special advantage over Activity switching:

Variables in all programs have the same state: class member variables, class functions, etc., which can be obtained directly in the same Activity without the problem of parameter passing. For example:

Layout1 collects payment information such as bank card number entered by users, clicks "Next Step" to enter Layout2 to display order information and let users confirm. After clicking "Confirm" button, users enter Layout3 to authorize payment, and there is no variable transmission in the whole process.

The above is Android using setContentView page conversion effect details, more information about Android page conversion effect please pay attention to other related articles on this site!


Related articles: