Android - How do you efficiently load a large amount of text in a TextView?

此生再无相见时 提交于 2019-12-04 23:41:13

Essentially, the key is to only load the data that you need, when you need it. One way to do this would be to put every paragraph into it's own TextView, which is put into a ListAdapter, which is included into a ListView. There has to be some kind of an index set in place, such that each paragraph knows where in the data file to find. This interface will allow you to load only what you need, when you need it. Your list adapter looks something like this (This code isn't complete, but it should give you an idea at least of what you should do):

class ParagraphAdapter extends ListAdapter{
ArrayList<Integer> mLocations; // Somewhere define this to your locations, I'll leave that for you to figure out
protected View getView(int position,View convertView, ViewGroup parent)
{
  mLocations.get(position); // Read the file starting at this position, until the next value
  String text; // This is the output of the above
  TextView tv=new TextView(context);
  tv.setText(parent.getContext());
}
}

It can be noted that Amazon uses a system of paging for the Kindle App. If you have the app, you can see at the bottom of each page what section you are on. Each "page" is probably closer to a sentence or so in length. Then it's just a matter of getting the right page, which can fairly quickly be done.

To add on what @PearsonArtPhoto has said -

I suggest you implement some sort of paging mechanism, to divide your text into pages.
What you should do is split your text according to let's say N +M number of characters per pages.
N = fixed number of characters.
M = number of characters from N to nearest end of line character (so you won't see the last line being "cut").
I would suggest that if your android device allows you to hold this "in memory" -
do that,
and don't try to fetch this from the file one page after the other, but rather fetch from the "in memory" structure - this will improve performance.
Once you scroll and realize you need to fetch the next page, fetch it from the "in memory" structure.

Lucas Rocha built a nice library called Smoothie for that purpose.

http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

At the end of those performance tips for Android listview there's a link to an explanation about Smoothie and finally you'll find the library available on github.

Originally described for loading images, the approach applies for loading text as well.

You must to consider if you are using right components. Maybe it is mutch better to read lines and put them to listview.

try

List<String> lines; 
listview.adapter(new ArrayAdapter<String>(...,lines));

And many times that what you see (Look and Feel) is not like you are think.

You can use Android Paging Library from Jetpack with custom View or RecyclerView

https://developer.android.com/topic/libraries/architecture/paging

  1. Implement DataSource abstraction that provides List of data ranges
  2. Implement Storage abstraction that provides real data (from Database, Network, Files, etc)
  3. Use RecyclerView and Adapter to display data

Sample app to display content of large files can be found here https://github.com/YablokovDmitry/FileView

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