2014-06-18 2 views
0

내 응용 프로그램 셸은 장치의 파일에서 데이터를 읽습니다. 테이블을 만들지 만 즉시 표시되지 않습니다 (onCreate() 메서드에서 테이블을 만듭니다). 어쩌면 당신은 내가 웹에서 내 데이터를 스크래핑 시작할 수 있다는 힌트를 줄 수있다. 내 주요 활동에서 데이터베이스에서 내 데이터를 보여주는 첫 번째 단계를 뛰어 넘을 것이기 때문이다.Android - 내보기가 완료되어 화면에 표시되는 시점은 언제입니까?

완전히 그려진 (사용자가 UI를 볼 수있을 때) 주요 활동으로 알림을 받으려고합니다. 이 메소드가 호출 될 때 내가 장치에 내 시야를 볼 수 없습니다이기 때문에

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener) [...] 

것은 작동하지 않습니다 :

는 나는이 방법을 깨달았다. 그래서 내가이보기에 대한 자세한 내용 얻기 위해이 방법을 몇 가지 변수를 로그 :

log("isActivated: " + linearLayout.isActivated()); --> returned false 
log("isEnabled: " + linearLayout.isEnabled()); --> returned true 
log("isInTouchMode: " + linearLayout.isInTouchMode()); --> returned true 
log("isShown: " + linearLayout.isShown()); --> returned true 

내가 사실이 아니다 유일한 변수 언급을 view.isActivated()이었다. 모든 것이 그려 질 때까지 응용 프로그램을 기다리는 가장 좋은 방법이 무엇인지 말해 줄 수 있습니까? (잘하면 이상 API 11).

편집 : 당신이) (

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     List<Group> groups = null; 
     String path = getFilesDir() + separator + STORAGE_FILE; 
     File storageFile = new File(path); 

     try { 
      InternalGroupStorage storage = new InternalGroupStorage(); 
      if (storageFile.exists()) { 
       log("Load groups from storage: " + path); 
       groups = storage.loadGroupStorage(path); 
       createGroupTables(groups); 
       log("Tables created"); 
      } 
     } 
     catch (JSONException e) { 
      scrapeGroupsFromWeb(); 
      log("JSONException occured: groups going to be scraped from weg now."); 
      e.printStackTrace(); 
     } 

    } 

    private void scrapeGroupsFromWeb() { 
     try { 
      GroupScraperTask task = new GroupScraperTask(); 
      List<Group> groups = task.execute("A", "B", "C", "D", "E", "F", "G", "H").get(); 

      if (groups == null) { 
       return; 
      } 
      log("Groups were scraped."); 
      InternalGroupStorage groupStorage = new InternalGroupStorage(); 
      String path = getFilesDir() + separator + STORAGE_FILE; 
      FileOutputStream fos = new FileOutputStream(path); 
      groupStorage.saveGroupStorage(fos, groups); 
      createGroupTables(groups); 
      log("Tables created."); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      throw new RuntimeException(e); 
     } 
    } 

    private void createGroupTables(List<Group> groups) { 
     GroupTable groupTable = new GroupTable(); 

     groupTable.createGroupTable(this, groups, 0, (TableLayout) findViewById(R.id.tableA)); 
     groupTable.createGroupTable(this, groups, 1, (TableLayout) findViewById(R.id.tableB)); 
     groupTable.createGroupTable(this, groups, 2, (TableLayout) findViewById(R.id.tableC)); 
     groupTable.createGroupTable(this, groups, 3, (TableLayout) findViewById(R.id.tableD)); 
     groupTable.createGroupTable(this, groups, 4, (TableLayout) findViewById(R.id.tableE)); 
     groupTable.createGroupTable(this, groups, 5, (TableLayout) findViewById(R.id.tableF)); 
     groupTable.createGroupTable(this, groups, 6, (TableLayout) findViewById(R.id.tableG)); 
     groupTable.createGroupTable(this, groups, 7, (TableLayout) findViewById(R.id.tableH)); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     log("resume"); 
     scrapeGroupsFromWeb(); 
    } 
} 

onResume를보고 싶어하면 일부 코드; 내 테이블이 그려지기 전에 호출됩니다!

final LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1); 
     ViewTreeObserver vto = layout.getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       int width = layout.getMeasuredWidth(); 
       int height = layout.getMeasuredHeight(); 
       log("draw?"); 
       scrapeGroupsFromWeb(); 

      } 
     }); 

그것은 일반적으로이보기에 ViewTreeObserver를 추가하여 이루어집니다 내 onGlobalLayout()

+0

왜 다른 것을 시작하기 전에 사용자가 볼 수 있도록해야합니까? 뷰는 가시적인지 아닌지에 관계없이'setContentView (R.layout.activity_main);에서 반환 될 때 존재한다. 보이지 않는 경우에도 여전히 해당 뷰의 요소에 액세스하여 내용 등을 설정할 수 있습니다. – Squonk

+0

'onResume'과'onCreate'에서'scrapeGroupsFromWeb'을 호출하고 있습니다. – TyMarc

+0

가능한 복제본 [레이아웃을 그렸을 때 어떻게 알 수 있습니까?] (http://stackoverflow.com/questions/7733813/how-can-you-tell-when-a-layout-has-been-drawn) – loeschg

답변

1

scrapeGroupsFromWeb() 후 그린 :

EDIT2 내에서 onCreate()이 추가되었습니다. 이 게시물을 참조하십시오 : https://stackoverflow.com/a/7735122/413254

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUR_VIEW_ID); 
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     int width = layout.getMeasuredWidth(); 
     int height = layout.getMeasuredHeight(); 

    } 
}); 
+0

나는 이것을 추가하고 거기에 내 함수의 'crapeGroupsFromWeb()'호출합니다. 하지만 내'onGlobalLayout()'에서 mycrapeGroupsFromWeb() – unrated

관련 문제