【问题标题】:Android - How to reload a content of a FragmentTabHostAndroid - 如何重新加载 FragmentTabHost 的内容
【发布时间】:2015-05-31 15:22:28
【问题描述】:

所以我有一个扩展 Fragment 的类,其中我有一个 FragmentTabHost,我的 FragmentTabHost 由 3 个选项卡组成,每个选项卡将显示一个项目列表.. 在每个项目中,我可以像它的名称/其他属性一样更新项目属性。 我的问题是在我更新值并按下后退按钮后,我的项目列表仍然包含我的项目值未更新的旧列表。只有当我切换到另一个选项卡并再次返回时,我的列表才会更新。 我已经为这个问题苦苦挣扎了几个小时,任何帮助将不胜感激谢谢

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;
}

}

【问题讨论】:

  • 我有一个类似的查询:我想在项目更新时重新加载所有选项卡主机。我的列表项可以根据更新从一个列表切换到另一个列表,因此每次在其中一个选项卡中更改项目时,我都需要更新整个选项卡主机。

标签: android fragment-tab-host


【解决方案1】:

我一直在为同样的问题苦苦挣扎,最后我想出了这样一个解决方案:

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);
        }
    });

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多