问题
I know this question has been asked on stackoverflow, (Link To Original Question)
I was following the solution provided there, and got a few errors. (I wanted to ask in comment there only, but think I don't have enough reputation to ask in comment of a marked answer) My original question is same, i.e. "I want to paginate long text"
Errors encountered in the provided answer(link posted above) - 1. After telling about PageSplitter class, solution provider (mixel) stated this -
...Then by using PageSplitter.getPages() method you can get original text splitted to pages and put each of them into TextView:
pageView.setAdapter(new TextPagerAdapter(getSupportFragmentManager(), pageSplitter.getPages()));
I think here he meant pagesView
. (my real concern is written below, mentioning it just in case i missed something else.)
- This is the real problem I am facing - In the PageFragment class, I am getting this error -
The method getObjectStorage() is undefined for the type PageFragment
while Mixel states about getObjectStorage() -
getObjectStorage() returns singleton SharedObjectStorage that stores map from keys to weak references to objects (this is useful when you want to put custom object to Bundle):
and I have created a separate class named SharedObjectStorage, as mentioned by Mixel, but I am still getting the error.
PageFragment
public class PageFragment extends Fragment {
private final static String PAGE_TEXT = "PAGE_TEXT";
public static PageFragment newInstance(CharSequence pageText) {
PageFragment frag = new PageFragment();
Bundle args = new Bundle();
args.putLong(PAGE_TEXT, getObjectStorage().putSharedObject(pageText));
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CharSequence text = (CharSequence) getObjectStorage().getSharedObject(getArguments().getLong(PAGE_TEXT));
TextView pageView = (TextView) inflater.inflate(R.layout.page, container, false);
pageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size));
pageView.setText(text);
return pageView;
}
}
SharedObjectStorage
public class SharedObjectStorage {
private final Random random = new Random(Calendar.getInstance().getTimeInMillis());
private HashMap<Long, WeakReference<Object>> sharedObjects = new HashMap<Long, WeakReference<Object>>();
public synchronized Long putSharedObject(Object o) {
long key;
do {
key = random.nextLong();
} while (sharedObjects.containsKey(key));
sharedObjects.put(key, new WeakReference<Object>(o));
return key;
}
public synchronized Object getSharedObject(long key) {
if (sharedObjects.containsKey(key)) {
return sharedObjects.get(key).get();
}
return null;
}
}
Is there anyway to work around this?
p.s. - I didn't post the answer here, as it's already on stackoverflow itself. But, if you still ask me to, I will. Thanks.
来源:https://stackoverflow.com/questions/24825063/how-to-paginate-long-text-into-pages-in-android