У меня есть базовое действие, которое включает панель инструментов и панель навигации. Все мои действия расширяют базовое действие, чтобы установить заголовок панели инструментов и выполнить некоторые действия с навигационным ящиком.
Все работает нормально, но теперь я хочу использовать привязку представления, но я не могу показать панель инструментов и панель навигации, как раньше.
Вот базовая активность xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" />
<androidx.drawerlayout.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerlayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>
</LinearLayout>
<!--Second child layout-->
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
Вот основная активность
public class BaseActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
NavigationView navigationView;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
Toolbar toolbar = findViewById(R.id.toolbar);
navigationView = findViewById(R.id.navigation_view);
drawerLayout = findViewById(R.id.drawerlayout);
context = this;
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.logout:
logout();
break;
case R.id.home:
Intent intent = new Intent (BaseActivity.this , HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return false;
});
}
}
public void logout() {
//perform action ..
}
Вот как я использовал это базовое действие в других своих активах.
public class HomeActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_home, contentFrameLayout);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.home));
}
Вот что я сделал после использования привязки представления
public class HomeActivity extends BaseActivity {
binding = ActivityHomeBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
}
Я пробовал, как указано ниже, но он не работает Базовая активность расширяет Activity ViewBinding Crash