我浏览标签后,我在列表中的列表视图内容(使用视图寻呼机的可切换的tans)似乎消失了。我有三个选项卡Polygon,Line,Point。一旦我浏览列表视图中的所有三个选项卡内容消失。我通过将数据从活动传递到每个片段来获取我的内容以填充listview。 `
class ViewPagerAdapter extends FragmentPagerAdapter {
int noOfTabs;
public ViewPagerAdapter(FragmentManager fm, int noOfTabs ) {
super(fm);
this.noOfTabs = noOfTabs;
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
switch (position) {
case 0:
SavedPolygonFragment polyFrag = new SavedPolygonFragment();
for (int i = 0; i<savedShapes.size(); i++) {
bundle.putParcelable("extras" + i, savedShapes.get(i));
}
polyFrag.setArguments(bundle);
return polyFrag;
case 1:
SavedLineFragment lineFrag = new SavedLineFragment();
for (int i = 0; i<savedLines.size(); i++) {
bundle.putParcelable("extras" + i, savedLines.get(i));
}
lineFrag.setArguments(bundle);
return lineFrag;
case 2:
SavedPointFragment pointFrag = new SavedPointFragment();
for (int i = 0; i<savedPois.size(); i++) {
bundle.putParcelable("extras" + i, savedPois.get(i));
}
pointFrag.setArguments(bundle);
return pointFrag;
default:
return null;
}
}
@Override
public int getCount() {
return noOfTabs;
}
}`
这是我从中获取所有三个选项卡的数据库数据的活动。
我的 SavedPolygonFragment.java 看起来像这样
package com.miraglo.drawing.fragment;
public class SavedPolygonFragment extends android.support.v4.app.Fragment {
private static String LOG_TAG = SavedPolygonFragment.class.getName();
private String message = "No message";
private ListView shapeList;
private SavedShapeAdapter savedShapeAdapter;
private long projId;
private float scaleFactor;
private List<Shape> savedShapes = new ArrayList<>();
public SavedPolygonFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
for (String key : getArguments().keySet()) {
savedShapes.add(getArguments().<Shape>getParcelable(key));
}
shapeList = (ListView) view.findViewById(R.id.shapeList);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getActivity(), "Here poly create",Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Here",Toast.LENGTH_SHORT).show();
if (savedShapeAdapter != null) {
savedShapeAdapter.notifyDataSetChanged();
} else {
savedShapeAdapter = new SavedShapeAdapter(savedShapes, getActivity());
shapeList.setAdapter(savedShapeAdapter);
shapeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), " " + String.valueOf(((Shape) view.getTag()).getShapeId()), Toast.LENGTH_SHORT).show();
}
});
}
}
}
答案 0 :(得分:4)
mViewPager.setOffscreenPageLimit(2);
感谢this post。花了一段时间才找到它。