Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android fragments TabHost内的RecyclerView仅在选项卡更改时刷新_Android Fragments_Android Viewpager_Android Tabhost_Android Adapter_Android Recyclerview - Fatal编程技术网

Android fragments TabHost内的RecyclerView仅在选项卡更改时刷新

Android fragments TabHost内的RecyclerView仅在选项卡更改时刷新,android-fragments,android-viewpager,android-tabhost,android-adapter,android-recyclerview,Android Fragments,Android Viewpager,Android Tabhost,Android Adapter,Android Recyclerview,我正在使用一个带有TabWidget的TabHost和一个ViewPager。对于ViewPager,我创建了FragmentPagerAdapter,并在其中放置了一些片段。在每个片段中,我都有一个RecyclerView来垂直和水平显示信息 当我从一个tabwidget更改时,片段正在更新,但当活动加载时,片段不会更新 图形行为 例如,我检查我的产品列表我购买此产品,然后在“其他”选项卡中,我的产品列表应该列出,但它没有列出。当我更改选项卡并返回到以前的选项卡时,视图将更新,产品列表将显示

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

当我从一个tabwidget更改时,片段正在更新,但当活动加载时,片段不会更新

图形行为 例如,我检查我的产品列表我购买此产品,然后在“其他”选项卡中,我的产品列表应该列出,但它没有列出。当我更改选项卡并返回到以前的选项卡时,视图将更新,产品列表将显示

好的,这是我选择产品的第一个选项卡,在第一个选项卡中,我有一个带有FragmentPagerAdapter的ViewPager。产品列表是一个回收视图。当我选择一个产品时,启动一个活动,然后我购买该产品

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

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

主要活动代码 这是我的主要活动代码:

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;
    }
}
}
斑翅目 我使用我的片段列表

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;
}
}
公共类选项卡SpagerAdapter扩展了FragmentPagerAdapter{
列表碎片列表;
公共选项卡SpagerAdapter(FragmentManager fm){
超级(fm);
fragmentList=新的ArrayList();
添加(新产品片段());
添加(新的BuyListFragment());
添加(新的DetailFragment());
}
@凌驾
公共片段getItem(int位置){
返回碎片列表。获取(位置);
}
@凌驾
public int getCount(){
返回fragmentList.size();
}
@凌驾
public int getItemPosition(对象){
返回位置\无;
}
}
提示
每个片段使用布局实现其视图。布局包含一些小部件,其中一个是RecyclerView。我只共享TabHost、ViewPager和适配器代码,因为我认为问题就在这里,当我更改选项卡时,列表是paint。我迷路了,我不知道问题出在哪里。

这个问题很老了,但是因为没有人回答,我想告诉你是什么解决了这个问题

你的错误可能在这里:

tabSpec.setContent(新的FakeView(getApplicationContext())

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

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