Android在隐形时更改视图内容

时间:2017-03-09 08:51:17

标签: android view

我有两个布局,里面有可更改内容的视图,我通过使它们可见和不可见来切换它们

视图内容取决于REST服务的响应,我必须在获得响应时更改指定视图的内容。

但REST服务在后台运行,不会等待可见的布局。

我将更改应用于Runnable中的相关视图,在后台线程中调用runOnUIThread

到目前为止看起来还不错,但是可以对INVISIBLE layout的子视图进行更改吗?

样品:

从服务器返回HTTP 200时:

public void success(retrofit.client.Response response, retrofit.client.Response ignore) {
                String out = new String();

                TypedInput body = response.getBody();
                try {
                    out = getString(response);
                    runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                     txtStuCounter.setText(out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

并且TimerTask根据时间在具有调用函数的视图之间切换:

private void showHideCourse(boolean show) {
    if (show) {
        if (isMultiple == false) {
            layoutCourse.setVisibility(View.VISIBLE);
            layoutMCourse.setVisibility(View.INVISIBLE);
            txtStuCounter.setVisibility(View.VISIBLE);
        } else {
            layoutMCourse.setVisibility(View.VISIBLE);
            layoutCourse.setVisibility(View.INVISIBLE);
            txtStuCounter.setVisibility(View.INVISIBLE);
        }
        if (!noSchedule) {
            //DersProgrami yururlukte
            layoutSchedule.setVisibility(View.INVISIBLE);
            isSchedule = false;
        } else {
            //Toplanti salonu videosu
            logoView.stopPlayback();
            wasVideoPlaying = false;
            logoView.setVisibility(View.INVISIBLE);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在xml布局中使用这个

 android:visibility="gone"

或在课堂内使用

object.setVisibility(View.GONE);

答案 1 :(得分:0)

我认为Loader是在backGround中加载数据的最佳解决方案 并更新UI 参见示例:

private LoaderManager.LoaderCallbacks<Cursor> favouriteLoaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new AsyncTaskLoader<Cursor>(getContext()) {


            @Override
            protected void onStartLoading() {
                forceLoad();
            }

            @Override
            public Cursor loadInBackground() {
// do your back ground work here  ...

    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor mCursor) {

       // set you visibilty Here... 
}
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
};
then intit the loaser in onCreate Methode



getLoaderManager().initLoader(FAVOURITE_LOADER_ID, null, favouriteLoaderCallbacks);

阅读更多 Loaders