我在ViewPager
内有一个Fragment
。在ViewPager
内是片段。 ViewPager
中的片段显示在黑屏上。我试过了:
FragmentPagerAdapter
切换为FragmentStatePagerAdapter
getItemId
方法以及我在网上发现的其他各种事情。有谁知道为什么会这样?
这是我的适配器:
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return ratedFrag;
case 1:
return unratedFrag;
case 2:
return collectionFrag;
default:
return ratedFrag;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Rated";
case 1:
return "To Rate";
case 2:
return "Collections";
default:
return "Rated";
}
}
@Override
public long getItemId(int position) {
return 100 + position;
}
};
实现寻呼机的编辑片段:
public class JournalActivity extends MasterFragment {
private final static String TAG = "JournalActivity";
@InjectView(R.id.pager)
private ViewPager pager;
private EntryFragment ratedFrag;
private EntryFragment unratedFrag;
private CollectionFragment collectionFrag;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_journal, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
protected void setupViews() {
ratedFrag = new EntryFragment();
unratedFrag = new EntryFragment();
collectionFrag = new CollectionFragment();
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return ratedFrag;
case 1:
return unratedFrag;
case 2:
return collectionFrag;
default:
return ratedFrag;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Rated";
case 1:
return "To Rate";
case 2:
return "Collections";
default:
return "Rated";
}
}
@Override
public long getItemId(int position) {
return 100 + position;
}
};
pager.setAdapter(adapter);
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) getView().findViewById(R.id.tabs);
tabs.setViewPager(pager);
tabs.setIndicatorColor(getResources().getColor(R.color.green));
tabs.setIndicatorHeight(5);
tabs.setTextColor(Color.WHITE);
tabs.setTextSize((int)getResources().getDimension(R.dimen.font_medium));
tabs.setTypeface(Fonts.getInstance().secondaryFontBold, Typeface.BOLD);
}
}
以下是EntryFragment
类:
public class EntryFragment extends MasterFragment {
private final static String TAG = "EntryFragment";
private String color;
private List<Entry> entries;
private WineListAdapter listAdapter;
@InjectView(R.id.search_container)
private LinearLayout searchContainer;
@InjectView(R.id.search_view)
private SearchView searchView;
@InjectView(R.id.filter_button)
private SecondaryButton filterButton;
@InjectView(R.id.list)
private ListView listView;
private OnFragmentInteractionListener mListener;
public static EntryFragment newInstance() {
EntryFragment fragment = new EntryFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public EntryFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.entries = (List<Entry>)getArguments().get("entries");
}
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_entry, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// getActivity() can be null if called before onAttach
listAdapter = new WineListAdapter(getActivity(),
entries != null ? entries : new ArrayList<Entry>(),
SortType.RATING);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mListener.entryWasSelected((Entry) adapterView.getItemAtPosition(i));
}
});
}
}