There is problem when i use hide (Page_aboutus) .it said that expression expected ,so what parameter i need to use here. . In addition, i write below code. My code is on replacefragment() function to show and hide fragment.Inside Hide() function block i don't know how to describe the current fragment.
public class Page_Aboutus extends Fragment {
View purview;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
purview = inflater.inflate(R.layout.activity_page__aboutus, container, false);
final RelativeLayout relate = (RelativeLayout) purview.findViewById(R.id.relate);
final LinearLayout open_liu = (LinearLayout) purview.findViewById(R.id.open_liu);
final LinearLayout open_zhen = (LinearLayout) purview.findViewById(R.id.open_zhen);
final LinearLayout open_chen = (LinearLayout) purview.findViewById(R.id.open_chen);
final LinearLayout open_wang = (LinearLayout) purview.findViewById(R.id.open_wang);
open_liu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = new Liu();
replaceFragment(fragment);
}
});
open_zhen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = null;
fragment = new Liuzhen();
replaceFragment(fragment);
}
});
open_chen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = null;
fragment = new Chengyixuan();
replaceFragment(fragment);
}
});
open_wang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = null;
fragment = new wanglei();
replaceFragment(fragment);
}
});
return purview;
}
public void replaceFragment(Fragment somefragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, somefragment)
.hide(Page_Aboutus)//error here;
transaction.addToBackStack("2");
transaction.commit();
}
}
答案 0 :(得分:0)
这是因为当您使用以下内容时:
transaction.add(R.id.fragment_container, somefragment)
.hide(Page_Aboutus);
您要提供一个类名,但是hide()
方法需要一个片段作为参数。
如果要隐藏当前片段,可以使用this
。像这样:
transaction.add(R.id.fragment_container, somefragment)
.hide(this);