在项目点击打开webview中的RSS链接

时间:2014-12-31 19:58:02

标签: android android-fragments webview hyperlink rss

在我拥有的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 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视图布局:

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

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

1 个答案:

答案 0 :(得分: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);

它应该有用。我也试过了。