我正在尝试滚动到 1000 行字符串中的子字符串。我注意到滚动和 UI 线程存在滞后。所以我想使用 AsyncTask 但在执行时我得到了文本但没有滚动。这是我的代码
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
SharedPreferences score = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int chapter_number_bookmark = score.getInt("chapter_number", 89);
int verse_number_bookmark = score.getInt("verse_number", 1);
GoToFunction(chapter_number_bookmark,verse_number_bookmark);
return "Executed";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "Executed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {}
}
转到函数
public void GoToFunction(int chapter, int verse)
{
int scroll_amt;
final TextView shw = (TextView) findViewById(R.id.textViewTab);
SharedPreferences score = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int chapter_number = chapter;
int verse_number = verse;
String verse_number_str = Integer.toString(verse_number);
SQLiteDatabase as = openOrCreateDatabase("/"+Environment.getExternalStorageDirectory().getPath()+"/tamil/verse", MODE_PRIVATE, null);
Cursor a = as.rawQuery("select * from verse"+chapter_number, null);
a.moveToFirst();
strTitle = a.getString(a.getColumnIndex("title"));
final String strContent_book = a.getString(a.getColumnIndex("content"));
int number = a.getInt(a.getColumnIndex("dialogues"));
tab.setText("\n\n "+strAthi+strTitle+"\n\n "+strVasa+number+"\n\n "+strContent_book);
final int offset_dot = strContent_book.indexOf(verse_number_str_dot);
final int offset_comma = strContent_book.indexOf(verse_number_str_comma);
a.close();
as.close();
if(offset_comma!=-1||offset_dot!=-1)
{
if(offset_comma==-1||(offset_dot<offset_comma))
{
try
{
Toast.makeText(getApplicationContext(), "Athiyayam : "+ chapter_number +" Verse : "+ verse_number, Toast.LENGTH_LONG).show();
**scroll(offset_dot); //Scroll function**
}
catch(Exception e)
{
Log.e("Scroll_comma", "Exception", e);
}
}
else
{
try
{
Toast.makeText(getApplicationContext(), "Athiyayam : "+ chapter_number +" Verse :"+ verse_number, Toast.LENGTH_LONG).show();
scroll(offset_comma);
}
catch(Exception e)
{
Log.e("Scroll", "Exception", e);
}
}
}
else
{
Toast.makeText(getApplicationContext(), "Not found", Toast.LENGTH_LONG).show();
}
}
滚动功能
public void scroll(final int a)
{
final TextView shw = (TextView) findViewById(R.id.textViewTab);
try
{
mScroll.post(new Runnable() {
@Override
public void run() {
int y = shw.getLayout().getLineForOffset(a); // e.g. I want to scroll to line 40
int n = shw.getLayout().getLineTop(y);
mScroll.scrollTo(0, n);
}
});
}
catch(Exception e)
{
Log.e("scroll", "error", e);
}
}
在这里,我得到了文字。但是滚动功能没有被执行。我还想在书签活动开始和结束时进行微调活动,这可以在 PreExecute 和 PostExecute 中完成