我在 android 中创建了一个包含 2 个函数的片段,在调用片段时在开始时调用 startview(),然后当用户单击按钮时调用 childfragment()。那时它应该从 startview() 中删除视图,然后用 childfragment() 中的 newview 替换它,这就是我认为它会做的。但是它并没有删除旧视图并将新视图放在旧视图之上。甚至认为我已经放置 mContainer.removeAllViews();它并没有删除旧视图。我错过了什么?请帮忙。
这是代码
@SuppressLint("ValidFragment")
public class FragmentSideContent extends android.app.Fragment {
//private OnFragmentInteractionListener mListener;
Context context;
View view,trueview;
ListView listView;
ListAdapter listAdapter;
private LayoutInflater mInflater;
private ViewGroup mContainer;
int[] image = {1,2,3};
String[] titleText = {"one","two","three","one","two","three","one","two","three","one","two","three"};
String[] subTitleText = {"one","two","three","one","two","three","one","two","three","one","two","three"};
public FragmentSideContent(Context context) {
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
mInflater = inflater;
mContainer = container;
startview();
return trueview;
}
public void startview(){
view = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
listView = (ListView) view.findViewById(R.id.noteList);
listAdapter = new ListAdapter(context,image,titleText,subTitleText);
listView.setAdapter(listAdapter);
trueview = view;
}
public void childfragment(){
mContainer.removeAllViews();
View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
Fragment childFragment = new FragmentList(context);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment_container, childFragment).commit();
mContainer.addView(newview);
mContainer.addView(trueview);
trueview = newview;
}
}
函数式编程
相关分类