我在这里用过碎片。但是我不能回到父母身边。
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>
请帮帮我!!!从该Web视图打开链接后,我想返回父片段。
我知道那里有很多答案。一个是覆盖后退按钮。但是我不知道如何实现。我是新来的。因此,有人可以帮助我编写代码。
最佳答案
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
}
@Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
在tab1 / tab2 / tab3片段页面上编写onBackPressed()重写方法。它会帮助你。
关于android - 如何使用webview返回到父 fragment ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19529770/