Android的 - 你如何有效地加载一个TextView大量的文本呢?有效地、加载、文本、Android

2023-09-07 08:45:52 作者:清酒

我正在写一个Android应用程序读取一个文本文件,并在的TextView 显示它。

I'm writing an Android app that reads a single text file and display it on a TextView.

我在做什么,现在是读取整个文件转换成字符串(使用的BufferedReader 的StringBuilder )并使用的setText(字符串)的TextView 显示它。它正显示在屏幕上之前甲700KB文本文件可能需要大约2至3秒。

What I'm doing right now is read the whole file into a String (using BufferedReader and StringBuilder) and display it on a TextView using setText(string). A 700KB text file can take about 2 to 3 seconds before it is being displayed on the screen.

不过,我已经使用了市场上的一些其他的电子书阅读器,他们几乎可以即时显示相同的文本。任何人都知道我是如何做到这一点?

But I've used some other ebook readers on the market and they can display the same text almost instantly. Anyone know how I can achieve this?

谢谢你。

推荐答案

从本质上讲,关键是要只加载你需要的数据,当你需要它。这样做的一个办法是把每款到它自己的TextView,它被放入ListAdapter,其中包括在一个ListView。必须有某种设置到位,使得每个段知道数据文件中找到一个索引。这个接口可以让你只是你需要加载,当你需要它。您的列表适配器看起来是这样的(这code是不完整的,但它应该给你一个想法是什么,你应该做至少):

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());
}
}

可以注意到,亚马逊使用分页为Kindle应用程序的系统。如果您有应用程序,你可以在每个你是哪个部门页面底部看到。每页是可能接近一个句子左右的长度。然后,它得到正确的网页,它可以很快完成的只是一个问题。

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.

 
精彩推荐
图片推荐