删除片段中的先前视图的问题

我在 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;


    }

}


慕容森
浏览 127回答 1
1回答

函数式编程

我建议你在你的片段布局中加入一些 GroupView 来做到这一点。fragment_layout.xml<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:id="@+id/container"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="match_parent"></FrameLayout>在你的 Fragment.java 中&nbsp; &nbsp; &nbsp;View view;&nbsp; &nbsp; &nbsp;Framelayout fContainer;&nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view = inflater.inflate(R.layout.fragment_layout, container, false);&nbsp; &nbsp; &nbsp; &nbsp; return startView();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public View startview(){&nbsp; &nbsp; &nbsp; &nbsp; fContainer = (Framelayout) view.findViewById(R.id.container);&nbsp; &nbsp; &nbsp; &nbsp; listView = (ListView) view.findViewById(R.id.noteList);&nbsp; &nbsp; &nbsp; &nbsp; listAdapter = new ListAdapter(context,image,titleText,subTitleText);&nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(listAdapter);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }public void childfragment(){&nbsp; &nbsp; &nbsp; &nbsp; fContainer.removeAllViews();&nbsp; &nbsp; &nbsp; &nbsp; Fragment childFragment = new FragmentList(context);&nbsp; &nbsp; &nbsp; &nbsp; FragmentTransaction transaction =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; getChildFragmentManager().beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; transaction.replace(R.id.container, childFragment).commit();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }现在在另一个片段 (FragmentList) 的 oncreateview 中放置您想要膨胀的视图。片段列表.java@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return newview;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java