0

我正在尝试滚动到 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 中完成

4

3 回答 3

0

在这种情况下,实际上不需要使用 asynctask 进行滚动。您可以简单地在主线程中运行代码。它会正常工作。
因为如果用户在通过 asyntask 滚动时单击滚动视图。会有意想不到的行为。
并且还指定
1.android:hardwareAccelerated="true"在您的滚动视图 2 的活动清单中
。除了上述步骤 -scrollView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
这将使用硬件加速到您的滚动视图以实现平滑滚动。

于 2013-10-20T08:01:22.567 回答
0

将此代码粘贴到您的布局中:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    >

           <!-- "your layout" -->

</ScrollView>
于 2013-10-20T07:14:51.093 回答
0

从您的代码中,您尝试从doInBackground.

       @Override
        protected String doInBackground(String... params) {
               ....
            GoToFunction(chapter_number_bookmark,verse_number_bookmark);
            .....
        }

你不能那样做。我确定您在 logcat 中会遇到异常。如果您仍然想更新,请通过 Handler

可以说我们有

private TextView m_txtLog;    
private ScrollView m_sv;

所以滚动到最后一行应该是这样的:

m_sv.post(new Runnable() { 
  public void run() { 
     m_sv.scrollTo(0, m_txtLog.getHeight());                
     } 
  }); 

如果您从以下位置运行它ServiceAsyncTask使用它Handler

static final int LOG_MSG = 1;

private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String txt;
            switch (msg.what) {
            case LOG_MSG:
                txt = msg.obj.toString();
                m_txtLog.append(txt);

                m_sv.post(new Runnable() { 
                    public void run() { 
                        m_sv.scrollTo(0, m_txtLog.getHeight());                 
                    } 
                });     
                break;
            default:
                super.handleMessage(msg);
            }
        }
    };

现在我们可以写出AsyncTask

mHandler.sendMessage(mHandler.obtainMessage(LOG_MSG, "Agent started" + "\n\n")); 

如您所见scrollTo,由处理程序包裹。通过这种方式,它将起作用。

(经测试)

于 2013-10-20T07:16:12.800 回答