Android - 我的视图何时完整显示在显示屏上?

时间:2014-06-18 18:54:23

标签: android eclipse android-activity view activity-finish

我的app shell从设备上的文件中读取数据。 我创建表但它们不会立即显示(我在onCreate()方法中创建它们)。 也许你可以给我一个提示,我可以开始从网上抓取我的数据,因为当我在我的主要活动中这样做时,它将跳过第一步,从数据库显示我的数据。

当我的主要活动被完全绘制时(当用户可以看到用户界面时),我试图通知我。

我意识到这一点:

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();在绘制表格之前调用它!

edit2在我的onCreate()中添加了这个:

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();

            }
        });

仅在scrapeGroupsFromWeb()

中的onGlobalLayout()之后绘制

2 个答案:

答案 0 :(得分:1)

通常,这是通过向视图添加ViewTreeObserver来完成的。请参阅此SO帖子: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(); 

    } 
});

答案 1 :(得分:0)

@loeschg答案对我有用,但我想补充一点细节,以帮助将来的潜在问题寻求者:

  • 其中的“ LinearLayout”

    LinearLayout layout = (LinearLayout)findViewById(R.id.YOUR_VIEW_ID);
    

我相信我可以使用FrameLayout来处理所有布局。

  • 如果您不知道如何查找ViewById,请在相应的xml文件中获取一个“ android:id = @+id/your_id_here”。
  • 除非您想在每次布局发生问题时都调用此回调,否则不要放弃removeGlobalOnLayoutListener(this);

哦和SEO:布局,完成,完成,绘制,之后,回调,侦听器