刷新标签(选项卡式活动)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷新标签(选项卡式活动)相关的知识,希望对你有一定的参考价值。

当我从“搜索活动”回来时,我正在尝试刷新我的三个标签。我正在寻找破坏它们并在onPause中重新创建它们但是不起作用。 “搜索活动”是我的主活动上的操作栏中的活动。当我在“搜索活动”中调用此方法时,我想刷新所有选项卡

    public void closeSearchActivity() {

    SharedPreferences.Editor editor = preferencias.edit();
    editor.putString("actual", xmlActual.toString());
    editor.putString("forecast", xmlForecast.toString());
    editor.commit();
    finish();
}

我的主要活动

package com.example.javier.meteojavier;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.io.File;


public class MT_Main extends AppCompatActivity {

private static final int COD_BUSCAR = 100;
private static final int COD_DEFECTO = 200;
private static final int COD_HOME = 300;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
File rutaCurrent;
File rutaForecast;
Fragment mt1;
Fragment mt2;
Fragment mt3;
Bundle argsInicial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mt__main);

    // Comprobar que archivo abrir la primera vez
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    mt1 = new MT_Tab1Actual();
    mt2 = new MT_Tab2Prediccion();
    mt3 = new MT_Tab3Historico();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == COD_BUSCAR) {
        if (resultCode == RESULT_OK) {

            if (data.hasExtra("ruta")) {
                //ruta = new File(data.getExtras().getString("ruta"));
                //Log.e("ruta", ruta.toString());
            }
        }
    }
    if (requestCode == COD_HOME) {
        if (resultCode == RESULT_OK) {
            if (data.hasExtra("forecast") && data.hasExtra("actual")) {
                argsInicial = new Bundle();
                argsInicial.putString("actual", data.getExtras().getString("actual"));
                argsInicial.putString("forecast", data.getExtras().getString("forecast"));
                mt1.setArguments(argsInicial);
                mt2.setArguments(argsInicial);
            }
        }
    }
}

// =========================== MENU THINGS // ===========================
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_mt__main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.action_settings:
            Intent intentPreferencias = new Intent(getApplicationContext(), MT_Preferencias.class);
            startActivity(intentPreferencias);
            break;
        case R.id.action_buscar:
            Intent intentBuscar = new Intent(getApplicationContext(), MT_Search.class);
            startActivityForResult(intentBuscar, COD_BUSCAR);
            break;
    }
    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return mt1;
            case 1:
                return mt2;
            case 2:
                return mt3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {

            case 0:
                return getString(R.string.tittle_Actual);
            case 1:
                return getString(R.string.tittle_Tiem);
            case 2:
                return getString(R.string.tittle_Hist);
        }
        return null;
    }
}

public void onResume() {

    super.onResume();
}

}

感谢大家!!对不起我的英语不好!

答案

从搜索活动回来时应该刷新适配器

viewPage.setAdapter( adapter );

以上是关于刷新标签(选项卡式活动)的主要内容,如果未能解决你的问题,请参考以下文章

Android - 选项卡式片段

使用选项卡式活动和片段时应用程序崩溃

Android 选项卡式活动通过 volley 添加新片段

ScrollView 在选项卡式活动中无法正常工作

如何在选项卡式活动中设置片段从右到左开始

在选项卡式应用程序中的何处放置位置代码?