为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕容森 2021-11-24 20:07:11
我在 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;    }}
查看完整描述

1 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

我建议你在你的片段布局中加入一些 GroupView 来做到这一点。


fragment_layout.xml


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

             xmlns:tools="http://schemas.android.com/tools"

             android:id="@+id/container"

             android:layout_width="match_parent"

             android:layout_height="match_parent">

</FrameLayout>

在你的 Fragment.java 中


     View view;

     Framelayout fContainer;

     @Override

      public View onCreateView(LayoutInflater inflater, ViewGroup container,

                               Bundle savedInstanceState) {

          view = inflater.inflate(R.layout.fragment_layout, container, false);


        return startView();

      }


      public View startview(){

        fContainer = (Framelayout) view.findViewById(R.id.container);

        listView = (ListView) view.findViewById(R.id.noteList);

        listAdapter = new ListAdapter(context,image,titleText,subTitleText);

        listView.setAdapter(listAdapter);

        return view;

    }



public void childfragment(){

        fContainer.removeAllViews();

        Fragment childFragment = new FragmentList(context);

        FragmentTransaction transaction = 

        getChildFragmentManager().beginTransaction();

        transaction.replace(R.id.container, childFragment).commit();    

    }

现在在另一个片段 (FragmentList) 的 oncreateview 中放置您想要膨胀的视图。


片段列表.java


@Override

          public View onCreateView(LayoutInflater inflater, ViewGroup container,

                                   Bundle savedInstanceState) {

             View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);


            return newview;

          }


查看完整回答
反对 回复 2021-11-24
  • 1 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信