Java 隐藏片段并显示其他片段不工作

Java 隐藏片段并显示其他片段不工作,java,android,android-fragments,Java,Android,Android Fragments,我正在开发一个Android应用程序,试图隐藏一个片段并显示另一个片段。我已经被困在这两天了,因为我尝试了所有的可能性,谷歌搜索了很多-但它并没有提供预期的结果: package com.cubic.cumo.rmvsmt.fragments; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.v

我正在开发一个Android应用程序,试图隐藏一个片段并显示另一个片段。我已经被困在这两天了,因为我尝试了所有的可能性,谷歌搜索了很多-但它并没有提供预期的结果:

 package com.cubic.cumo.rmvsmt.fragments;

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;

    import com.cubic.cumo.basislib.cibo.Cibo;
    import com.cubic.cumo.basislib.cibo.CiboJourney;
    import com.cubic.cumo.basislib.common.CumoTextTools;
    import com.cubic.cumo.basislib.fragments.BasisFragment;
    import com.cubic.cumo.basislib.fragments.FragmentCreator;
    import com.cubic.cumo.basislib.fragments.UiContent;
    import com.cubic.cumo.basislib.model.StopInfo;
    import com.cubic.cumo.basislib.model.TicketInfo;
    import com.cubic.cumo.rmvsmt.MainActivity;
    import com.cubic.cumo.rmvsmt.R;

    public class RMVCiboJourneyFragment extends BasisFragment {

        /**
         * Fragment creator for the RMV Station Selector.
         */
        public static class Creator extends FragmentCreator {

            @Override
            public BasisFragment create(String name, Bundle args) {
                RMVCiboJourneyFragment f = new RMVCiboJourneyFragment();
                f.setArguments(args);
                return f;
            }
        }

        private View mContentNoJourney;
        private View mContentJourney;
        private TextView mOriginTextView;
        private TextView mDestinationTextView;
        private TextView mDepartureTextView;
        private TextView mArrivalTextView;

        private CiboJourney mJourney;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_cibo_journey, container, false);

            mContentJourney =       view.findViewById(R.id.content_journey);
            mContentNoJourney =     view.findViewById(R.id.content_no_journey);
            mOriginTextView =       view.findViewById(R.id.cibo_journey_origin_text);
            mDestinationTextView =  view.findViewById(R.id.cibo_journey_destination_text);
            mDepartureTextView =    view.findViewById(R.id.cibo_journey_departure_text);
            mArrivalTextView =      view.findViewById(R.id.cibo_journey_arrival_text);

            Button okButton = view.findViewById(R.id.cibo_ok_button);
            okButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    getActivity().setResult(0);
                    getActivity().finish();
                }
            });
            Button onReportErrorButton = (Button) view.findViewById(R.id.cibo_report_error_button);
            onReportErrorButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().setResult(1);
                    getActivity().finish();
                }
            });

            Bundle bundle = getArguments();
            try {
                mJourney = (CiboJourney) bundle.getSerializable("DATA");
            }
            catch (Exception e) {
                // This will catch any exception, because they are all descended from Exception
                System.out.println("Error " + e.getMessage());
                return null;
            }

            if (mJourney != null && mJourney.arrival != null && mJourney.departure != null && mJourney.origin != null && mJourney.destination != null) {
                mContentJourney.setVisibility(View.VISIBLE);
                mContentNoJourney.setVisibility(View.GONE);

                mOriginTextView.setText(mJourney.origin.getName());
                mDepartureTextView.setText(getString(R.string.cibo_start_station, CumoTextTools.formatDate(mJourney.departure, CumoTextTools.CumoDateFormat.TIME)));
                mDestinationTextView.setText(mJourney.destination.getName());
                mArrivalTextView.setText(getString(R.string.cibo_dest_station, CumoTextTools.formatDate(mJourney.arrival, CumoTextTools.CumoDateFormat.TIME)));
            } else {
                mContentJourney.setVisibility(View.GONE);
                mContentNoJourney.setVisibility(View.VISIBLE);
                this.getId();
            }

            return view;
        }


        @Override
        public void showContent(UiContent content) {

        }

        @Override
        public boolean onBackPressed() {
            return false;
        }
    }
这是应该在另一个片段中显示的视图:

