2014-03-24 53 views
1

我正在開發圖書閱讀器應用程序。我有LinearLayout。如何識別到達屏幕底部的textview?

<LinearLayout 
    android:id="@+id/llReader" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:orientation="vertical" > 
</LinearLayout> 

我正在從內部存儲逐行取html文件。我爲一行分配一個文本視圖並將它們放入LinearLayout。

private void readFile(LinearLayout ll) throws IOException { 

     fileName = myDatabase.getBookById(book_id) + ".html"; 

     FileInputStream fis = openFileInput(fileName); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 

     String line = ""; 

     while (null != (line = br.readLine())) { 

      TextView tv = new TextView(this); 
      tv.setTextSize(24); 
      tv.setText(Html.fromHtml(line)); 
      ll.addView(tv); 
     } 
     br.close(); 
    } 

如何識別TextViews到達屏幕底部?

回答

1

您可以通過像素操作:

private void readFile(LinearLayout ll) throws IOException { 

    fileName = myDatabase.getBookById(book_id) + ".html"; 

    FileInputStream fis = openFileInput(fileName); 
    BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 

    String line = ""; 

    int parentHeight = ll.getHeight(); // in pixels 
    int sumHeigth = 0; 

    while (null != (line = br.readLine())) { 

     TextView tv = new TextView(this); 
     tv.setTextSize(24); 
     tv.setText(Html.fromHtml(line)); 
     ll.addView(tv); 
     sumHeigth += tv.getHeight(); 
     if(sumHeigth>parentHeight) { 
      // if can't fit in LinearLayout 
      ll.removeView(tv); 
      // break; // or what you want in this situation 
     } 
    } 
    br.close(); 
} 
+0

THX的答案,但是當我把裏面的,而 'Log.d( 「AkitaLog」, 「sumHeight:」 + sumHeigth + 「;上級高度:」 +上級高度);'它正在返回** 03-25 20:51:29.800:D/AkitaLog(2825):sumHeight:0; parentHeight:0 ** – user3388473