如何在片段中的Webview上添加后退按钮

时间:2013-10-24 01:37:15

标签: android webview android-fragments

我在webview中看到了很多关于后退按钮的例子。但是,我有一个使用片段和viewpager的标签布局。我见过一个有活动的例子,但有一个内部片段类,后退按钮有效。但是,当我尝试这个时,我的TabsPagerAdapter中出现了很多错误。如果有人知道如何为后退按钮做代码,请告诉我。

TabFour

public class TabFour extends Fragment {

static WebView webView;
String myBlogAddr = "http://192.168.1.75/Treasuria-v2/";
String myUrl;
Button map;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_tab_four, container, false);

    webView= (WebView)rootView.findViewById(R.id.webView);


    webView.getSettings().setJavaScriptEnabled(true); 
    webView.setWebViewClient(new MyWebViewClient());
    String title = "Quezon City Guide";
    String desc = "Your Number One City Guide!";
    String image = "http://s22.postimg.org/5t4qidqpt/launcher114x114.png";

    myBlogAddr ="http://www.facebook.com/sharer.php?m2w&s=100&p[title]="+title+"&p[summary]="+desc+"&p[url]=http://www.sample.com&p[images][0]="+image;
    if(myUrl == null){
        myUrl = myBlogAddr;
    }
    webView.loadUrl(myUrl);
    Log.i("URL", myBlogAddr);

    webView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().getActionBar().setSelectedNavigationItem(2);
        }
    });

    map = (Button) rootView.findViewById(R.id.buttonMap);

    map.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            getActivity().getActionBar().setSelectedNavigationItem(2);
        }
    });

    return rootView;


}



private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        myUrl = url;
        view.loadUrl(url);
        return true;
    }
}


public void onBackPressed() {
    getActivity().getActionBar().setSelectedNavigationItem(2);
}

public void OnCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    getActivity().getActionBar().setSelectedNavigationItem(1);
    map = (Button) rootView.findViewById(R.id.buttonMap);

    map.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            getActivity().getActionBar().setSelectedNavigationItem(2);
        }
    });
}


}

MainActivity

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

public static FragmentManager fragmentManager;
public ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private Integer[] Icons = {R.drawable.homeg,R.drawable.placesg, R.drawable.mapg, R.drawable.reviewg,R.drawable.fbg};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragmentManager = getSupportFragmentManager();
    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (Integer tab_icon : Icons) {
        actionBar.addTab(actionBar.newTab().setIcon(tab_icon)
                .setTabListener(this));



    }

    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.synergy88studios.quezoncityguide", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

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

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

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


}

0 个答案:

没有答案