I want to have a navigation drawer in my app
I have already make this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<View
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#7e7e7e" />
<WebView
android:id="@+id/sitesWebView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1.07" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFDFE"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/webviewBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="@drawable/imageview_selector"
android:padding="5dp"
android:src="@drawable/ic_back" />
<ImageView
android:id="@+id/webviewForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/webviewBack"
android:background="@drawable/imageview_selector"
android:padding="5dp"
android:src="@drawable/ic_forward" />
<ImageView
android:id="@+id/mDrawerToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/imageview_selector"
android:padding="5dp"
android:src="@drawable/ic_drawer" />
</RelativeLayout>
</LinearLayout>
and i want to use this image button
<ImageView
android:id="@+id/mDrawerToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/imageview_selector"
android:padding="5dp"
android:src="@drawable/ic_drawer" />
to be able open the navigation drawer when it's click
I make the fragment
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.webviewBack:
isWebViewCanGoBack();
break;
case R.id.webviewForward:
if (webView.canGoForward())
webView.goForward();
break;
case R.id.webviewReload:
String url = webView.getUrl();
LoadWebViewUrl(url);
break;
case R.id.webviewClose:
finish();
break;
case R.id.mDrawerToggle:
DrawerLayout mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.mDrawerToggle);
final DrawerLayout drawer = (DrawerLayout)getActivity().findViewById(R.id.mDrawerToggle);
drawer.openDrawer(Gravity.LEFT);
return false;
}
}
but i'll get error
error: cannot find symbol method getActivity()
error: incompatible types: unexpected return value
does anyone know how to fix it? what i want is when the imagebutton (mDrawerToggle)
is clicked it'll open navigation drawer
thanks
You cannot write findViewById in fragment to find activity xml views.Instead try this
In you activity make this private memeber virailbe
DrawerLayout mDrawerLayout,drawer;
Then in onCreate
find your drawer using findViewById
.
mDrawerLayout = (DrawerLayout)findViewById(R.id.mDrawerToggle);
drawer = (DrawerLayout)findViewById(R.id.mDrawerToggle);
Make a method to get the reference of DrawerLayout in your activity
public DrawerLayout getDrawerLayout(){
return mDrawerLayout;
}
public DrawerLayout getDrawerLayout(){
return drawer;
}
And in your fragment call your activity method like this
DrawerLayout drawer=((YOURACTIVITY)getActivity()).getDrawerLayout();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.