2017-02-17 68 views
6

我在Android中創建了一個導航抽屜,我想在其中實現onClick。這是我的主要活動:導航抽屜中的onClick事件

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); 

}} 

這是活動我的XML佈局:

<?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> 

這是我的菜單項:

<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> 

的問題是我無法理解如何在導航上實現onClick離子抽屜,因爲它由我們給出的列表填充而不是任何listView。

如何在導航抽屜的物品上實現onClick

+0

'名單給我們不是由任何listView'你能解釋你的意思嗎? – Luciferangel

+1

對不起,我知道這行會bug,實現navigationView後,我掃描了許多關於他們的教程,他們都由listView填充,通過他們可以很容易地得到被點擊的項目的位置。這就是爲什麼我加了這條線。 –

回答

12

你需要添加實現NavigationView.OnNavigationItemSelectedListener

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener 

相加法

@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    // Handle navigation view item clicks here. 
    switch (item.getItemId()) { 

     case R.id.nav_maths: { 
     //do somthing 
      break; 
     } 
    } 
    //close navigation drawer 
    mDrawerLayout.closeDrawer(GravityCompat.START); 
    return true; 
} 

方法來設置監聽

private void setNavigationViewListner() { 
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 
在的onCreate()

調用方法

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setNavigationViewListener() 
} 
+0

你可以告訴如何在這個實現onClickListner? –

+0

我編輯我的文章,並添加方法添加監聽器 – Grzegorz

0

下面的代碼添加切換到DrawerLayout

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
     this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
drawer.setDrawerListener(toggle); 

而要監聽器添加到導航菜單項

implements NavigationView.OnNavigationItemSelectedListener 

並在下面覆蓋方法

@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    if(id == R.id. nav_maths){ 
     //Handle your stuff here 
    } 
} 

添加以下代碼到onCreate方法

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
navigationView.setNavigationItemSelectedListener(this); 
+0

當我做它沒有什麼事情發生。 –

1

添加NavigationItemSelectedListener事件nav_view

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

實現你的活動與NavigationView。OnNavigationItemSelectedListener

並添加點擊項目的代碼

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_camera) { 
     // Handle the camera action 
    } else if (id == R.id.nav_gallery) { 

    } else if (id == R.id.nav_slideshow) { 

    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
+0

我不使用framgents,所以我必須在每個活動中實現此方法,或者有任何其他方式爲它感謝。 –

+0

是的,您必須創建導航視圖並將其寫入每個其他活動。但這不是最佳做法。使用片段它將有助於你實現你的任務 –

+0

對不起,延遲迴復,但沒有任何事情發生時,我這樣做。 –

0

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (aToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 

}} 

加上這些行:因爲它是由填充

switch (item.getItemId()) { 
    case R.id.nav_maths: 
     // your logic here. 
     return true; 
    case R.id.nav_physics: 
     //your logic here 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
-2
fab=(FloatingActionButton)findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
} 
    } 
+0

請爲此代碼的工作原理添加一些上下文。這提高了你的答案的質量! :) – Alex