简体   繁体   中英

how to make a webview fragment i can inflate for multiple diff webpages

i have a web-View layout. and i want to open it using a on item click and a list view. the list view has various web pages. also the list view is in its own activity called downloads. i want to be able to inflate the view but depending on position of on item click i would like a different web page to open thanks guys and i hope i explained my self properly

i have tried this on my on item click after Switch(position) in the case statement.

  LayoutInflater li = (LayoutInflater)     
  this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 li.inflate(R.layout.webviewlayout, null);

 DownloadWebNav.getSettings().setJavaScriptEnabled(true);
 DownloadWebNav.loadUrl(LilliPutSdCardUrl);

You have to create one common fragment which will open on itemclick of listview

After this open that fragment on itemclick of listview make sure when you replace the fragment you have to pass one string that contains url for load into webview with setArguments() and in otherside in fragment you have to getArguments() so you will receive link for load into webview.

in fragment onResume() you have to get link and proceed to load in webview

Following is just example as refrance

newsLs.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TrialNewsFragment mFragment = new TrialNewsFragment();
                Bundle bundle = new Bundle();
                Data mNewsData = (Data) parent.getItemAtPosition(position);
                if (MainFragmentActivity.sDefSystemLanguage.equalsIgnoreCase("ar")) {
                    bundle.putString("news_desc", mNewsData.tDesc_ar);
                    bundle.putString("news_title", mNewsData.vTitle_ar);
                } else {

                    bundle.putString("news_desc", mNewsData.tDesc_en);
                    bundle.putString("news_title", mNewsData.vTitle_en);

                }
                bundle.putString("news_start", mNewsData.tCreateDate);
                bundle.putString("news_img", mNewsData.vLogo.original);
                mFragment.setArguments(bundle);
                ((MainFragmentActivity) getActivity()).displayFragmentWithArg(mFragment);
            }
        });

One sample method which will replace fragment.

public void displayFragmentWithArg(Fragment mFragment) {
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.addToBackStack("back");
    transaction.replace(R.id.act_login_main_lContainer, mFragment).commit();
}

And in TrailnewsFragment onResume()

Bundle args = getArguments();
    if (args != null && args.containsKey("news_desc") && !args.equals(""))
        tvNewsDesc.setText(args.getString("news_desc"));
    if (args != null && args.containsKey("news_start") && !args.equals(""))
        tvNewsStartDate.setText(UtilMethods.getFormatedDate(args.getString("news_start"), "yyyy-MM-dd hh:mm:ss", "dd MMM,yyyy"));
    if (args != null && args.containsKey("news_title") && !args.equals(""))
        tvNewsHeading.setText(args.getString("news_title"));
    if (args != null && args.containsKey("news_img") && !args.equals("")) {
        if (args.getString("news_img") != null && !args.getString("news_img").equals(""))
            Picasso.with(getActivity()).load(args.getString("news_img")).placeholder(R.drawable.defaultimage).into(ivNewsImage);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM