基于Drawer点击的LoadURL WebView
LoadURL WebView based on Drawer click
我是 Android 的初学者,目前正在开发一个概念验证应用程序,该应用程序有一个抽屉,其中包含一些项目,当单击这些项目时,会将某个 URL 加载到 WebView 中,这是一部分的主要布局。单击项目时,我无法加载不同的 URL。我该如何解决?
我正在使用 Mike Penz 的 Material Drawer 库,以下是我的代码:
public class MainActivity extends AppCompatActivity {
private Drawer mDrawer = null;
private AccountHeader headerResult = null;
private WebView mWebView = null;
private TextView mTextView = null;
private WebViewClient wvc = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get TextView
mTextView = (TextView) findViewById(R.id.txtLabel);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Set the WebView
mWebView = (WebView) findViewById(R.id.mWebView);
WebSettings webSettings = mWebView.getSettings();
mWebView.setWebViewClient(wvc);
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://radio.iium.edu.my/v5/");
// Create the header
// Create the AccountHeader
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withCompactStyle(false)
.withHeaderBackground(R.drawable.fmheader)
.withSavedInstance(savedInstanceState)
.build();
mDrawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withName(R.string.drawer_item_stream).withIcon(FontAwesome.Icon.faw_microphone),
new PrimaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_envelope),
new PrimaryDrawerItem().withName(R.string.drawer_item_about).withIcon(FontAwesome.Icon.faw_question),
new SecondaryDrawerItem().withName(R.string.drawer_item_social_section_header).withEnabled(false),
new SecondaryDrawerItem().withName(R.string.drawer_item_facebook).withIcon(FontAwesome.Icon.faw_facebook_official),
new SecondaryDrawerItem().withName(R.string.drawer_item_twitter).withIcon(FontAwesome.Icon.faw_twitter),
new SecondaryDrawerItem().withName(R.string.drawer_item_youtube).withIcon(FontAwesome.Icon.faw_youtube_play)
) // add the items we want to use with our Drawer
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
/*if (drawerItem instanceof Nameable) {
Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
}*/
if (drawerItem != null) {
if (drawerItem.getIdentifier() == 1) {
mTextView.setText("Home");
} else if (drawerItem.getIdentifier() == 2) {
mTextView.setText("Live Stream");
} else if (drawerItem.getIdentifier() == 3) {
mTextView.setText("Facebook");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.facebook.com");
} else if (drawerItem.getIdentifier() == 4) {
mTextView.setText("Twitter");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://twitter.com");
} else if (drawerItem.getIdentifier() == 5) {
mTextView.setText("YouTube");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.youtube.com");
}
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.withShowDrawerOnFirstLaunch(true)
.build();
mDrawer.openDrawer();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = mDrawer.saveInstanceState(outState);
//add the values which need to be saved from the accountHeader to the bundle
outState = headerResult.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (mDrawer != null && mDrawer.isDrawerOpen()) {
mDrawer.closeDrawer();
} else {
super.onBackPressed();
}
}
}
下面是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp" />
<FrameLayout
android:layout_below="@id/toolbar"
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/mWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</WebView>
<TextView
android:id="@+id/txtLabel"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:visibility="gone"
android:textSize="16sp" />
</FrameLayout>
知道我做错了什么吗?
我终于能够通过将 drawerItem.getIdentifier()
替换为 position
来解决这个问题,如下所示:
if (drawerItem != null) {
if (position == 1) {
mTextView.setText("Home");
} else if (position == 2) {
mTextView.setText("Live Stream");
} else if (position == 3) {
mTextView.setText("Facebook");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.facebook.com");
} else if (position == 4) {
mTextView.setText("Twitter");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://twitter.com");
} else if (position == 5) {
mTextView.setText("YouTube");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.youtube.com");
}
}
我是 Android 的初学者,目前正在开发一个概念验证应用程序,该应用程序有一个抽屉,其中包含一些项目,当单击这些项目时,会将某个 URL 加载到 WebView 中,这是一部分的主要布局。单击项目时,我无法加载不同的 URL。我该如何解决?
我正在使用 Mike Penz 的 Material Drawer 库,以下是我的代码:
public class MainActivity extends AppCompatActivity {
private Drawer mDrawer = null;
private AccountHeader headerResult = null;
private WebView mWebView = null;
private TextView mTextView = null;
private WebViewClient wvc = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get TextView
mTextView = (TextView) findViewById(R.id.txtLabel);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Set the WebView
mWebView = (WebView) findViewById(R.id.mWebView);
WebSettings webSettings = mWebView.getSettings();
mWebView.setWebViewClient(wvc);
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://radio.iium.edu.my/v5/");
// Create the header
// Create the AccountHeader
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withCompactStyle(false)
.withHeaderBackground(R.drawable.fmheader)
.withSavedInstance(savedInstanceState)
.build();
mDrawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withName(R.string.drawer_item_stream).withIcon(FontAwesome.Icon.faw_microphone),
new PrimaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_envelope),
new PrimaryDrawerItem().withName(R.string.drawer_item_about).withIcon(FontAwesome.Icon.faw_question),
new SecondaryDrawerItem().withName(R.string.drawer_item_social_section_header).withEnabled(false),
new SecondaryDrawerItem().withName(R.string.drawer_item_facebook).withIcon(FontAwesome.Icon.faw_facebook_official),
new SecondaryDrawerItem().withName(R.string.drawer_item_twitter).withIcon(FontAwesome.Icon.faw_twitter),
new SecondaryDrawerItem().withName(R.string.drawer_item_youtube).withIcon(FontAwesome.Icon.faw_youtube_play)
) // add the items we want to use with our Drawer
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
/*if (drawerItem instanceof Nameable) {
Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
}*/
if (drawerItem != null) {
if (drawerItem.getIdentifier() == 1) {
mTextView.setText("Home");
} else if (drawerItem.getIdentifier() == 2) {
mTextView.setText("Live Stream");
} else if (drawerItem.getIdentifier() == 3) {
mTextView.setText("Facebook");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.facebook.com");
} else if (drawerItem.getIdentifier() == 4) {
mTextView.setText("Twitter");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://twitter.com");
} else if (drawerItem.getIdentifier() == 5) {
mTextView.setText("YouTube");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.youtube.com");
}
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.withShowDrawerOnFirstLaunch(true)
.build();
mDrawer.openDrawer();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = mDrawer.saveInstanceState(outState);
//add the values which need to be saved from the accountHeader to the bundle
outState = headerResult.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (mDrawer != null && mDrawer.isDrawerOpen()) {
mDrawer.closeDrawer();
} else {
super.onBackPressed();
}
}
}
下面是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp" />
<FrameLayout
android:layout_below="@id/toolbar"
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/mWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</WebView>
<TextView
android:id="@+id/txtLabel"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:visibility="gone"
android:textSize="16sp" />
</FrameLayout>
知道我做错了什么吗?
我终于能够通过将 drawerItem.getIdentifier()
替换为 position
来解决这个问题,如下所示:
if (drawerItem != null) {
if (position == 1) {
mTextView.setText("Home");
} else if (position == 2) {
mTextView.setText("Live Stream");
} else if (position == 3) {
mTextView.setText("Facebook");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.facebook.com");
} else if (position == 4) {
mTextView.setText("Twitter");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://twitter.com");
} else if (position == 5) {
mTextView.setText("YouTube");
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("https://www.youtube.com");
}
}