Android 安卓-点击一次就可以改变页面中的页面片段
使用此适配器,我在Android 安卓-点击一次就可以改变页面中的页面片段,android,android-fragments,android-viewpager,android-tabhost,Android,Android Fragments,Android Viewpager,Android Tabhost,使用此适配器,我在TabHost中获得了一个ViewPager: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_home, container, false); mTabHost = (TabHost)
TabHost
中获得了一个ViewPager
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager) v.findViewById(R.id.pager_home);
mTabsAdapter = new TabsAdapter(getActivity(), mTabHost, mViewPager);
// Here we load the content for each tab.
mTabsAdapter.addTab(mTabHost.newTabSpec("one").setIndicator("Recently Viewed"), PageOne.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("two").setIndicator("Offers"), PageTwo.class, null);
//mTabsAdapter.addTab(mTabHost.newTabSpec("two").setIndicator("Experiences"), ExperiencesTab.class, null);
return v;
}
public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo
{
private final String tag;
private final Class<?> clss;
private final Bundle args;
TabInfo(String _tag, Class<?> _class, Bundle _args)
{
tag = _tag;
clss = _class;
args = _args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory
{
private final Context mContext;
public DummyTabFactory(Context context)
{
mContext = context;
}
public View createTabContent(String tag)
{
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager)
{
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
{
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public int getCount()
{
return mTabs.size();
}
@Override
public Fragment getItem(int position)
{
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
public void onTabChanged(String tabId)
{
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void onPageSelected(int position)
{
// Unfortunately when TabHost changes the current tab, it kindly
// also takes care of putting focus on it when not in touch mode.
// The jerk.
// This hack tries to prevent this from pulling focus out of our
// ViewPager.
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
public void onPageScrollStateChanged(int state)
{
}
}
但是选项卡仍然是空的,里面什么也没有显示。
如您所见,我还需要将新片段放在后堆栈中,这样当我按back时,将再次加载上一个片段。u可以使用mTabHost.setCurrentTab(1);在选项卡中显示初始片段时,prolem不可用。我可以通过列表视图看到片段。问题是如何在单击列表项后切换同一选项卡内的片段。ViewPager应负责片段的生命周期,我将更改
getItem(int position)
函数,以考虑您在onclick函数上设置的全局变量,以便像现在一样创建片段,或者创建所需的新片段,然后刷新适配器。@EvripidisDrakos我需要将片段添加到后堆栈中,以便在按“上一步”按钮时,加载上一个片段。您的解决方案听起来好像没有处理好它。然后只需覆盖“上一步”按钮,再次更新该全局变量,并再次调用adpater上的notifyDataChanged(其getItemPosition返回POSITION_NONE,以使其正常工作)。这就是我无论如何都会尝试的。如果您不想这样做,可以尝试使用getChildFragmentManager()
而不是getSupportFragmentManager()
在哪里添加该代码?它将如何加载我想要的新片段?
widget.getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tHost.setCurrentTab(0);
((ViewPager) mViewPager).setCurrentItem(0);
/// perform fragment transaction here
}
});
widget.getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tHost.setCurrentTab(0);
((ViewPager) mViewPager).setCurrentItem(0);
/// perform fragment transaction here
}
});