2017-06-11 2 views
-1

J'ai un problème avec mon application.Je veux faire une application qui a WebView et changera l'URL lorsque je clique sur l'un des articles sur le tiroir de navigation.Par exemple: Facebook Twitter Github et ainsi de suite. Mais je ne pouvais pas mettre en œuvre l'événement onClick. Je suis nouveau dans le développement Android. Merci d'avance.Événement OnClick pour les éléments de tiroir de navigation

Ceci est mon fichier Java

public class MainActivity extends AppCompatActivity { 

private DrawerLayout mDrawerLayout; 
private ActionBarDrawerToggle mToggle; 
private Toolbar mToolBar; 
private WebView webView; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mToolBar = (Toolbar) findViewById(R.id.nav_action_bar); 
    setSupportActionBar(mToolBar); 


    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close); 

    mDrawerLayout.addDrawerListener(mToggle); 
    mToggle.syncState(); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


    webView = (WebView) findViewById(R.id.webView); 
    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 

    webView.loadUrl("https://www.facebook.com/groups/276912206003690/"); 
    webView.setWebViewClient(new WebViewClient()); 




} 



@Override 
public boolean onOptionsItemSelected(MenuItem item) { 


    if (mToggle.onOptionsItemSelected(item)){ 
      return true; 
    } 



    return super.onOptionsItemSelected(item); 
} 





@Override 
public void onBackPressed() { 
    if(webView.canGoBack()){ 
     webView.goBack(); 
    }else{ 
     super.onBackPressed(); 
    } 

} 

}

Ce Main_activity.xml mon

<?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:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    tools:context="com.tehedligmail.navigation_drawer.MainActivity" 
 
    android:id="@+id/drawer_layout"> 
 

 

 
<LinearLayout 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical"> 
 

 
    <include layout = "@layout/navigation_action_bar" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content"/> 
 

 
    <WebView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:id="@+id/webView"> 
 
    </WebView> 
 

 
     
 
</LinearLayout> 
 

 
    <android.support.design.widget.NavigationView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     app:menu="@menu/navigation_menu" 
 
     app:headerLayout="@layout/navigation_header" 
 
     android:layout_gravity = "start"> 
 
    </android.support.design.widget.NavigationView> 
 
    
 

 
</android.support.v4.widget.DrawerLayout>

Ceci est mon fichier Menu menu.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
 

 

 
    <item 
 
     android:id="@+id/settings" 
 
     android:icon="@mipmap/ic_launcher" 
 
     android:title="Settings" /> 
 

 
    <item 
 
     android:id="@+id/log_out" 
 
     android:title="Log out" 
 
     android:icon="@mipmap/ic_launcher"/> 
 

 
    <item 
 
     android:id="@+id/google" 
 
     android:title="google" 
 
     android:icon="@mipmap/ic_launcher"/> 
 

 
</menu>

+0

Vous n'êtes pas susceptible d'obtenir du trafic juste jeter un tas de code et demander aux autres de mettre en œuvre quelque chose pour toi. Veuillez affiner votre question avec des choses spécifiques que vous avez essayées et des problèmes que vous rencontrez pour les mettre en œuvre; passez en revue [Comment poser une bonne question] (https://stackoverflow.com/help/how-to-ask) et [Comment créer un exemple minimal, complet et vérifiable] (https://stackoverflow.com/ aide/mcve). –

Répondre

1
  1. Vos MainActivity besoins pour mettre en œuvre NavigationView.OnNavigationItemSelectedListener et redéfinissent onNavigationItemSelected
  2. Créer une instance de NavigationView (par exemple mNavigationView) et liez-le à votre vue.
  3. Set auditeur comme mNavigationView.setNavigationItemSelectedListener(this);
  4. Override onNavigationItemSelected()

Voici comment cette méthode ressemble:

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

    if (id == R.id.settings) { 
     //do something 
    } else if (id == R.id.log_out){ 
     //do something 
    } else if (id == R.id.google) { 
     //do something 
    } 
    mDrawerLayout.closeDrawer(GravityCompat.START); 
    return true; 
} 
+0

_Merci beaucoup, ça marche_ :) –

+0

Vous êtes les bienvenus :) – Pulak