如何在GridView中单击图标/图像时显示网页?
我有一个包含3个图像/图标的GridView布局。我试图做的是每当一个项目被点击时,应该显示一个网站页面(Webview)。 这里是我做过什么:如何在GridView中单击图标/图像时显示网页?
- 在main.xml中文件中定义的GridView和与LinearLayout中的web视图布局的根元素。
- 在AndroidManifest.xml文件给权限上网
- 写代码的GridView
- 编写代码的网页视图。
- 在GridView中,setOnItemClickListener下,提供的网址为使用使用loadURL()
所以问题来显示的网页:“我怎样才能让我图标/图像将显示一个网页点击?” 我能够在模拟器上看到用户界面。但点击图标时没有回应。我想我的onClickItem()事件做错了。
谢谢你的时间。 我写了下面的代码:
package prototype.wenview.gridview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity
{
// Images to display-----------------------//
Integer[] imageIds = { R.drawable.market, R.drawable.news, R.drawable.weather };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.main); //sets the UI
GridView gridView = (GridView) findViewById(R.id.gridview);// Bring gridView into context from main.xml
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
WebView myWebView = (WebView)findViewById(R.id.webview);
myWebView.loadUrl("http://translate.google.com/translate?sl=auto&tl=hi&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.kvk.pravara.com%2Fweather.html");
}
}
); //setOnItemClickListener declaration ends here.
} //onCreate() ends here.
public class ImageAdapter extends BaseAdapter
{
private Context context; // Context class object - context
public ImageAdapter(Context c)
{
context = c;
}
//return number of images
public int getCount()
{
return imageIds.length;
}
public Object getItem(int position) // return type of getItem method is an Object
{
return position;
}
public long getItemId(int position)
{
return position;
}
//returns an ImageView view--------------
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if(convertView == null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIds[position]);
return imageView;
}
}
}
XML代码:为Activity2.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
/* XML代码。此页面旨在单击网格视图中的图标后显示网页。 */
<?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" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
// Java进行活动2个
包in.niteesh.MultipleActivities代码;
import android.app.Activity; import android.os.Bundle;
public class Activity2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.activity2); //sets the UI
}
}
其中我提到活性2的存在// AndroidManifest.xml文件
<activity android:name=".Activity2" android:label="Activity 2">
<intent-filter>
<action android:name="net.learn2develop.ACTIVITY2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
//在onItemClick下MainActivity类别我写网页是在//第二活动中所示的下面的代码
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String url = "http://google.com/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(new Intent("net.learn2develop.ACTIVITY2"));//reference to start the activity
}
/现在的问题是 - 点击图标后,它切换到Activity2,但没有显示网页。我尝试过各种回合制变通,但无法找到解决/
我有一个解决方案,以打开网页的活性状况中TY:
首先,我改变了布局的某些属性:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
否则的WebView将不可见。
您还必须添加一个类来接管控制整个操作系统:
private class CustomWebViewClient extends WebViewClient {
@Override
/**
* Take control over the loaded url so the url is open in the current Webview and not by the OS browser
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
和你的听众应该是这样的:
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
WebView myWebView = (WebView)findViewById(R.id.webview);
CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
myWebView.setWebViewClient(myCustomWebViewClient);
myWebView.loadUrl("http://www.google.com");
}
希望这是你的期待对于。
编辑:另一种解决方案,这将打开第二个活动内的WebView(应用程序):
我只是做了一些改变你的听众:
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String url;
//The url changes accordingly to the button clicked
switch (position) {
case 0: url = "http://stackoverflow.com";
break;
case 1: url = "http://yahoo.fr";
break;
case 2: url = "http://bing.com";
break;
default: url = "http://google.com";
break;
}
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//used to pass the url to the next activity
intent.putExtra("url", url);
startActivity(intent);//reference to start the activity
}
我的第二个activy:
public class SecondActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.second); //sets the UI
//the webview inside the second activity xml
WebView myWebView = (WebView)findViewById(R.id.webview);
CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
myWebView.setWebViewClient(myCustomWebViewClient);
//get the url from the previous activity
String url = getIntent().getStringExtra("url");
myWebView.loadUrl(url);
}
private class CustomWebViewClient extends WebViewClient {
@Override
/**
* Take control over the loaded url so the url is open in the current Webview and not by the OS browser
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
}
我没有碰到xml文件。
至于第一种解决方案,我们需要重写ordre中的shouldOverrideUrlLoading以在正确的视图中加载url。
非常感谢您的编辑。你的回答非常有帮助。 XST先生,请问我是否希望在其他活动中显示该网页,那么我们是否需要更改以下方法: public void onItemClick(AdapterView > parent,View v,int position,long id) – Niteesh 2012-04-10 11:41:52
通过其他活动,我认为它是默认浏览器,在这种情况下,如@JosephusVillarey所说,您的方法应该如下所示: public void onItemClick(AdapterView > parent,View v,int position,long id) \t { \t \t String url =“http://myawesomeurl.com”; \t \t Intent i = new Intent(Intent.ACTION_VIEW); \t \t i.setData(Uri.parse(url)); \t \t startActivity(i); \t} – XST 2012-04-10 12:05:09
每当我点击第二张图片或第三张图片时,我都会看到相同的页面 - http://microsoft.com页面。我在onItemClick事件中列出了更多网址: myWebView.loadUrl(“http://www.google.com”); myWebView.loadUrl(“http://www.amazon.com”); myWebView.loadUrl(“http://www.microsoft.com”); 我的想法是,如果我点击我的GridView中的第二个图像,我应该得到amazon.com;和第三张图片:microsoft.com。但是,所有三个点击我越来越microsoft.com 我觉得有点混乱。这是为什么发生? – Niteesh 2012-04-10 16:54:37
在你的XML代码的高度的网页视图和网格视图是填补父。 你必须能够加载页面,但GridView控件存在fillparent其占据整个屏幕。
使其包裹的内容。
,也是我认为这是下一个活动更好的负载的WebView它是否符合您的要求。
Agarwal是对的 - 考虑修改你的布局,或者改为在单独的活动中打开网页。
下面就来从URL打开网页代码:
String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
编辑:
要打开一个新的活动链接:在new Intent(this, Activity2.class);
String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(this, Activity2.class);
i.putExtra("key_url", url);
startActivity(i);
this
是您活动的背景。如果这个代码是一个内部类中,使用MyActivity1.this
的活性2,以获得从以前startActivity
代码由意向抛出的URL,
String url = getIntent().getStringExtra("key_url");
那么你可以做任何你想要的网址,像
webView.loadUrl(url);
所有这些在onCreate
您可能还需要添加main.xml代码。正如我所看到的,webview正在加载页面,但它只是在屏幕上不可见。 – josephus 2012-04-07 08:33:14
@JosephusVillarey。我已经添加了XML代码。谢谢。请让我贴出更多需要做的事情。 – Niteesh 2012-04-07 11:34:05