You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何点击ListView Fragment中的项后,在含WebView的新Fragment打开链接

实现ListView点击跳转WebView Fragment加载对应链接

嘿,我来帮你搞定这个功能!你已经搭好了ListView的基础展示框架,接下来只需要3个关键步骤就能实现点击网站标题跳转到WebView Fragment并加载对应链接的需求:


1. 创建带WebView的Fragment

首先我们需要一个专门用来展示网页的WebViewFragment,先写它的布局文件fragment_webview.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

然后是WebViewFragment的Java代码,我们通过静态方法传递链接参数:

public class WebViewFragment extends android.support.v4.app.Fragment {
    private static final String ARG_URL = "url";
    private String mTargetUrl;

    // 静态工厂方法,用于传递链接参数
    public static WebViewFragment newInstance(String url) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_URL, url);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTargetUrl = getArguments().getString(ARG_URL);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_webview, container, false);
        WebView webView = view.findViewById(R.id.webview);
        
        // 可选:启用JavaScript(如果目标网站需要)
        webView.getSettings().setJavaScriptEnabled(true);
        
        // 加载指定链接
        if (mTargetUrl != null && !mTargetUrl.isEmpty()) {
            webView.loadUrl(mTargetUrl);
        }
        return view;
    }
}

2. 修改AtlasListFragment的点击事件逻辑

在你已有的AtlasListFragment中,我们需要在onItemClick方法里获取对应位置的链接,然后启动WebViewFragment:

class AtlasListFragment extends android.support.v4.app.ListFragment implements AdapterView.OnItemClickListener {
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        View view = inflater.inflate(R.layout.atlas_list_fragment, container, false); 
        return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
        super.onActivityCreated(savedInstanceState); 
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.tut_titles, android.R.layout.simple_list_item_1); 
        setListAdapter(adapter); 
        getListView().setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
        // 从strings.xml的字符串数组中获取对应位置的链接
        String[] websiteLinks = getResources().getStringArray(R.array.tut_links);
        String targetUrl = websiteLinks[position];
        
        // 创建WebViewFragment并传入链接
        WebViewFragment webFragment = WebViewFragment.newInstance(targetUrl);
        
        // 替换当前Fragment并加入回退栈(让用户能返回列表页)
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.fragmentview, webFragment);
        ft.addToBackStack(null);
        ft.commit();
    }
}

重要提醒:要确保你的strings.xml里已经定义了和tut_titles长度完全匹配的tut_links数组,示例如下:

<string-array name="tut_titles">
    <item>Android开发官网</item>
    <item>Google开发者文档</item>
</string-array>
<string-array name="tut_links">
    <item>https://developer.android.com/</item>
    <item>https://developers.google.com/</item>
</string-array>

3. 修复导航抽屉代码的语法小问题

你的导航抽屉代码里有个小语法错误:if (id == R.id.home) { fragment = frag; 后面缺少闭合的},修正后代码如下:

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    android.support.v4.app.Fragment fragment = null; 
    if (id == R.id.home) { 
        fragment = frag; 
    } else if (id == R.id.settings) { 
        fragment=new Settings(); 
    } else if (id == R.id.about_us) { 
        fragment=new AboutUc(); 
    } else if(id == R.id.atlas){ 
        fragment = new AtlasListFragment(); 
    } else{ 
    } 
    if (fragment!=null) { 
        FragmentManager fragmentManager=getSupportFragmentManager(); 
        FragmentTransaction ft=fragmentManager.beginTransaction(); 
        ft.replace(R.id.fragmentview,fragment); 
        ft.commit(); 
    } 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
}

这样操作后,当你点击ListView里的任意网站标题,就会自动跳转到WebView Fragment并加载对应的目标网站了!

内容的提问来源于stack exchange,提问作者Hosowu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:46:55