文本分页 android,pagination - How to paginate long text into pages in Android? - Stack Overflow

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> sharedObjects = new HashMap>();

public synchronized Long putSharedObject(Object o) {

long key;

do {

key = random.nextLong();

} while (sharedObjects.containsKey(key));

sharedObjects.put(key, new WeakReference(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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值