gpt4 book ai didi

android - 如何使用 webview 返回到父 Fragment?

转载 作者:行者123 更新时间:2023-11-29 21:30:59 24 4
gpt4 key购买 nike

我在这里使用了 fragment 。但我不能回到 parent 身边。 Swipeactivitydefault.java

 package com.pokhara.nepal;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import com.pokhara.nepal.json.TabsPagerAdapter;

public class Swipeactivitydefault extends FragmentActivity implements
ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3" };

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

// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

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

// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}

/**
* 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) {
}



}

Tab1.java

package com.pokhara.nepal;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class Tab1 extends Fragment {

private WebView webView;
private Bundle webViewBundle;

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

super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout) inflater.inflate(
R.layout.news_activity_child, container, false);

webView = (WebView) ll.findViewById(R.id.webview);

webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

webView.setWebViewClient(new MyCustomWebViewClient());

if (webViewBundle == null) {
webView.loadUrl("http://www.tipfortechs.com/mobile");
} else {
webView.restoreState(webViewBundle);
}
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

return ll;

}

}

Tab2.java和Tab3.java也和Tab 1一样

只想制作三个标签。

TabsPagerAdapter.java

package com.pokhara.nepal.json;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.pokhara.nepal.Ekantipur;
import com.pokhara.nepal.HimalayaTimes;
import com.pokhara.nepal.NepalNews;

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int index) {

switch (index) {
case 0:
// Top Rated fragment activity
return new Tab1();
case 1:
// Games fragment activity
return new Tab2();
case 2:
// Movies fragment activity
return new Tab3();
}

return null;
}

@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}

}

news_activity_child.xml

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

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

</LinearLayout>

请帮帮我!!!从该 WebView 打开链接后,我想返回到父 fragment 。

我知道那里有很多答案。一个是覆盖后退按钮。但我不知道如何实现。我是新来的。那么,有人可以帮我处理代码吗?

最佳答案

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

super.onCreate(savedInstanceState);
//...
}
@Override
public void onBackPressed() {
this.getParent().onBackPressed();
}

在 tab1/tab2/tab3 fragment 页面上编写 onBackPressed() 覆盖方法。它会帮助你。

关于android - 如何使用 webview 返回到父 Fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19529770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com