如何将ScrollView滚动到特定百分比的文本内容?

时间:2011-12-07 08:11:30

标签: android textview android-edittext scrollview

我正在编写一个操作文本的程序。 我需要能够以编程方式将文本滚动到某个位置。 我已经部分实现了它,但问题在于它并不总是有效,

有时不起作用的情况如下:

  1. 从活动(1)显示文本用户打开另一个活动(2),用于编辑一些概述的文本片段(1)
  2. 当用户关闭活动时(2)必须刷新活动(1)并滚动到之前的位置。
  3. 以下是我需要滚动的视图布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    
         <ScrollView   
          android:id="@+id/ScrollView02"  
          android:layout_height="fill_parent"   
          android:layout_width="fill_parent">
          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layoutAllText"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
                <TextView
                android:id="@+id/textSanskrit"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:textSize="17dip"
                 android:gravity="center_vertical|center_horizontal"
                 android:scrollbars="vertical"
                  />    
                <TextView
                android:id="@+id/textTranslit"
                 android:gravity="center_vertical|center_horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:textSize="15dip"
                 android:scrollbars="vertical"
                  />            
                <TextView
                android:id="@+id/textTransl1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:textSize="15dip"
                 android:scrollbars="vertical"
                />
    
            <EditText
                android:id="@+id/textTransl2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:textSize="17dip"
                android:paddingLeft="10px"
                android:paddingRight="6px"
                android:textColor="@color/green"
                 android:scrollbars="vertical"
                 android:editable = "false"
                 android:background = "#000000"
    
                />      
    
                <EditText
                android:id="@+id/textComment"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:textSize="17dip"
                android:paddingLeft="10px"
                android:paddingRight="6px"
                 android:scrollbars="vertical"
                 android:editable = "false"
                 android:textColor = "@android:color/white"
                 android:background = "#000000"
    
                />      
                </LinearLayout>
          </ScrollView>
    </LinearLayout>
    

    这是活动的摘录,显示了要滚动的文本:

        public void onCreate(Bundle savedInstanceState) {
    
    
            super.onCreate(savedInstanceState);
            Central.applySharedTheme(this);
    
            mExtras = getIntent().getExtras();
            mCurrBookID = mExtras.getString("BookID");
            mCurrChapterNum = mExtras.getString("ChapterNum");
            mCurrTextNum = mExtras.getString("TextNum");
            mTranslateMode = mExtras.getBoolean("TranslateMode");
    
            setContentView(R.layout.text_disp_basic);
    
            detector = new SimpleGestureFilter(this,this);
            mTextScroll=(ScrollView) findViewById(R.id.ScrollView02);
    
            mGitaDB=Central.mDB;
    
            String q="("+mGitaDB.COL_TXT_BOOK_ID+"='"+mCurrBookID+"' AND "+mGitaDB.COL_TXT_CHAPTER_NUM+"='"+mCurrChapterNum+"' AND "+mGitaDB.COL_TXT_TEXT_NUM+"='"+mCurrTextNum+"')";
            mCursor = mGitaDB.getdb().query(mGitaDB.TEXTS_TABLE, null, q, null, null, null, null);
    
            Typeface face=Typeface.createFromAsset(getAssets(), "Sanskrit2003.ttf");
    
            mtxtSanskritView=(TextView) findViewById(R.id.textSanskrit);
            mtxtTranslitView =(TextView) findViewById(R.id.textTranslit);
            mtxtTransl1View =(TextView) findViewById(R.id.textTransl1);
            mtxtTransl2View =(TextView) findViewById(R.id.textTransl2);
            mtxtComment=(TextView) findViewById(R.id.textComment);
    
            mtxtSanskritView.setTypeface(face);
    
    
        }
    
        public void onStart()   {
            super.onStart();
    
            if (mCursor.moveToFirst()) FillTextData();
            ShowSanskrit();
            getScrollPos(); 
            Log.v("SCROLL", "onStart - mScrollPercentage="+String.valueOf(mScrollPercentage));
            ScrollTextToCurrentPercentage("OnStart");
    
        }
        public void onPause()   {
            super.onPause();
            mbFirstCall=true;
            float scroll = getScrollPos(mTextScroll);
            Log.v("SCROLL", "onPause - getScrollPos="+String.valueOf(scroll));
    
            SharedPreferences LastOpenedScreen = getSharedPreferences(Central.SHARED_PREFS_LASTSCREEN, MODE_PRIVATE);   
            SharedPreferences.Editor prefEditor = LastOpenedScreen.edit();   
            prefEditor.putString(Central.SP_LASTSCREEN_BOOKID, mCurrBookID);
    
            if(!mTranslateMode){
                prefEditor.putString(Central.SP_LASTSCREEN_BCHAPTER+mCurrBookID, mCurrChapterNum);
                prefEditor.putString(Central.SP_LASTSCREEN_BTEXT+mCurrBookID, mCurrTextNum);
                prefEditor.putFloat(Central.SP_LASTSCREEN_SCROLL, scroll);
                Log.v("SCROLL", "onPause - saved scroll="+String.valueOf(scroll));
            }
            else        
            {
                prefEditor.putFloat("TranslateScrollPercentage", scroll);
                Log.v("SCROLL", "onPause - Before Translate scroll="+String.valueOf(scroll));
            }
    
            prefEditor.commit();  
        }
    
       public void onResume()
       {     
            super.onResume(); 
    
            FillTextData();
            getScrollPos();
            Log.v("SCROLL", "onResume - mScrollPercentage="+String.valueOf(mScrollPercentage));
            ScrollTextToCurrentPercentage("onResume");
       }
        private void getScrollPos()
        {
            String sLastBookID, sLastChapter, sLastText;
    
            SharedPreferences LastOpenedScreen = getSharedPreferences(Central.SHARED_PREFS_LASTSCREEN, MODE_PRIVATE);
            sLastBookID=LastOpenedScreen.getString(Central.SP_LASTSCREEN_BOOKID, "");
            mScrollPercentage=this.ScrollToFloat(mExtras.getLong("Scroll", 0));
            if (sLastBookID.equalsIgnoreCase(this.mCurrBookID))
            {
                //last chapter and text of this book
                sLastChapter=LastOpenedScreen.getString(Central.SP_LASTSCREEN_BCHAPTER+mCurrBookID, "");
                sLastText=LastOpenedScreen.getString(Central.SP_LASTSCREEN_BTEXT+mCurrBookID, "");
    
                //if this chapter and text are the same as saved last text and chapter, then set scroll
                if (sLastChapter.equals(this.mCurrChapterNum) && sLastText.equals(this.mCurrTextNum) )
                    mScrollPercentage=LastOpenedScreen.getFloat(Central.SP_LASTSCREEN_SCROLL, 0);
            }
            //if this activity is called after translate button pressed
            if (mTranslateMode)
            {
                mScrollPercentage=LastOpenedScreen.getFloat("TranslateScrollPercentage", 0);
                Log.v("SCROLL", "getScrollPos - mTranslateMode - mScrollPercentage="+String.valueOf(mScrollPercentage));
            }
    
            Log.v("SCROLL", "getScrollPos - mScrollPercentage="+String.valueOf(mScrollPercentage));
        }
        private void ScrollTextToCurrentPercentage(String sCallOrigin)
        {
            //scroll to mScrollPercentage position
            mbFirstCall=true;
            mTextScroll=(ScrollView) findViewById(R.id.ScrollView02);
            int iHeight=mTextScroll.getChildAt(0).getHeight();
            Log.v("SCROLL", "ScrollTextToCurrentPercentage call. height=:"+String.valueOf(iHeight));
            if (iHeight!=0)
            {
                int y=(int)(iHeight*mScrollPercentage);
                mTextScroll.scrollBy(0, y);
                Log.v("SCROLL", "ScrollText scrolled to:"+String.valueOf(y));
            }
            else
            {
                ViewTreeObserver vto = mTextScroll.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                    public void onGlobalLayout() {
                        int y=(int)(mTextScroll.getChildAt(0).getHeight()*mScrollPercentage);
                        if (mbFirstCall){
                            mTextScroll.scrollBy(0, y);
                            mbFirstCall=false;
                            Log.v("SCROLL", "ScrollTextToCurrentPercentage scrolled to:"+String.valueOf(y));
    
                            }
                        }
                });         
            }
    
        }
    

    http://www.ljplus.ru/img4/n/e/nepalec/screenshot.jpg

    在此屏幕截图中,您可以看到程序滚动到正确的保存位置,但是,屏幕上的实际位置是不同的。 Toast显示实际滚动为8%,而保存的滚动为26%。 LogCat中的最后一行显示mTextScroll.scrollBy(0,y); ScrollTextToCurrentPercentage中的行已执行。

    我被困在上面,你能帮帮我吗,也许是因为缺乏知识和经验,我做错事。

0 个答案:

没有答案