Bundle bundle = getArguments();
    try {
        mJourney = (CiboJourney) bundle.getSerializable("DATA");
    }
    catch (Exception e) {
        // This will catch any exception, because they are all descended from Exception
        System.out.println("Error " + e.getMessage());
        return null;
    }

    if (mJourney != null && mJourney.arrival != null && mJourney.departure != null && mJourney.origin != null && mJourney.destination != null) {
        mContentJourney.setVisibility(View.VISIBLE);
        mContentNoJourney.setVisibility(View.GONE);

        mOriginTextView.setText(mJourney.origin.getName());
        mDepartureTextView.setText(getString(R.string.cibo_start_station, CumoTextTools.formatDate(mJourney.departure, CumoTextTools.CumoDateFormat.TIME)));
        mDestinationTextView.setText(mJourney.destination.getName());
        mArrivalTextView.setText(getString(R.string.cibo_dest_station, CumoTextTools.formatDate(mJourney.arrival, CumoTextTools.CumoDateFormat.TIME)));
    } else {
        mContentJourney.setVisibility(View.GONE);
        mContentNoJourney.setVisibility(View.VISIBLE);
        this.getId();
    }

    return view;
}
这是另一个片段中的代码,应该执行MContentNoTravely.setVisibility(View.VISIBLE)或至少显示相应的视图:

 if ((backButton != null) && (closeButton != null)) {
                    backButton.setVisibility(View.INVISIBLE);
                    closeButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.d(this.getClass().getName(), "closeButton geklickt");

                            RMVCiboJourneyFragment nextFrag= new RMVCiboJourneyFragment();
                            getActivity().getSupportFragmentManager().beginTransaction()
                                    .replace(R.id.WebContentWebView, nextFrag, "mContentNoJourney")
                                    .addToBackStack(null)
                                    .commit();
                            //mContentNoJourney =     view.findViewById(R.id.content_no_journey);
                            //mContentNoJourney.setVisibility(View.VISIBLE);
                             /*     *//*      List<Fragment> fragmentList = getFragmentManager().getFragments();
                FragmentManager fragmentManager = getFragmentManager();
                            FragmentFactory.instance().registerCreator(FragmentFactory.CiboJourney, new RMVCiboJourneyFragment.Creator());
                            FragmentTransaction transaction;
                            transaction = fragmentManager.beginTransaction();*//*
                            //transaction.replace(R.id.container,R.id.content_no_journey);
                            transaction.addToBackStack(null);
                            transaction.commit();*/

                         /*   Fragment fragment = getFragmentManager().findFragmentByTag();
                            if(fragment != null)
                                getFragmentManager().beginTransaction().remove(fragment).commit();*/

                            // FragmentManager fm = getActivity().getFragmentManager();
                            // fm.popBackStack();
                            // You might have to access all the fragments by their tag,
                            // in which case just follow the line below to remove the fragment
                            /*if (fragmentList == null) {
                                // code that handles no existing fragments
                            }
                            for (Fragment frag : fragmentList )
                            {
                                // To save any of the fragments, add this check
                                // a tag can be added as a third parameter to the fragment when you commit it
                               *//* if (frag.getTag().equals("<tag-name")) {
                                    continue;
                                }*//*
                            getFragmentManager().beginTransaction().remove(frag).commit();
                            }

                            Fragment id = getFragmentManager().findFragmentById(R.id.container);
                            if(getFragmentManager().findFragmentById(R.id.container) == null) {
                                getFragmentManager().beginTransaction()
                                        .replace(R.id.titlebar, id)
                                        .commit();
                            }
                            RMVCiboJourneyFragment journeyFragment = new RMVCiboJourneyFragment();
                            getFragmentManager().beginTransaction().replace(R.id.content_no_journey, journeyFragment).commit();*/
                            //mContentNoJourney = getFragmentManager().popBackStackImmediate(R.id.content_no_journey);
                            //mContentNoJourney.setVisibility(View.VISIBLE);
                        }
                    });
                }
if((backButton!=null)和&(closeButton!=null)){
backButton.setVisibility(视图不可见);
closeButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Log.d(this.getClass().getName(),“closeButton geklickt”);
RMVCiboJourneyFragment nextFrag=新的rmvcibourneyfragment();
getActivity().getSupportFragmentManager().beginTransaction()
.替换(R.id.WebContentWebView,nextFrag,“mContentNoJourney”)
.addToBackStack(空)
.commit();
//mContentNoJourney=view.findviewbyd(R.id.content\u no\u journey);
//mContentNoJourney.setVisibility(View.VISIBLE);
/**//*List fragmentList=getFragmentManager().getFragments();
FragmentManager FragmentManager=getFragmentManager();
FragmentFactory.instance().registerCreator(FragmentFactory.CiboTravely,新的RMVCiboJourneyFragment.Creator());
零碎交易;
事务=fragmentManager.beginTransaction()*//*
//事务.替换(R.id.容器,R.id.内容\u编号\u行程);
transaction.addToBackStack(空);
commit()*/
/*Fragment Fragment=getFragmentManager().findFragmentByTag();
if(片段!=null)
getFragmentManager().beginTransaction().remove(fragment.commit()*/
//FragmentManager fm=getActivity().getFragmentManager();
//fm.popbackbackstack();
//您可能必须通过标签访问所有片段,
//在这种情况下,只需按照下面的行删除片段
/*if(fragmentList==null){
//不处理现有片段的代码
}
for(片段片段:片段列表)
{
//要保存任何片段,请添加此检查
//提交片段时,可以将标记作为第三个参数添加到片段中

*//*如果(frag.getTag())等于("而不是
show/hide
尝试使用fragment根据条件替换fragmentTransaction@Md.Asaduzzaman:非常感谢-你能提供一个如何做到这一点的例子吗?我在注释掉的代码中尝试了它,但没有成功-任何帮助或提示都将不胜感激,谢谢!不清楚你的要求。你想重复吗用这个
RMVCiboJourneyFragment
?我说得对吗?事实上,我想做/展示mContentJourney.setVisibility(View.GONE);mContentNoJourney.setVisibility(View.visibility);它在一个片段中工作得很好,但在另一个片段中工作得不好不清楚你的应用程序/结构是什么样子的(主要是上面提到的
WebContentWebView
view)无论如何,我可能建议在版面中插入单个匹配父项/match父项
FrameLayout
,并在此容器中插入和替换您的片段。您在
FragmentManager
上的替换方法看起来正确,因此问题应该在不同的位置。