2017-02-17 9 views
6

पर ऑनक्लिक ईवेंट मैंने एंड्रॉइड में नेविगेशन ड्रॉवर बनाया है जिसमें मैं इसके लिए क्लिक करना चाहता हूं।नेविगेशन ड्रॉवर

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

}} 

इस गतिविधि के लिए मेरे एक्सएमएल लेआउट है: यह मेरा मुख्य गतिविधि है

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

समस्या है:

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

यह मेरा मेनू आइटम है मैं समझ नहीं पा रहा हूं कि नेविगेट पर onClick को कैसे कार्यान्वित किया जाए आयन ड्रॉवर क्योंकि यह किसी भी सूची द्वारा हमारे द्वारा दी गई सूची द्वारा पॉप्युलेट किया गया है व्यू।

मैं नेविगेशन ड्रॉवर के आइटम पर onClick कैसे कार्यान्वित कर सकता हूं?

+0

' सूची हमारे द्वारा किसी भी सूची द्वारा नहीं दिया गया है 'क्या आप इस बात से समझा सकते हैं कि इसका क्या अर्थ है? – Luciferangel

+1

क्षमा करें मुझे पता है कि नेविगेशन को लागू करने के बाद यह लाइन बग होगी, मैंने उनके बारे में कई ट्यूटोरियल स्कैन किए हैं, वे सभी एक सूची दृश्य द्वारा पॉप्युलेट किए गए थे, जिससे वे आसानी से आइटम का स्थान प्राप्त कर सकते थे। Thats क्यों मैं इस लाइन को जोड़ा। –

उत्तर

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

क्या आप बता सकते हैं कि इसमें क्लिकक्लिकलिस्ट को कैसे कार्यान्वित किया जाए? –

+0

मैं अपनी पोस्ट संपादित करता हूं और श्रोता – Grzegorz

0

कोड नीचे नेविगेशन मेनू आइटम के लिए श्रोता जोड़ने के लिए

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

और DrawerLayout

करने के लिए टॉगल जोड़ने के लिए है

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

जोड़े NavigationItemSelectedListenernav_view को घटना

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

अपनी गतिविधि को लागू करें नेविगेशन व्यू के साथ।

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

संबंधित मुद्दे