How to make tabs with webview to load from a web address?

懵懂的女人 提交于 2020-02-02 06:05:12

问题


I want to build an application in Android Studio. I have inside tabs:

| tab one | tab two | tab three |

...and each tab should load a webview page.

For example: tab one should load www.google.com, and tab two should load www.youtube.com, ...

How can I build this?


回答1:


Here It Is

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;

public class MainActivity extends Activity {

// Declaring our tabs and the corresponding fragments.
ActionBar.Tab bmwTab, fordTab, toyotaTab;
Fragment bmwFragmentTab = new Fragment();
Fragment toyotaFragmentTab = new Fragment();
Fragment fordFragmentTab = new Fragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Asking for the default ActionBar element that our platform supports.
    ActionBar actionBar = getActionBar();

    // Screen handling while hiding ActionBar icon.
    actionBar.setDisplayShowHomeEnabled(false);

    // Screen handling while hiding Actionbar title.
    actionBar.setDisplayShowTitleEnabled(false);

    // Creating ActionBar tabs.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Setting custom tab icons.


    // Setting tab listeners.
    bmwTab.setTabListener((ActionBar.TabListener) new Fragment1());
    toyotaTab.setTabListener((ActionBar.TabListener) new Fragment2());
    fordTab.setTabListener((ActionBar.TabListener) new Fragment3());

    // Adding tabs to the ActionBar.
    actionBar.addTab(bmwTab);
    actionBar.addTab(toyotaTab);
    actionBar.addTab(fordTab);
}
}

PagerAdapter.java

import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar;

public class PagerAdapter implements ActionBar.TabListener {

private Fragment fragment;

// The contructor.
public PagerAdapter (Fragment fragment) {
    this.fragment = fragment;
}

// When a tab is tapped, the FragmentTransaction replaces
// the content of our main layout with the specified fragment;
// that's why we declared an id for the main layout.
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.activity_main, fragment);
}

// When a tab is unselected, we have to hide it from the user's view.
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}

// Nothing special here. Fragments already did the job.
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}
}

activity_fragment1.xml

 <RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ss.tabswithwebview.Fragment1">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView">

</WebView>
</RelativeLayout>

Fragment1.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class Fragment1 extends android.support.v4.app.Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_fragment1, container, false);
    WebView webView=(WebView)rootView.findViewById(R.id.webView);
    webView.loadUrl("www.google.com");
    return rootView;
}

}

Similarly create different fragment as created above.




回答2:


Actually FragmentActivites Need To Be Created and in Your layouts You neeed to SetBackground As Your Wish Color So That activity_main is not loaded Along With FragmentLayout

And for Webview Just Add Webview And Load Url In each fragments According to Yourself




回答3:


My codes

import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.app.ActionBar;

public class Adapter implements ActionBar.TabListener {

private android.support.v4.app.Fragment fragment;

// The contructor.
public Adapter(android.support.v4.app.Fragment fragment) {
this.fragment = fragment;
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.activity_main, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}
}

When a tab is tapped, the FragmentTransaction replaces the content of our main layout with the specified fragment; that's why we declared an id for the main layout.



来源:https://stackoverflow.com/questions/38415216/how-to-make-tabs-with-webview-to-load-from-a-web-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!