我在 ViewPager 中使用带有 TabWidget 的 TabHost。对于 ViewPager,我创建了我的 FragmentPagerAdapter 并在里面放了一些片段。在每个 Fragment 中,我都有一个 RecyclerView 来垂直和水平显示信息。
当我从一个 tabwidget 更改时片段正在更新,但在加载 Activity 时没有更新。
图形行为
例如,我检查了我的产品列表,我在购买该产品时取货,然后在其他选项卡中应该列出我的产品列表,但它没有,当我更改选项卡并返回到我之前的选项卡时,然后查看正在更新并显示产品列表。
好的,这是我选择产品的第一个选项卡,在第一个选项卡中,我有一个带有 FragmentPagerAdapter 的 ViewPager。产品列表是一个 RecyclerView。当我选择一个产品时,会启动一个事件,然后我购买该产品。
当我购买产品时,我返回到我的主要 Activity(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;
}
}
}
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;
}
}
提示
每个片段都使用布局实现其 View 。布局包含一些小部件,其中之一是 RecyclerView。我只共享 TabHost、ViewPager 和 Adapter 代码,因为我认为问题出在这里,当我更改选项卡时,列表正在绘制。我迷路了,我不知道是什么问题。
最佳答案
这个问题很老了,但是没人回答,我想说说是什么为我解决了这个问题。
你的错误可能在这里:
tabSpec.setContent(new FakeView(getApplicationContext()));
您不能将 View 设置为“内容”。而是使用 TabContentFactory:
tabSpec.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
return findViewById(R.id.your_view);
}
});
关于android-fragments - TabHost 内的 RecyclerView 仅在选项卡更改时刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873958/