Android - 如何重新加载FragmentTabHost的内容

时间:2015-05-31 15:22:28

标签: android fragment-tab-host

所以我有一个扩展Fragment的类,其中有一个FragmentTabHost,我的FragmentTabHost由3个Tab组成,每个选项卡都会显示一个项目列表。 在每个项目中,我可以更新项目属性,如其名称/其他属性。 我的问题是在我更新值并按下后退按钮后,我的项目列表仍然包含我的项目值未更新的旧列表。仅当我切换到另一个选项卡并再次返回时,我的列表才会更新。 我一直在努力解决这个问题几个小时,非常感谢任何帮助谢谢

package com.example.hansel.tapbandung_v00.page.Menu_Parent;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.hansel.tapbandung_v00.R;
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_category;
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_featured;
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_newest;

/**
 * Created by Hansel on 5/25/2015.
 */
public class Business_parent extends Fragment{
private FragmentTabHost mTabHost;
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity().getApplicationContext();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    //3 tab category, dll
    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabhost);
    mTabHost.addTab(mTabHost.newTabSpec("featured").setIndicator("Featured"),
            bus_featured.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("category").setIndicator("Category"),
            bus_category.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("newest").setIndicator("Newest"),
            bus_newest.class, null);
    return mTabHost;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    mTabHost = null;
}

}

1 个答案:

答案 0 :(得分:0)

我一直在努力解决同样的问题,最后我提出了这样的解决方案:

mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab1Fragment()).commit();
            mTabHost.setCurrentTab(0);
        }
    });

    mTabHost.getTabWidget().getChildAt(1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab2Fragment()).commit();
            mTabHost.setCurrentTab(1);
        }
    });

    mTabHost.getTabWidget().getChildAt(2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab3Fragment()).commit();
            mTabHost.setCurrentTab(2);
        }
    });

有效。我不确定这是一个非常优雅的解决方案。