android viewpager to contain a long text separated to pages

荒凉一梦 提交于 2019-12-08 06:58:33

问题


I have a viewPager in my app. I want to put a long block of text in the viewPager. I don't want the text to be scrollable, I want it to be separated or parted to pages, and when the user pages it shows the rest of text in the other pages.

How can I approach this?


回答1:


You can instantiate a number of Fragments based on the dimension of the text, measured via public float measureText (String text). Then you determine the screen as stated in Get screen dimensions in pixels

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

And this way you can calculate the number of fragments you need.
For the splitting part the quickest solution I can think of would be a loop with measureText (String text, int start, int end) to determine where the text has to be split. (There might be more elegant solutions to this)




回答2:


Android use (Boring|Static|Dynamic)Layout classes to measure and wrap text, Theses class take width as constructor param and will calc the total height of you text, they also provide methods like getLineHeight(int line) to get line info, so you can calculate String offset according to these info. I create a class to do this, see my answer here: https://stackoverflow.com/a/30468884/2800351



来源:https://stackoverflow.com/questions/22199331/android-viewpager-to-contain-a-long-text-separated-to-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!