使用浏览器画面以两个片段之间切换 - 但它切换到一个片段后消失 [英] Using viewer page to switch between two fragments - however it disappears after switching into a fragment
问题描述
我有一个问题,我的浏览器页面,所以从片段扩展了这个的Java文件,我有有一些code做视图寻呼机。基本上在我的屏幕我想,让用户刷卡对面左侧横传到右侧和刷卡。它工作正常,因为它应该是,但是我有太多,所以如果我去一个不同的片段,然后回来这个屏幕可以让我做这件事刷卡功能,所有的突然一切从消失了,我的工作项目中的其他片段屏幕。任何想法如何解决这个问题?
I have a problem with my viewer page, so in this java file that I have which extends from Fragment has some code to do with view pager. Basically in my screen I want to let the user to swipe cross to the right and swipe across to the left. It works fine as it should be however I have other fragments too within the project I am working on so if I go to a different fragment and come back to this screen which lets me do this swipe feature, all for a sudden everything disappears from the screen. Any idea how to fix this issue ??
这是java文件;
public class ExampleFragment extends Fragment {
View v;
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.tab2_main, container, false);
viewPager = (ViewPager)v.findViewById(R.id.accountswitching);
AccountSwitching accountSwitching = new AccountSwitching(getFragmentManager());
viewPager.setAdapter(accountSwitching);
这是code我有AccountSwitching java类里面;
This is the code I have inside the AccountSwitching java class;
public class AccountSwitching extends FragmentPagerAdapter {
public AccountSwitching(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new FirstPage();
case 1:
return new SecondPage();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
所以,当你看到我重视这个帖子上,在那里说你好的形象,我有一个onclick它是在第一页。 java的文件
So as you saw on the image I attached on this post, where it says hello, I have got an onclick which is in the FirstPage. java file
第一页。的Java文件;
FirstPage. java file ;
public class FirstPage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.home_screenmain_first, container, false);
v.findViewById(R.id.first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
((FrameLayout)getActivity().findViewById(android.R.id.tabcontent)).removeAllViews();
transaction.replace(android.R.id.tabcontent, new ChangeScreen(), "ChangeScreen");
transaction.commit();
}
});
return v;
}
}
return v;
}
}
这是从浏览器页面的东西的屏幕显示是显示在屏幕的我所片段之间切换之前看到的例子
This is a screen show of something from the viewer page is showing on the screen Example of what I am seeing before switching between fragments
这是我的片段之间切换的片段之间切换后
This is what I see after switching between fragments After switching between fragments
请告诉我,如果你想看到的任何其他code,不知道哪个code这里给这就是为什么我不同意它在这里。
Please tell me if you want to see any other code, not sure which code to send here that's why I didn't share it on here.
推荐答案
看起来你是一个片段内创建一个ViewPager。
Looks like you are creating a ViewPager inside a fragment.
在这种情况下, getFragmentManager()
应 getChildFragmentManager()
。
这篇关于使用浏览器画面以两个片段之间切换 - 但它切换到一个片段后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!