[英]Android - Change fragment in a pager that in a tab by a click
I got a ViewPager
inside a TabHost
with this adapter: 我有一个
ViewPager
一个内部TabHost
该适配器:
@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)
{
}
}
PageOne
is a fragment which contains a ListView
. PageOne
是一个片段,其中包含ListView
。 I want to click on a list item and then change the fragment into a new fragment and pass it information on where I clicked so the fragment will be opened with the data of which item list exactly have I pushed. 我想单击一个列表项,然后将该片段更改为一个新片段,并将其信息传递给我单击的位置,以便将打开的片段中包含我已正确推入的项目列表的数据。
for now I'm stuck at just switching the fragments (even before also sending the information about the clicked list item). 现在,我只停留在切换片段(甚至在还发送有关单击列表项的信息之前)。 I've tried this:
我已经试过了:
myList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment f = new SingleConversationFragment();
ft.replace(R.id.pager_home, f);
ft.addToBackStack(null);
ft.commit();
}
});
but then the tab remains empty and nothing is shown inside it. 但是该标签仍然为空,并且里面没有任何显示。 As you can see, I also need the new fragment to be at the back stack so when I press back, the previous fragment will be loaded again.
如您所见,我还需要将新的片段放在后堆栈中,因此当我按返回时,将再次加载前一个片段。
widget.getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tHost.setCurrentTab(0);
((ViewPager) mViewPager).setCurrentItem(0);
/// perform fragment transaction here
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.