2017-06-11 2 views
-1

Ich habe ein Problem mit meiner App.Ich möchte eine App, die WebView hat und die URL ändern, wenn ich auf eines der Elemente in der Navigationsleiste klicken. Zum Beispiel: Facebook Twitter Github und so weiter. Aber ich konnte OnClick-Ereignis nicht implementieren. Ich bin neu in Android Entwicklung. Vielen Dank im Voraus.OnClick Event für Navigation Schubladenelemente

Das ist mein Java Datei

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

} 

}

Das ist mein Main_activity.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: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>

Das ist mein Menü Datei 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

Sie sind nicht wahrscheinlich, Traffic zu bekommen nur einen Haufen von Code zu werfen und von anderen verlangt, etwas zu implementieren für dich. Bitte grenze deine Frage mit bestimmten Dingen ein, die du ausprobiert hast, und Problemen, die du bei der Umsetzung hast. review [Wie stelle ich eine gute Frage?] (https://stackoverflow.com/help/how-to-ask) und [Wie erstelle ich ein minimales, vollständiges und überprüfbares Beispiel] (https://stackoverflow.com/ Hilfe/mcve). –

Antwort

1
  1. Ihre MainActivity Bedürfnisse NavigationView.OnNavigationItemSelectedListener zu implementieren und zu überschreiben onNavigationItemSelected
  2. eine Instanz von NavigationView erstellen (z mNavigationView) und binden Sie es an Ihre Ansicht.
  3. Set Hörer als mNavigationView.setNavigationItemSelectedListener(this);
  4. Aufschalten onNavigationItemSelected()

Hier ist, wie diese Methode aussieht:

@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

_Danke sehr, es funktioniert_ :) –

+0

Gern geschehen :) – Pulak

Verwandte Themen