gpt4 book ai didi

android - 与 TabHost 相比,ViewPager 和 Fragments 的行为不一致

转载 作者:行者123 更新时间:2023-11-30 02:47:17 26 4
gpt4 key购买 nike

我正在尝试实现四个选项卡的 ViewPager,因为 TabHost 不支持滑动。这里的问题是,我在每个 fragment 的 OnCreateView 中都有一些异步调用,并且除了我在使用 ViewPager 时滑动到的调用之外,还进行了来自下一个 fragment 的调用。

然而,对于 TabHost,情况并非如此。仅从选定的选项卡进行调用。是我应该解决的 ViewPager 问题还是我对 Fragment 使用了错误的回调方法?

这是项目的部分代码:

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
private PagerAdapter mPagerAdapter;

private class TabInfo {
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}

}

class TabFactory implements TabContentFactory {

private final Context mContext;

/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}

public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}

}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout
setContentView(R.layout.activity_main);
// Initialise the TabHost
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
// Intialise ViewPager
this.intialiseViewPager();
}

protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
super.onSaveInstanceState(outState);
}

private void intialiseViewPager() {

List<Fragment> fragments = new Vector<Fragment>();
fragments.add(new ActivityFeedFragment());
fragments.add(new InboxFragment());
fragments.add(new TaskFragment());
fragments.add(new CalendarEventFragment());
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);

this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}

private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", ActivityFeedFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), ( tabInfo = new TabInfo("Tab2", InboxFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), ( tabInfo = new TabInfo("Tab3", TaskFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("Tab 4"), ( tabInfo = new TabInfo("Tab4", CalendarEventFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);

mTabHost.setOnTabChangedListener(this);
}

private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}

public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos, true);
}

@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);
}

@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub

}
}

此外,这是我正在使用的适配器

public class PagerAdapter extends FragmentStatePagerAdapter {

private List<Fragment> fragments;

public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

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

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

如果有关于在 TabHost 中启用滑动手势的任何建议,我也愿意尝试。

最佳答案

默认 ViewPager在当前页面之外创建并保留一个页面,如果您不想这样做,可以 setOffscreenPageLimitViewPager到 0

viewPager.setOffscreenPageLimit(0);

--

另一种解决方案是 setOnPageChangeListener到 viewpager 并在选择 fragment 时触发异步调用

private static class CustomOnPageChangeListener extends SimpleOnPageChangeListener
{
@Override
public void onPageSelected(int position)
{
switch(position){
case 1:
FirstFragment fragment1 = (FirstFragment) = FirstFragment.getInstance();
fragment1.startAsyncProcess();
break;
case 2:
SecondFragment fragment2 = (SecondFragment) = SecondFragment.getInstance();
fragment2.startAsyncProcess();
break;
}

super.onPageSelected(position);
}
}

...

viewPager.setOnPageChangeListener(new CustomOnPageChangeListener());

关于android - 与 TabHost 相比,ViewPager 和 Fragments 的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24818308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com