我在tablayout上设置了fragment。我在第一个标签上有一个floatingactionbutton。当我点击按钮时,我会转到另一个fragment。但当我按下后退按钮然后回到tabview时,我的布局就消失了,并显示为白色屏幕。 这是tablayout
的代码 public class ChooseTab extends Fragment implements TabLayout.OnTabSelectedListener{
TabLayout tabLayout;
private ViewPager viewPager;
RelativeLayout relativeLayout;
FragmentManager fm;
FragmentTransaction tx;
String back="";
public ChooseTab() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.choose_tab,container,false);
relativeLayout=(RelativeLayout)v.findViewById(R.id.relative);
tabLayout=(TabLayout)v.findViewById(R.id.simpleTabLayout);
viewPager=(ViewPager)v.findViewById(R.id.simpleViewPager);
viewPager.setOffscreenPageLimit(3);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
PagerAdapter adapter=new PagerAdapter(getActivity().getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(this);
fm=getFragmentManager();
tx=fm.beginTransaction();
tx.addToBackStack(back);
tx.commit();
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return v;
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
}
这是Tab One的代码
public class CurrentStatus extends Fragment {
//
// FragmentManager fm;
FragmentManager fm;
FragmentTransaction tx;
RecyclerView recyclerView;
// RecyclerView.Adapter adapter;
NewAdapter adapt;
private ArrayList<CurrentEntry> current;
String userUrl;
String second_req="second_req";
EditText search;
public CurrentStatus() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.current_status,container,false);
FloatingActionButton fab=(FloatingActionButton)v.findViewById(R.id.fab);
recyclerView=(RecyclerView)v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
current=new ArrayList<>();
search=(EditText)v.findViewById(R.id.search);
getData();
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
fm=getFragmentManager();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tag="one";
tx=fm.beginTransaction();
tx.replace(R.id.frame,new Fragment(),tag);
tx.addToBackStack(tag);
tx.commit();
}
});
return v;
}
当我转到新片段并再次返回tablayout时,布局消失,屏幕显示为白色。
这是MainActivity的代码
public void onBackPressed() {
Fragment f=getSupportFragmentManager().findFragmentById(R.id.frame);
if(f instanceof Choose)
{
if (doubleBackToExitPressedOnce)
{
super.onBackPressed();
return;
}
doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
else {
super.onBackPressed();
}
}
答案 0 :(得分:0)
在您的活动中覆盖onBackPressed()
。
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
finish();
} else {
getSupportFragmentManager().popBackStackImmediate();
}
}
要添加新片段,请使用add
代替replace
来克服重新加载问题。如果您的片段中嵌套了片段,则必须使用getChildFragmentManager()
来处理事务,并在getChildFragmentManager()
活动的父片段中使用onBackPressed()
对其进行验证。