【问题标题】:RecyclerView inside TabHost only refresh when tab changeTabHost 内的 RecyclerView 仅在选项卡更改时刷新
【发布时间】:2016-03-08 17:25:57
【问题描述】:

我在 ViewPager 中使用带有 TabWidget 的 TabHost。对于 ViewPager,我创建了 FragmentPagerAdapter,并在里面放了一些片段。在每个片段中,我都有一个 RecyclerView 来垂直和水平显示信息。

当我从一个 tabwidget 更改时 Fragment 正在更新,但在加载 Activity 时不会更新。

图形行为

例如,我检查我的产品列表,我买了这个产品,然后在其他选项卡中我的产品列表应该列出但它没有,当我更改选项卡并返回到我以前的选项卡然后视图正在更新并显示产品列表。

好的,这是我选择产品的第一个选项卡,在选项卡 One 中,我有一个带有 FragmentPagerAdapter 的 ViewPager。产品列表是一个 RecyclerView。当我选择产品时,会启动一个 Activity,然后我会购买该产品。

当我购买产品时,我会返回我的主 Activity(TabHost 容器),我应该会在列表中看到购买的产品,但列表为空。

当我更改选项卡并返回到列表选项卡时,会显示片段。我不知道这是我的错误。

主Activity代码

这是我的主要活动代码:

public class AppTabActivity extends AppCompatActivity
        implements ViewPager.OnPageChangeListener,
        TabHost.OnTabChangeListener {

public static final String TAB_INDEX = "TAB_INDEX";
ViewPager pager;
TabPagerAdapter tabsPagerAdapter;
TabHost tabHost;
Toolbar topToolBar;

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

    topToolBar = (Toolbar) findViewById(R.id.idTopToolBar);
    setSupportActionBar(topToolBar);

    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();

    String[] tabNames = getResources().getStringArray(R.array.tabs_name);
    for (String tab : tabNames) {
        TabHost.TabSpec tabSpec;
        tabSpec = tabHost.newTabSpec(tab);
        tabSpec.setIndicator(tab);
        tabSpec.setContent(new FakeView(getApplicationContext()));
        tabHost.addTab(tabSpec);
    }
    tabHost.setOnTabChangedListener(this);

    pager = (ViewPager) findViewById(R.id.viewPager);

    if(tabsPagerAdapter == null )
        tabsPagerAdapter = new TabPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(tabsPagerAdapter);
    tabsPagerAdapter.notifyDataSetChanged();
    pager.addOnPageChangeListener(this);

    Intent from = getIntent();
    //when i buy a product i send the tab list index of my bought products
    int currentIndex = from.getIntExtra(TAB_INDEX, 1);
    pager.setCurrentItem(currentIndex);

}

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

}

@Override
public void onPageSelected(int position) {
    tabHost.setCurrentTab(position);
    pager.setCurrentItem(position);
    tabsPagerAdapter.notifyDataSetChanged();
}

@Override
public void onPageScrollStateChanged(int state) {

}

@Override
public void onTabChanged(String tabId) {
    int selectedItem = tabHost.getCurrentTab();
    pager.setCurrentItem(selectedItem);
}

class FakeView implements TabHost.TabContentFactory {

    Context context;

    public FakeView(Context ctx) {
        context = ctx;
    }

    @Override
    public View createTabContent(String name) {

        View view = new View(context);
        view.setMinimumHeight(0);
        view.setMinimumWidth(0);
        return view;
    }
}
}

TabPagerAdapter

我使用我的片段列表。

public class TabsPagerAdapter extends FragmentPagerAdapter  {

List<Fragment> fragmentList;

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
    fragmentList = new ArrayList<>();
    fragmentList.add(new ProductFragment());
    fragmentList.add(new BuyListFragment());
    fragmentList.add(new DetailFragment());
}

@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

@Override
public int getCount() {
    return fragmentList.size();
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}
}

提示

每个片段都使用布局实现其视图。布局包含一些小部件,其中之一是 RecyclerView。我只分享 TabHost、ViewPager 和 Adapter 代码,因为我认为问题出在此处,当我更改选项卡时,列表是绘制的。我迷路了,我不知道问题出在哪里。

【问题讨论】:

    标签: android-fragments android-viewpager android-tabhost android-adapter android-recyclerview


    【解决方案1】:

    这个问题很老了,但由于没有人回答,我想告诉我是什么解决了这个问题。

    你的错误可能在这里:

    tabSpec.setContent(new FakeView(getApplicationContext()));

    您不能将视图设置为“内容”。而是使用 TabContentFactory:

    tabSpec.setContent(new TabHost.TabContentFactory() {
               @Override
                public View createTabContent(String tag) {
                    return findViewById(R.id.your_view);
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多