问题描述
我开发的应用程序,其中包含2片,我想根据我的需要显示隐藏。继code具有简单的我的问题的例子。这个简单的Fragmentactivity包含1个按钮和一个listfragment。
这个简单的例子作品完美无瑕。但我并不满足于显示隐藏片段。如果您删除layout.setVisibility(View.GONE);从code,那么ft.hide(F);会不会隐藏片段。事实上,我们没有隐瞒片段,我们都躲在容器。
我的问题是,这是一种方式来显示隐藏的片段?如果没有,那么请及时与测试例子来说明如何隐藏和显示片段,因为很多人都面临这样的问题。
公共类MainActivity扩展FragmentActivity实现OnClickListener {
片段1 F;
按钮B;
的LinearLayout布局;
片段了Myf;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
B =(按钮)findViewById(R.id.button1);
布局=(的LinearLayout)findViewById(R.id.ll);
F =新片段1();
}
@覆盖
公共无效的onClick(视图v){
FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
如果(f.isHidden()){
ft.show(F);
layout.setVisibility(View.VISIBLE);
b.setText(隐藏);
} 其他 {
ft.hide(F);
b.setText(显示);
layout.setVisibility(View.GONE);
}
ft.commit();
// TODO自动生成方法存根
}
不要乱用容器的可视性标志 - FragmentTransaction.hide /节目确实在内部为您
因此,要做到这一点,正确的做法是:
FragmentManager FM = getFragmentManager();
fm.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out)
.show(somefrag)
。承诺();
I am developing application which contains 2 fragments and i want to show hide according to my need. Following code has simple example of my problem.This simple Fragmentactivity contains 1 button and one listfragment.
This simple example works flawless. but i am not satisfied with show hide fragment. If you remove layout.setVisibility(View.GONE); from the code then ft.hide(f); will not hide fragment. In fact we are not hiding fragment we are hiding container.
My Question is, IS this a way to show hide fragments? If not then please explain with tested example How to hide and show Fragments because lots of people are facing this problem.
public class MainActivity extends FragmentActivity implements OnClickListener {
Fragment1 f;
Button b;
LinearLayout layout;
Fragment myf;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
layout = (LinearLayout) findViewById(R.id.ll);
f = new Fragment1();
}
@Override
public void onClick(View v) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
if (f.isHidden()) {
ft.show(f);
layout.setVisibility(View.VISIBLE);
b.setText("Hide");
} else {
ft.hide(f);
b.setText("Show");
layout.setVisibility(View.GONE);
}
ft.commit();
// TODO Auto-generated method stub
}
Don't mess with the visibility flags of the container - FragmentTransaction.hide/show does that internally for you.
So the correct way to do this is:
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(somefrag)
.commit();
这篇关于显示在Android的隐藏片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!