2017-06-11 1 views
-1

私のアプリに問題があります.WebViewを持つアプリケーションを作成したいのですが、ナビゲーションドロワーのアイテムの1つをクリックするとURLが変更されます。 Facebook Twitter Github など。onClickイベントを実装できませんでした。私はAndroid開発の新機能です。事前に感謝しています。Navigation DrawerアイテムのOnClickイベント

これは、これは私のメニューファイルmenu.xml

ある

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

} 

}

この私の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>

私のJavaファイルであります

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

あなたは単にコードの束を投げると何かを実装するために他の人を求めて、トラフィックを取得する可能性じゃありませんあなたのために。あなたが試した特定の事柄やそれらを実装している問題について質問を絞り込んでください。レビュー[良い質問をするにはどうすればいいですか](https://stackoverflow.com/help/how-to-ask)と[最小限で完全で検証可能なサンプルを作成する方法](https://stackoverflow.com/ヘルプ/ mcve)。 –

答えて

1
  1. あなたMainActivityニーズNavigationView.OnNavigationItemSelectedListenerを実装し、onNavigationItemSelected
  2. を上書きNavigationViewのインスタンスを作成する(例えば、 mNavigationView)、それをあなたのビューにバインドします。
  3. セットリスナーオーバーライド
  4. mNavigationView.setNavigationItemSelectedListener(this);としてonNavigationItemSelected()
ここ

そのメソッドがどのように見えるかです:

@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

_Thanks非常に、それworks_ :) –

+0

あなたは大歓迎です:) – Pulak

関連する問題