Ich habe eine Navigationsschublade in Android gemacht, in der ich onClick für sie implementieren möchte. Das ist meine Haupttätigkeit:onClick Event in der Navigationsleiste
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle aToggle;
private Toolbar toolbar;
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
private RecyclerView.Adapter adapter;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
aToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.navig, R.string.open, R.string.Close);
navigationView = (NavigationView) findViewById(R.id.nav_view);
mDrawerLayout.addDrawerListener(aToggle);
toolbar = (Toolbar) findViewById(R.id.nav_action);
toolbar.setNavigationIcon(R.drawable.navig);
setSupportActionBar(toolbar);
aToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setItemIconTintList(null);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerAdapter = new RecyclerAdapter(getApplicationContext());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerAdapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (aToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}}
Das ist mein XML-Layout für die Aktivität ist:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
tools:context="com.example.alpit.formula2.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="0dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="58dp"
android:orientation="vertical"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.Toolbar
android:id="@+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EF6C00"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark"></android.support.v7.widget.Toolbar>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFA726"
app:menu="@menu/navigation_menu"
app:theme="@style/NavigationTheme">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Das ist mein Menüpunkte:
<group
android:id="@+id/gp1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_maths"
android:icon="@drawable/maths"
android:title="Maths" />
<item
android:id="@+id/nav_physics"
android:icon="@drawable/physics"
android:title="Physics" />
<item
android:id="@+id/nav_chem"
android:icon="@drawable/chem"
android:title="Chemistry" />
<item
android:id="@+id/EEE"
android:icon="@drawable/lightbulb"
android:title="Electronics Electrical" />
</group>
<group
android:id="@+id/gp2"
android:checkableBehavior="single">
<item
android:id="@+id/unitconversion"
android:icon="@drawable/unitconversion"
android:title="Unit Conversion" />
<item
android:id="@+id/Scientist"
android:icon="@drawable/scientist"
android:title="Scientist" />
<item
android:id="@+id/favourite"
android:icon="@drawable/favourite"
android:title="Favourite" />
</group>
<group
android:id="@+id/gp3"
android:checkableBehavior="single">
<item
android:id="@+id/Share"
android:icon="@drawable/share"
android:title="Share" />
<item
android:id="@+id/Rate"
android:icon="@drawable/rate"
android:title="Rate" />
<item
android:id="@+id/ads"
android:icon="@drawable/ad"
android:title="Remove Ads" />
<item
android:id="@+id/aboutus"
android:icon="@drawable/aboutus"
android:title="About Us" />
</group>
</menu>
Das Problem ist, Ich bin nicht in der Lage zu verstehen, wie man die onClick
auf der Navigation implementiert ionenschublade, da sie von der von uns angegebenen Liste ausgefüllt wird, nicht von einer listView.
Wie kann ich onClick
auf den Elementen der Navigationsschublade implementieren?
'wie es durch die bevölkert Liste gegeben von uns nicht von irgendeinem listView 'kannst du erklären, was du damit meinst? – Luciferangel
Sorry, ich weiß, dass diese Zeile Bug, nach der Implementierung der navigationView habe ich viele Tutorial über sie gescannt, wurden sie alle von einer ListView aufgefüllt, durch die sie leicht den Speicherort des Elements geklickt bekommen konnten. Deshalb habe ich diese Zeile hinzugefügt. –