我认为,我一整天都在与这个问题作斗争,但仍然无法做到正确。 我正在为我的一个朋友建立一些应用程序。 我正在使用ActionBarSherlock,因此我可以使用操作栏和其他一些内容,例如pageviewer。
无论如何,我想要进行主要活动:3个标签,每个标签显示一个片段(3个不同的片段),每个片段必须显示一个fragmentPagerView,这样我就可以来回滚动片段(也许我'我得到这个后,在某个时刻添加“永久滚动”)
问题是我已经启动并运行了标签(我从apiDemos复制了一些代码) 但是我无法显示标签,我找不到原因(它甚至没有崩溃)
这是我的MainActivity,它包含navActionbar(和一些选项)
public class MainActivity extends SherlockFragmentActivity
{
SherlockFragment yesTabFragment = new YesTabFragment();
SherlockFragment newTabFragment = new NewTabFragment();
SherlockFragment trendingTabFragment = new TrendingTabFragment();
private void createTabs()
{
// put ActionBar to nav. mode
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab yesTab = actionBar
.newTab()
.setText("yes")
.setTabListener(new TabListener<YesTabFragment>(
this, "yes", YesTabFragment.class));
actionBar.addTab(yesTab);
ActionBar.Tab trendingTab = actionBar
.newTab()
.setText("trending")
.setTabListener(new TabListener<TrendingTabFragment>(
this, "trending", TrendingTabFragment.class));
actionBar.addTab(trendingTab);
ActionBar.Tab newTab = actionBar
.newTab()
.setText("new")
.setTabListener(new TabListener<NewTabFragment>(
this, "new", NewTabFragment.class));
actionBar.addTab(newTab);
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
createTabs();
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_activity_menu, menu);
return true;
}
public static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener
{
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private SherlockFragment mFragment;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz)
{
this(activity, tag, clz, null);
}
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz, Bundle args)
{
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = (SherlockFragment)mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached())
{
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
if (mFragment == null)
{
mFragment = (SherlockFragment)SherlockFragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
}
else
{
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
if (mFragment != null)
{
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
//Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
}
正如我所说,我直接从apiDemos复制了TabListener实现(虽然我写的那个完全相同)
这是一个标签片段的实现(它们都是相同的)
public class YesTabFragment extends SherlockFragment
{
View _fragmentView;
private Vector<stuff> list;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.inflate(R.layout.yes_fragment, container, false);
ViewPager pager = (ViewPager)_fragmentView.findViewById(R.id.gag_pager);
API api = API.getInstance();
list = api.get("Yes", 1, 10);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), listOfGags);
pager.setAdapter(adapter);
return _fragmentView;
}
}
yesTabFragment类的实现
public class YesTabFragment extends SherlockFragment
{
View _fragmentView;
private Vector<things> list;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.inflate(R.layout.yes_fragment, container, false);
ViewPager pager = (ViewPager)_fragmentView.findViewById(R.id.thing_pager);
API api = API.getInstance();
list = api.get("Yes", 1, 10);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), list);
pager.setAdapter(adapter);
return _fragmentView;
}
}
当然主要和是标签的XML(标签几乎相同)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
和yesTab
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/thing_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
我真的找不到标签片段不替换framelayout并向我显示viewpager的原因。
我真的可以在这里使用一些好的建议,因为我肯定错过了一些大的东西,无法理解...
谢谢你......
答案 0 :(得分:3)
您的MainActivity应包含ViewPager防御,例如:
viewPager=(ViewPager)findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener =
new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionBar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listner to the viewPager */
viewPager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
fragmentPagerAdapter = new PhotoMugFragmentPagerAdapter(fm);
/** Setting the FragmentPagerAdapter object to the viewPager object */
viewPager.setAdapter(fragmentPagerAdapter);