好的,我找到了解决方案。newInstance
它是为可以传递变量的片段使用构造函数( )。
public class ListPeopleFragment extends Fragment {
public static ListPeopleFragment newInstance(boolean nested){
ListPeopleFragment f = new ListPeopleFragment();
Bundle args = new Bundle();
args.putBoolean("nested", nested);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_list_people, container, false);
boolean nested = false;
Bundle arguments = getArguments();
if (arguments != null)
{
nested = getArguments().getBoolean("nested");
}
displayListViewPeople(view, nested);
return view;
}
}
根据displayListViewPeople
的值设置点击监听器nested
。
您以这种方式实例化片段:
ListPeopleFragment nestedFrag = ListPeopleFragment.newInstance(true);