简体   繁体   中英

how can I open a webpage in a webview from a fragment?

I have a fragment with name "FragmentA".

I have another "fragment" that is a WebView, with name "ShowWebFragment".

i want make a link on an imageView in FragmentA with url "adobe.com", that be opened in ShowWeb.

I write this code. where i should enter the url?

ImageView myImageView_Start =(ImageView) View.findViewById(R.id.imageView2);
    myImageView_Start.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View V) {

        FragmentManager fragmentManager = getActivity().getFragmentManager();
        FragmentTransaction t = fragmentManager.beginTransaction();
        Fragment myFragment = new ShowWebFragment();
        t.replace(R.id.frame_container, myFragment);
        t.commit();
    }
});

This is the way I had done it in one of my projects:

Fragment Class:

public class EventFragment extends Fragment {

    private Dialog WebDialog;
    private WebView Ticketline;
    private Button btnWeb;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_event, container, false);

        btnWeb = (Button) rootView.findViewById(R.id.btnWeb);
        btnWeb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                WebDialog = new Dialog(getActivity());
                WebDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WebDialog.setContentView(R.layout.fragment_cashier);
                WebDialog.setCancelable(true);                  

                Ticketline = (WebView) WebDialog.findViewById(R.id.ticketline);  
                Ticketline.setWebViewClient(new WebConn());
                Ticketline.setScrollbarFadingEnabled(true);  
                Ticketline.setHorizontalScrollBarEnabled(false);  
                //Ticketline.getSettings().setJavaScriptEnabled(true);
                Ticketline.getSettings().setUserAgentString("Ticketline");  
                Ticketline.loadUrl("//url goes here");

                WebDialog.show();

                Button btnDone = (Button) WebDialog.findViewById(R.id.btnDone);
                btnDone.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        WebDialog.dismiss();
                    }   
                });
            }
        });
        return rootView;
    }

Layout xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_black" >

    <Button
        android:id="@+id/btnWeb"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="117dp"
        android:background="@drawable/checkout"
        android:text="Buy Tickets"
        android:textColor="#000000" />

</RelativeLayout>

Webview xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rl_relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dip"
        android:layout_weight="0.82" >

        <TextView
            android:id="@+id/Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="Buy your Tickets:" />

        <WebView
            android:id="@+id/ticketline"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/Title"
            android:layout_marginTop="5dip" />
    </RelativeLayout>

    <Button
        android:id="@+id/btnDone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Back to App" />

</LinearLayout>

Hope this helps :)

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