Java 碎片赢了';隐藏后不显示?
我今天只是学习一些片段。我按下一个按钮,它隐藏了一个碎片。但是,如果我试图展示片段,什么都没有发生,为什么?我在跟随这个教程,在中间,我决定试着让一个片段消失/出现在我按下按钮< /p> 按钮2片段:Java 碎片赢了';隐藏后不显示?,java,android,android-fragments,Java,Android,Android Fragments,我今天只是学习一些片段。我按下一个按钮,它隐藏了一个碎片。但是,如果我试图展示片段,什么都没有发生,为什么?我在跟随这个教程,在中间,我决定试着让一个片段消失/出现在我按下按钮< /p> 按钮2片段: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button2_fragment,
container, false);
Button button = (Button) view.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ButtonFragment fragment = (ButtonFragment) getFragmentManager()
.findFragmentById(R.id.ButtonFragment);
if (fragment != null && fragment.isInLayout()) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.hide(fragmentManager.findFragmentById(R.id.ButtonFragment));
transaction.commit();
}
else
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.show(fragmentManager.findFragmentById(R.id.ButtonFragment));
transaction.commit();
}
}
});
return view;
}
我在xml中有三个类似的片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#123456" >
<fragment
android:id="@+id/ButtonFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.myfragment.ButtonFragment" ></fragment>
<fragment
android:id="@+id/TimeFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.myfragment.TimeFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
<fragment
android:id="@+id/Button2Fragment"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
class="com.example.myfragment.Button2Fragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
控制似乎总是在这里面
if(fragment!=null&&fragment.isInLayout())
因此,片段总是隐藏的,一旦隐藏,就永远不会显示
尝试使用切换,它将在每个onClick()
事件上保持切换。这样,控件将交替流向if
和else
块,从而隐藏和显示片段
尝试使用
isVisible()
而不是isInLayout()
谢谢,我会试试的。但是,我也尝试了if(!fragment.isInLayout())而不是else,这也不起作用。另外,你知道为什么如果我使用remove而不是hide,会出现Backbackback异常吗?感谢更改为isVisible,除了.remove之外,还可以使事情正常工作,但我会再做一个线程。还有一个问题,你所说的切换是什么意思,我只是有一个if然后一个else if.by切换,我只是指一个布尔值
,它会在每个onClick()
package com.example.myfragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity implements ButtonFragment.OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// if the wizard generated an onCreateOptionsMenu you can delete
// it, not needed for this tutorial
@Override
public void onRssItemSelected(String link) {
TimeFragment fragment = (TimeFragment) getFragmentManager()
.findFragmentById(R.id.TimeFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}
}
}