gpt4 book ai didi

java - 在 Fragment 中设置 onBackPressed

转载 作者:行者123 更新时间:2023-11-30 00:15:58 31 4
gpt4 key购买 nike

如何将activity下面的代码改成Fragment中的代码?这是 Android Studio 代码。我的页面现在是 fragment ,我想使用这段代码,我想修改为 fragment 代码。谁知道请告诉我,谢谢。而且我还将我当前的代码放在 fragment 中, Activity 与 fragment 连接。

fragment 代码:

public class Tab1Fragment extends Fragment {
Context mContext;

public WebView webView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_fragment, container, false);
mContext = view.getContext();
webView = (WebView) view.findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setInitialScale(1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("https://www.facebook.com/");
webView.setWebViewClient(new WebViewClient());

return view;

}
}

Activity 代码:

public class Schedule extends AppCompatActivity {

private static final String TAG = "Schedule";
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);
Log.d(TAG, "onCreate: Starting.");
initialiseView();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
toolbar.setTitle("SCHEDULE");
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
if(getSupportActionBar() !=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}


// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

}
private void setupViewPager(ViewPager viewPager){
SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "4 Sept 2018");
adapter.addFragment(new Tab2Fragment(), "5 Sept 2018");
adapter.addFragment(new Tab3Fragment(), "6 Sept 2018");
viewPager.setAdapter(adapter);
}
private void initialiseView() {

mTab1Fragment = new Tab1Fragment();

FragmentManager mFragmentManager = getSupportFragmentManager();

FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();

mFragmentTransaction.replace(R.id.container, mTab1Fragment, "TAB 1 FRAGMENT");

mFragmentTransaction.commit();

}

@Override
public void onBackPressed() {

//Toast.makeText(this, "Calling back pressed", Toast.LENGTH_SHORT).show();

if (mTab1Fragment != null) {

if (mTab1Fragment.webView.canGoBack()) {

mTab1Fragment.webView.goBack();

}

} else {

super.onBackPressed();

}
}
}

最佳答案

根据您的需要,这是您的 onCreateView 方法

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

View view = inflater.inflate(R.layout.tab1_fragment, container, false);

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

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

webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);

webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("https://www.facebook.com/");
webView.setWebViewClient(new WebViewClient());

view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int code, KeyEvent keyEvent) {

if (code == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {

//Here is logic to handle back press event

return true;

}


return false;
}
});

return view;
}

更新以在 webview 中加载上一页

Your onCreateView is same as you put in question. I made some simple change in Fragment.

Tab1 fragment will be like

public class Tab1Fragment extends Fragment {

Context mContext;

public WebView webView;

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

View view = inflater.inflate(R.layout.tab1_fragment, container, false);

mContext = view.getContext();

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

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setInitialScale(1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("https://www.facebook.com/");
webView.setWebViewClient(new WebViewClient());

return view;
}


}

Now your Activity will be

public class SampleActivity extends AppCompatActivity {

Tab1Fragment mTab1Fragment;

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


initialiseView();

}

private void initialiseView() {

mTab1Fragment = new Tab1Fragment();

FragmentManager mFragmentManager = getSupportFragmentManager();

FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();

mFragmentTransaction.replace(R.id.container, mTab1Fragment, "TAB 1 FRAGMENT");

mFragmentTransaction.commit();

}

@Override
public void onBackPressed() {

//Toast.makeText(this, "Calling back pressed", Toast.LENGTH_SHORT).show();

if (mTab1Fragment != null) {

if (mTab1Fragment.webView.canGoBack()) {

mTab1Fragment.webView.goBack();

}

} else {

super.onBackPressed();

}
}
}

NOTE: Here I kept webview public just for simplicity. Keep it private and use getter to access it. If you want to use more fragments then add TAG for each fragment and identify which fragment is visible.

关于java - 在 Fragment 中设置 onBackPressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47277048/

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