Android Official Demo ActionBarCompat-Styled

ActionBarCompat-Styled Demo download address: https://github.com/googlesamples/android-ActionBarCompat-Styled/#readme

This example demonstrates how to use the custom ActionBarCompat, which contains three paging tags.It is important to note that the paging mode of ActionBar has been abandoned since API 21.

The interface may vary from device to device. Here are some screenshots:

        

 

Steps to achieve:

  1. Create Activity and inherit AppCompatActivity to implement ActionBar.TabListener interface;
  2. Create a menu folder under the res resource folder and a main menu layout xml under the menu folder.
  3. Override the onCreateOptionsMenu() method to create and initialize menus;
  4. Create and initialize Tab paging.

Below is the Demo code, which allows you to page different content by loading fragments.

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class ActionBarStyledActivity extends AppCompatActivity implements ActionBar.TabListener {

    private final String TAG = "ActionBarStyledActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar_styled);
        // Setup Menu Use Tab paging
        ActionBar ab = getSupportActionBar();
        /*
        Set the current navigation mode.There are three navigation modes:
         NAVIGATION_MODE_STANDARD: Standard navigation mode.
         NAVIGATION_MODE_LIST: Label navigation mode.
         NAVIGATION_MODE_TABS: Drop-down list navigation mode.
         */
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Establish Tab paging
        ab.addTab(ab.newTab().setText("Tab 1").setTabListener(this));
        ab.addTab(ab.newTab().setText("Tab 2").setTabListener(this));
        ab.addTab(ab.newTab().setText("Tab 3").setTabListener(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Bind menu layout file, which has two menu subitems defined refresh and settings
        getMenuInflater().inflate(R.menu.styled_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
        // Tab Called when paging is selected.
        Log.i(TAG, tab.getText() + " onTabSelected");
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
        // current Tab Paging is no longer selected and is called when replaced by the next selected page.For example, currently selected Tab1,When you click Tab2 When, Tab1 This method will be triggered, and Tab2 Will trigger onTabSelected.
        Log.i(TAG, tab.getText() + "onTabUnselected");
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
        // This is selected Tab Paging, called when you click again to select the same page.For example, it is already selected Tab1,When you click again Tab1 When, Tab1 This method will be triggered.
        Log.i(TAG, tab.getText() + "onTabReselected");
    }
}

Tags: Android github xml

Posted on Tue, 24 Mar 2020 09:25:01 -0700 by mband2000