每次选择选项卡时,tabfragment都会重新加载

时间:2016-07-01 00:15:10

标签: android tabs android-tabhost

我有3个tabfragments附加到tabhost,当我将数据放入textview然后我选择另一个选项卡时,第一个选项卡的数据被删除,但我意识到当我选择一个选项卡时,片段会重新加载。

我的问题是如何在不重新加载内容的情况下选择标签? 选择另一个选项卡后,当我选择相同的选项卡时,将清除textview的值。

我创建所有标签的主要活动

 private void initTabs()
{

    boolean createDoneActivityBtn = true;

    thMain = ( FragmentTabHost ) findViewById( R.id.thMain );


    thMain.setup( this, getSupportFragmentManager(), R.id.flTabContent );

    thMain.addTab( thMain.newTabSpec( "AddLabourCropFieldTab" ).setIndicator( "Cultivos" )
            , AddLabourCropFieldTab.class , null);

    thMain.addTab( thMain.newTabSpec( "AddLabourDoneActivityTab" ).setIndicator( "Info. General" )
            , AddLabourDoneActivityTab.class , null);

    if(  activityName.equals( "Riego" ) )
    {
        thMain.addTab( thMain.newTabSpec( "AddLabourIrrigationTab" ).setIndicator( "Info. de Riego" )
                , AddLabourIrrigationTab.class , null);
    }


    if(  activityName.equals( "Siembra" ) )
    {
        thMain.addTab(thMain.newTabSpec("AddLabourSowingTab").setIndicator("Info. de Siembra")
                , AddLabourSowingTab.class, null);
    }

    if(  activityName.equals( "Fertilizacion" ) )
    {
        thMain.addTab(thMain.newTabSpec("AddLabourInputTab").setIndicator("Info. de Insumo")
                , AddLabourInputTab.class, null);
    }


}//

第一个标签,

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        this.setRetainInstance( true );

        initCropFieldTabContent();


    }//

private void initCropFieldTabContent()
{
    AddLabour act = (AddLabour) getActivity();

    AlertDialog.Builder builder = new AlertDialog.Builder( act );
    builder.setTitle("A que cultivos se aplicara la labor?");

    List<CustCropField> cropFieldList = CustCropField.listAll( CustCropField.class );

    Log.d( "initcrop", " valor del listado " + cropFieldList.size() );

    ListAdapter cropfieldAdapter =
            new ArrayAdapter( act.getApplicationContext() , R.layout.spinner_row, cropFieldList );

        //lvCropfield = (ListView)act.findViewById( R.id.lvCropfield );
        lvCropfield = new ListView( act );
    try
    {
        lvCropfield.setAdapter( cropfieldAdapter );
        lvCropfield.setChoiceMode( ListView.CHOICE_MODE_MULTIPLE );
        lvCropfield.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                CustCropField ccf = ( CustCropField ) parent.getItemAtPosition( position );
                Log.d( "lvpersonal","click al item: "+position+" nombre: "+ccf.toString() );

                if ( cropFieldSelected.contains( ccf ) )
                {
                     cropFieldSelected.remove( ccf );
                    Log.d( "lvpersonal","removido crop "+ccf.getName() );
                }
                else
                {
                     cropFieldSelected.add( ccf );
                    Log.d( "lvpersonal","agregado crop"+ccf.getName() );
                }

                showTextViewContent( R.id.tvCropFieldList );

            }//
        });
        lvCropfield.setVisibility( View.VISIBLE );
    }
    catch(Exception e)
    {
        Log.e( "error inp act"," error en activityinput-> "+e.getMessage() );
    }

    builder.setView( lvCropfield );
    cropDialog = builder.create();

    //aqui el otro listado ( el de personal )
    builder = new AlertDialog.Builder( act );
    builder.setTitle("personal que aplicara la labor?");

    List<Personal> personalList = Personal.listAll( Personal.class );

    ListAdapter personalAdapter =
            new ArrayAdapter( act.getApplicationContext() , R.layout.spinner_row, personalList );


    //lvPersonal = ( ListView )act.findViewById( R.id.lvPersonal );
    lvPersonal = new ListView( act );
    try
    {
        lvPersonal.setAdapter( personalAdapter );
        lvPersonal.setChoiceMode( ListView.CHOICE_MODE_MULTIPLE );
        lvPersonal.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Personal per = ( Personal ) parent.getItemAtPosition( position );
                Log.d( "lvpersonal","click al item: "+position+" nombre: "+per.toString() );

// CustCropField ccf =(CustCropField)parent.getItemAtPosition(position); // Log.d(&#34; lvpersonal&#34;,&#34;点击al item:&#34; + position +&#34; nombre:&#34; + ccf.toString());

                if ( personalSelected.contains( per ) )
                {
                    personalSelected.remove( per );
                    Log.d( "lvpersonal","removido personal "+per.getName() );
                }
                else
                {
                    personalSelected.add( per );
                    Log.d( "lvpersonal","agregadp personal "+per.getName() );
                }

                showTextViewContent( R.id.tvPersonalList );

            }//
        });//onitemclicklistener
        lvPersonal.setVisibility( View.VISIBLE );
    }
    catch(Exception e)
    {
        Log.e( "error inp act"," error en activityinput-> "+e.getMessage() );
    }

    builder.setView( lvPersonal );

    personalDialog = builder.create();

    personalBtn = ( Button )act.findViewById( R.id.personalBtn );
    personalBtn.setOnClickListener( this );
    cropfieldBtn = ( Button )act.findViewById( R.id.cropfieldBtn );
    cropfieldBtn.setOnClickListener( this );


    //se crean los listados de los items seleccionados
    cropFieldSelected = new ArrayList<>();

    personalSelected = new ArrayList<>();

    tvCropFieldList = ( TextView )act.findViewById( R.id.tvCropFieldList );
    tvPersonalList = ( TextView )act.findViewById( R.id.tvPersonalList );


    //se pasan las listas con los campos y personal
    //seleccionados a la actividad principal
    act.setCcfList( cropFieldSelected );
    act.setPerList( personalSelected );


}//

1 个答案:

答案 0 :(得分:0)

ViewPagers保持:

您当前的标签&amp;内存中任意一侧的1个标签

所以当你在1和2之间切换时,引擎盖下什么也没发生 - 就像它们只是被隐藏/显示一样。

使用setOffscreenPageLimit()