简体   繁体   English

如何在不使用堆栈的情况下替换活动微调器中的片段

[英]how to replace fragment from activity spinner without backstack

i have one activity which contains spinner control in appbar. 我有一个活动,其中包含Appbar中的微调控件。 in homeActivity i'm using fragment which have default fragment as parentoptionfragment, from that fragment there are 3 option to change the fragments, if i have choosen one fragment from parentoption fragment and i want to change spinner value then the fragment should be updated without adding to backstack means if i press back button then it should call parent optionfragment, but when i'm trying to do so i'm getting error. 在homeActivity中,我正在使用具有默认片段作为parentoptionfragment的片段,从该片段中有3个选项可以更改片段,如果我从parentoption片段中选择了一个片段并且我想更改微调器值,那么该片段应该在不添加的情况下进行更新返回堆栈意味着如果我按下后退按钮,那么它应该调用父optionfragment,但是当我尝试这样做时,我得到了错误。

    public void GetChildData(String token) {
    ParentOptionsFragment fragment =new ParentOptionsFragment();
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
    }

@Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            tv_childClassname.setText(classNameArr[position]);
            tv_childSchoolName.setText(schoolNameArr[position]);

            Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
            if (f instanceof ChildMapFragment){
                Toast.makeText(HomeActivity.this, "refreshing childmapfragment", Toast.LENGTH_SHORT).show();

                ChildMapFragment fragment = new ChildMapFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.popBackStackImmediate (fragment.getClass().getName(), 0);
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            }else if(f instanceof ParentOptionsFragment){
                Toast.makeText(HomeActivity.this, "spinner changed from ParentOptionsFragment", Toast.LENGTH_SHORT).show();

            }
        }

parentoptionfragment.java parentoptionfragment.java

public class ParentOptionsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_parent_options, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final ImageView img1=(ImageView)view.findViewById(R.id.imageView);
        ImageView img2=(ImageView)view.findViewById(R.id.imageView2);
        ImageView img3=(ImageView)view.findViewById(R.id.img_transport);

        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChildMapFragment fragment = new ChildMapFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        /*final ViewTreeObserver vto = img1.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int x;
                img1.getViewTreeObserver().removeOnPreDrawListener(this);
                x = img1.getMeasuredWidth();
                img1.setLayoutParams(new LinearLayout.LayoutParams(x,x));
                return true;
            }
        });*/
    }
}

childfragment.java childfragment.java

public class ChildFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_child_map, container, false);
    }
}

homeActivity ---> defaultfragment-- parentoptionfragment parentoptiofragment---> childfragment using spinner update childfragment without backstack onbackpress of childfragment --> parentoptionfragment homeActivity ---> defaultfragment-- parentoptionfragment parentoptiofragment --->使用微调器更新childfragment的子片段,而没有backstack onbackpressed的子片段-> parentoptionfragment

replace 更换

Toast.makeText(HomeActivity.this, "refreshing childmapfragment", Toast.LENGTH_SHORT).show(); Toast.makeText(HomeActivity.this,“刷新子地图片段”,Toast.LENGTH_SHORT).show();

            ChildMapFragment fragment = new ChildMapFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.popBackStackImmediate (fragment.getClass().getName(), 0);
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

with : 与:

FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.popBackStack()

update : on your child fragment add a method : update() and call it : 更新:在您的子片段上添加方法: update()并调用它:

FragmentManager fragmentManager = getSupportFragmentManager();
           Fragment  fragment = fragmentManager.findFragmentById(R.id.fragment_container)
    if(fragment isInstanceOf ChildMapFragment ){
    ((ChildMapFragment )fragment).update() //call your update method here
    }
FragmentManager fragmentManager = getSupportFragmentManager();
        //    String f_name=fragment.getClass().getName();
        if (!fragmentManager.popBackStackImmediate(tag, 0)) {

            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.content_frame, fragment, tag);
            ft.addToBackStack(tag);
            ft.commit();

        }

to Refresh fragment implement back stack in activity like this 刷新片段在活动中实现反向堆栈

   @Override
    public void onBackStackChanged() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fr = fragmentManager.findFragmentById(R.id.content_frame);
        if (fr != null) {
            fr.onResume();
        }
    }

here pass different tag base on different fragment 在这里根据不同的片段传递不同的标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM