错误:片段之间交换

时间:2018-09-26 09:48:38

标签: android android-fragments android-recyclerview android-toolbar

  • 我将尽力解释,添加参考文献和图片,其中 我认为很难用语言解释我遇到的错误。 使用片段和导航抽屉时,我遇到了这个问题 错误:

  • 当我打开导航菜单时,有时 片段被重复或再次加载。这东西完全是 随机的,没有精确的方案。似乎唯一的事情 连接的是RecyclerView和导航抽屉。
  • 仅因为我不知道我在哪里错,如果您在注释中写上 需要一些东西,代码或其他信息,我会不时添加它 原来的问题。
  • 我没有收到任何错误,为了帮助我和您,请添加一些代码并 该片段如何无错误加载的图像:

@Override
public boolean onNavigationItemSelected(MenuItem item) {

    Fragment fragment = null;
    Class aclass = null;
    Bundle args = new Bundle();
    Intent intentOperatore = getIntent();
    String mParamOperatore = intentOperatore.getStringExtra(ARG_PARAM_OPERATORE);
    boolean navigation = Boolean.FALSE;
    boolean intent = Boolean.FALSE;
    boolean scan = Boolean.FALSE;
    boolean exit = Boolean.FALSE;

    try{
        switch (item.getItemId()){
            case R.id.nav_home:
                aclass = HomeFragment.class;
                fragment = (Fragment) aclass.newInstance();
                args.putString(ARG_PARAM_OPERATORE,mParamOperatore);
                fragment.setArguments(args);
                navigation = Boolean.TRUE;
                break;
            case R.id.nav_procedura_immobiliare:
                aclass = FiltroFragment.class;
                fragment = (Fragment) aclass.newInstance();
                args.putString(ARG_CHECK_CLASS,"immobiliari");
                fragment.setArguments(args);
                navigation = Boolean.TRUE;
                break;
            case R.id.nav_procedura_mobiliare:
                aclass = FiltroFragment.class;
                fragment = (Fragment) aclass.newInstance();
                args.putString(ARG_CHECK_CLASS,"mobiliari");
                fragment.setArguments(args);
                navigation = Boolean.TRUE;
                break;
            case R.id.nav_localizza_procedura:
                aclass = LocalizzaActivity.class;
                intent = Boolean.TRUE;
                break;
            case R.id.nav_ricognizione:
                aclass = RicognizioneFragment.class;
                fragment = (Fragment) aclass.newInstance();
                args.putString(ARG_PARAM_RICOGNIZIONE,""); //TODO: Add param here.
                fragment.setArguments(args);
                navigation = Boolean.TRUE;
                break;
            case R.id.nav_qrcode:
                scan = Boolean.TRUE;
                break;
            case R.id.nav_exit:
                exit = Boolean.TRUE;
                break;
        }

        if(navigation) { // Navigazione per i fragment
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
        } if(intent) { // Navigazione per le activity
            Intent myIntent = new Intent(getApplicationContext(),aclass);
            myIntent.putExtra(ARG_PARAM_OPERATORE,mParamOperatore);
            startActivity(myIntent);
            finish();
        } if(scan) {
            new MVBarcodeScanner.Builder()
                    .setScanningMode(MVBarcodeScanner.ScanningMode.SINGLE_AUTO)
                    .setFormats(Barcode.QR_CODE)
                    .build()
                    .launchScanner(MainActivity.this,REQ_CODE);
        } if(exit) { // Navigazione per l'uscita
            MaterialDialog dialog = new MaterialDialog.Builder(this)
                    .title(R.string.title_exit)
                    .content(R.string.summary_exit)
                    .positiveText(R.string.action_exit)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            finish();
                        }
                    })
                    .negativeText(R.string.action_no)
                    .show();
        }
    } catch (Exception e) {
        Log.e(this.getClass().getName(), e.getMessage(), e);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

编辑:

  • 我还注意到另一件事,回收不是唯一的 重复。还有一个重复的工具栏项:

enter image description here enter image description here

修改2:  -我添加了onCreateOptionsMenu和一些在    onCreateView:

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //inflater.inflate(R.menu.procedura_menu_action, menu);
        inflater.inflate(R.menu.procedura_menu_list, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }


@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_procedure_list, container, false);

    Context context = view.getContext();
    setHasOptionsMenu(true);

    recyclerView = (RecyclerView) view.findViewById(R.id.rvProcedure);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);

    mAdapter = new ProcedureAdapter(context,procedure,this);

    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
    recyclerView.setAdapter(mAdapter);

    actionModeCallback = new ActionModeCallback();

    Toolbar toolbar = view.findViewById(R.id.toolbar);
    toolbar.setTitle(getString(R.string.title_lista_procedure) + " " + mCheckClass);
    toolbar.setNavigationIcon(R.drawable.ic_menu_navigator);
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener( new View.OnClickListener() {
        public void onClick(View v) {
            DrawerLayout drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);

            if(drawerLayout.isDrawerOpen(GravityCompat.START)){
                drawerLayout.closeDrawer(GravityCompat.START);
            } else {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        }
    });

    floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab_new);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            args.putString(ARG_PARAM_TIPOLOGIA,mParamTipologia); //Passa la procedura selezionata se siamo in modifica
            args.putString(ARG_PARAM_PROCEDURA,new Gson().toJson(procedure.get(lastPosition)));

            if(actionMode != null) //Leva la selezione se è presente, prima di passare ad un'altro fragment.
                enableActionMode(lastPosition);

            try {
                if ((getActivity()) != null) {
                    FragmentManager fragmentManager = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
                    Class fragmentClass = ProceduraFragment.class;
                    Fragment fragment = (Fragment) fragmentClass.newInstance();
                    fragment.setArguments(args);
                    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
                }
            } catch (java.lang.InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    });

   //TODO: Implements here the load logic for procedure.
    if(mParamProcedura != null && !(procedure.size() > 0)) {
        procedure.add(mParamProcedura);
    } else if(!(procedure.size() > 0)) {
        refreshList(AlphaMAS.getData());
    }

    return view;
}

3 个答案:

答案 0 :(得分:0)

解决方案

将背景设置为两个xml文件的父布局。

android:background="@color/background_color_which_you_want"

编辑1:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.procedura_menu_list, menu);
    return super.onCreateOptionsMenu(menu);
}

答案 1 :(得分:0)

为什么每次都要创建一个新的片段实例?尝试这样的事情:

if (fragment == null)
fragment = (Fragment) aclass.newInstance();

答案 2 :(得分:0)

在两个Fragment中的布局内部都使用此:

android:clickable="true"
android:focusable="true"