В MainActivity я создал DrawerLayout и ActionBar. Есть ли способ использовать тот же Drawer и ActionBar в других действиях без повторного написания того же кода? Вот код, который я использую:
Main. xml:
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:titleTextColor="#FFF"></androidx.appcompat.widget.Toolbar>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimary"
>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homepage_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/main_nav"
android:layout_width="277dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer_menu"></com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
Main Java (Я удалил дополнительный код [Код не на панели действий]):
DrawerLayout drawerLayout;
Toolbar toolbar;
TextView json;
RequestQueue requestQueue;
ActionBarDrawerToggle actionBarDrawerToggle;
private RequestQueue mqueue;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mqueue = Volley.newRequestQueue(this);
setuptools();
setUpToolbar();
}
private void setUpToolbar()
{
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name) ;
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.right_menu,menu);
final MenuItem awesomeMenuItem = menu.findItem(R.id.notification_iconmain);
View awesomeActionView = awesomeMenuItem.getActionView();
awesomeActionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(awesomeMenuItem);
}
});
final TextView not_num = (TextView) menu.findItem(R.id.notification_iconmain).getActionView().findViewById(R.id.hotlist_hot);
String username = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
String server_url = "https://google.com/api/notifications.php?username="+username;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, server_url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject notification_numbers = jsonArray.getJSONObject(i);
String final_num = notification_numbers.getString("notifications");
if(final_num.equals("0")){
}else{
not_num.setVisibility(View.VISIBLE);
not_num.setText(final_num);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
mqueue.add(request);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.notification_iconmain){
Intent intent_noti = new Intent(this, NotificationsPage.class);
this.startActivity(intent_noti);
}
if(id == R.id.user_icon){
Intent intent_user = new Intent(this, Profile_Page.class);
this.startActivity(intent_user);
}
return super.onOptionsItemSelected(item);
}
}
Что делает:
Создает ящик ползунка, панель действий с правым меню и отображает счетчик уведомлений на значке колокольчика, и каждый элемент имеет свой собственный -click event.
Теперь, как я могу использовать один и тот же ящик, панель действий без повторного написания кода в каждом действии.