【问题标题】:On Item Click opens RSS links in webviewOn Item Click 在 webview 中打开 RSS 链接
【发布时间】:2015-01-29 22:37:21
【问题描述】:

在我拥有的一个 RSS 片段中,我认为以下代码指示了在创建 RSS 列表视图中的项目时会发生什么。当一个项目被点击时,它会在 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 活动中打开链接,该活动目前只加载一个网页,google。这是 webview 活动代码:

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 片段

<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 视图布局:

<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>

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

【问题讨论】:

  • Bundle 中传递链接并开始您的网络视图活动。非常基础的android。

标签: android android-fragments webview hyperlink rss


【解决方案1】:

将下面的代码放在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);

它应该工作。我也试过了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2016-03-19
    • 2021-01-07
    相关资源
    最近更新 更多