gpt4 book ai didi

android - On Item Click 在 webview 中打开 RSS 链接

转载 作者:行者123 更新时间:2023-11-30 02:27:39 25 4
gpt4 key购买 nike

在我拥有的一个 RSS fragment 中,我认为以下代码指示了在创建 RSS ListView 中的项目时会发生什么。单击某个项目时,它会在 chrome 中打开。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RssAdapter adapter = (RssAdapter) parent.getAdapter();
RssItem item = (RssItem) adapter.getItem(position);
Uri uri = Uri.parse(item.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}

我希望在我已经创建的 webview Activity 中打开该链接,该 Activity 当前仅加载一个网页,google。这是 WebView Activity 代码:

public class WebViewActivity extends ActionBarActivity {

WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);

web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://www.google.com");
web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);

}

public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub

view.loadUrl(url);
return true;

}
}

// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, MainActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
@Override
public void onBackPressed() {
startActivity(new Intent().setClass(WebViewActivity.this, MainActivity.class).setData(getIntent().getData()));
return;
}

}

XML 布局:

RSS fragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="yanay.end.TwitterFragment"
android:background="@color/white"
android:paddingLeft="3dp"
android:paddingRight="3dp">

<!-- TODO: Update blank fragment layout -->



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>


<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingBottom="1dp"
android:layout_height="fill_parent"
android:divider="@color/blue1"
android:dividerHeight="3dp"
>
</ListView>

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

</RelativeLayout>


</FrameLayout>

Web View 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>

<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
/>

</RelativeLayout>

编辑:现在点击链接打开网络 Activity ,但我仍然无法弄清楚如何将 url 设置为点击的项目 url。

最佳答案

将下面的代码放在RSSFragment的onItemClick中

String urlval = uri + "";
Intent mIntent = new Intent(getActivity(), WebViewActivity.class);
mIntent.putExtra(WebViewActivity.URL_KEY, urlval);
startActivity(mIntent);

WebViewAcitivity 中的代码

Bundle extras = getIntent().getExtras();
String url = extras.getString(URL_KEY);
web.loadUrl(url);

它应该可以工作。我也试过。

关于android - On Item Click 在 webview 中打开 RSS 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27725810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com