Не открывается боковое меню после Migrate to AndroidX
В меню "Раздел 2" и "Раздел 3" должны открываться подменю. Перестало работать после выполнения команды в AndroidStudio "Migrate to AndroidX".
package ru.test.navigation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.core.view.MenuItemCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
import java.util.ArrayList;
import java.util.Stack;
import ru.test.navigation.SearchTask.OnResultSearchTaskListener;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnResultSearchTaskListener,
NavigationView.OnNavigationItemSelectedListener {
final static int PADDING_ONE_LEVEL_MENU_ITEM = 30;
final static int DISPLAY_NOT = -1;
final static int DISPLAY_RESULT = 0;
final static int DISPLAY_DOCUMENT = 1;
final static int DISPLAY_BOOKMARKS = 2;
private int nowDisplay = DISPLAY_NOT;
public Settings settings;
public static Document nowDocument;
public BookmarkManager bookmarkManager;
public static Stack<Document> documents;
public ArrayList<NavigationMenuItem> navigationMenuItems;
public LastOpenedManager lastOpenedManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
//getActionBar().setTitle(mTitle);
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
//getActionBar().setTitle(mDrawerTitle);
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggle.syncState();
lastOpenedManager = new LastOpenedManager(getApplicationContext());
}
@Override
protected void onPause() {
if ((nowDocument.type == Document.TYPE_BOOKMARK) ||
(nowDocument.type == Document.TYPE_FINDED) ||
(nowDocument.type == Document.TYPE_OPENED)) {
nowDocument.indexOf = findViewById(R.id.document_content).getScrollY();
}
lastOpenedManager.saveLastOpened(nowDocument);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String textSize = prefs.getString(AppPreferenceActivity.PREFERENCE_TEXT_SIZE, "16");
String navigatorSize = prefs.getString(AppPreferenceActivity.PREFERENCE_NAVIGATION_SIZE, "18");
int textColor = prefs.getInt(AppPreferenceActivity.PREFERENCE_TEXT_COLOR, 0xFF000000);
int backgroundColor = prefs.getInt(AppPreferenceActivity.PREFERENCE_BACKGROUND_COLOR, 0xFFFFFFFF);
settings = new Settings();
settings.textSize = Integer.parseInt(textSize);
settings.navigatorSize = Integer.parseInt(navigatorSize);
settings.textColor = textColor;
settings.backgroundColor = backgroundColor;
RelativeLayout layout = findViewById(R.id.main_background);
layout.setBackgroundColor(settings.backgroundColor);
if (nowDocument == null) {
nowDocument = new Document();
documents = new Stack<>();
Document document = lastOpenedManager.getLastOpened();
if (document != null)
{
Utils.openDocument(this, document.fileName, false, document.indexOf);
}
}
if (nowDocument.type == Document.TYPE_FINDED) {
Utils.openFindedDocument(this, nowDocument.fileName, nowDocument.matchPosTextStart, nowDocument.matchPosTextEnd, false, nowDocument.indexOf);
} else if (nowDocument.type == Document.TYPE_OPENED) {
Utils.openDocument(this, nowDocument.fileName, false, nowDocument.indexOf);
} else if (nowDocument.type == Document.TYPE_NOT) {
Utils.openDocument(this, "n0.html", false, 0);
} else if (nowDocument.type == Document.TYPE_BOOKMARK) {
Utils.openBookmark(this, nowDocument, false, nowDocument.indexOf);
} else if (nowDocument.type == Document.TYPE_BOOKMARK_MENU) {
setDisplay(DISPLAY_BOOKMARKS);
showBookmark(false);
} else if (nowDocument.type == Document.TYPE_RESULTS) {
findViewById(R.id.progress_bar).setVisibility(ProgressBar.VISIBLE);
SearchTask searchTask = new SearchTask(this, this);
searchTask.execute(nowDocument.searchText);
}
bookmarkManager = new BookmarkManager(getApplicationContext());
NavigationView navigationView = findViewById(R.id.navigation_view);
Menu menu = navigationView.getMenu();
menu.removeGroup(0);
navigationMenuCreateData();
navigationMenuCreateGui(menu);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void navigationMenuCreateData() {
navigationMenuItems = new ArrayList<>();
navigationMenuItems.add(new NavigationMenuItem("Раздел 1", "n0.html", 1));
navigationMenuItems.add(new NavigationMenuItem("Раздел 2", "", 1));
navigationMenuItems.add(new NavigationMenuItem("ПодРаздел 2.1", "n1.html", 2));
navigationMenuItems.add(new NavigationMenuItem("ПодРаздел 2.2", "n2.html", 2));
navigationMenuItems.add(new NavigationMenuItem("ПодРаздел 2.3", "n3.html", 2));
navigationMenuItems.add(new NavigationMenuItem("Раздел 3", "", 1));
navigationMenuItems.add(new NavigationMenuItem("ПодРаздел 3.1", "", 2));
navigationMenuItems.add(new NavigationMenuItem("Глава 1", "n4.html", 3));
navigationMenuItems.add(new NavigationMenuItem("Глава 2", "n5.html", 3));
navigationMenuItems.add(new NavigationMenuItem("Глава 3", "n6.html", 3));
navigationMenuItems.add(new NavigationMenuItem("ПодРаздел 3.2", "", 2));
navigationMenuItems.add(new NavigationMenuItem("Глава 4", "n7.html", 3));
navigationMenuItems.add(new NavigationMenuItem("Глава 5", "n8.html", 3));
navigationMenuItems.add(new NavigationMenuItem("Глава 6", "n9.html", 3));
navigationMenuItems.add(new NavigationMenuItem("Раздел 4", "n10.html", 1));
}
private void setMenuItemMultiLine(MenuItem item, String text, int padding) {
final RelativeLayout layout = (RelativeLayout) View.inflate(this, R.layout.custom_menu_item, null);
((TextView) layout.findViewById(R.id.menu_text)).setTextSize(settings.navigatorSize);
((TextView) layout.findViewById(R.id.menu_text)).setText(text);
layout.setPadding(padding, 0, 0, 0);
MenuItemCompat.setActionView(item, layout);
}
public void navigationMenuCreateGui(Menu menu) {
for (int i = 0; i < navigationMenuItems.size(); i++) {
MenuItem menuItem = menu.add(0, i, 0, null);
if (navigationMenuItems.get(i).level > 1) {
menuItem.setVisible(false);
}
setMenuItemMultiLine(menuItem, navigationMenuItems.get(i).name, (navigationMenuItems.get(i).level - 1) * PADDING_ONE_LEVEL_MENU_ITEM);
}
}
public void saveActionInStack(final boolean saveAction) {
if (saveAction) {
documents.push(new Document(nowDocument));
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
NavigationView navigationView = findViewById(R.id.navigation_view);
Menu menu = navigationView.getMenu();
for (int i = 0; i < navigationMenuItems.size(); i++) {
if (id == i) {
NavigationMenuItem navigationMenuItem = navigationMenuItems.get(i);
if (navigationMenuItem.href.equals("")) {
boolean visible = menu.findItem(i + 1).isVisible();
for (int k = i + 1; k < navigationMenuItems.size(); k++) {
if (!visible) {
if (navigationMenuItems.get(k).level == navigationMenuItem.level + 1) {
menu.findItem(k).setVisible(true);
} else if (navigationMenuItems.get(k).level == navigationMenuItem.level) {
break;
}
} else {
if (navigationMenuItems.get(k).level > navigationMenuItem.level) {
menu.findItem(k).setVisible(false);
} else {
break;
}
}
}
} else {
nowDocument.indexOf = findViewById(R.id.document_content).getScrollY();
Utils.openDocument(this, navigationMenuItem.href, true, 0);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
break;
}
}
return true;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:background="@android:color/white"
android:id="@+id/main_background"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@+id/scroll_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layout_results"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/layout_text"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<ScrollView
android:id="@+id/scroll_bookmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layout_bookmarks"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</ViewFlipper>
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
app:theme="@style/NavigationDrawerStyle"
android:id="@+id/navigation_view"
android:fitsSystemWindows="true"
android:textAlignment="gravity">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "ru.test.navigation"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.drawerlayout:drawerlayout:1.1.1'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.rarepebble:colorpicker:1.7.0'
implementation 'com.github.bluejamesbond:textjustify-android:2.1.6'
}
