导航抽屉项目的OnClick事件

时间:2017-06-11 10:11:45

标签: android onclick navigation drawer navigationview

我的应用有问题。我想创建一个具有WebView的应用,并在点击导航抽屉上的其中一个项目时更改网址。例如: Facebook的 推特 Github上 等等。但我无法实施onClick事件。我是Android开发的新手。提前谢谢。

这是我的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();
    }

}

}

这是我的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>
&#13;
&#13;
&#13;

这是我的菜单文件menu.xml

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

  1. 您的MainActivity需要实施NavigationView.OnNavigationItemSelectedListener并覆盖onNavigationItemSelected
  2. 创建NavigationView的实例(例如mNavigationView)并将其绑定到您的视图。
  3. 将侦听器设置为mNavigationView.setNavigationItemSelectedListener(this);
  4. 覆盖onNavigationItemSelected()
  5. 以下是该方法的外观:

    @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;
    }