如何从其他线程修改片段视图?

时间:2017-09-20 14:57:17

标签: android android-fragments android-activity okhttp

我有一个片段和okhttpp客户端。 当我在片段内发出请求时,okhttp得到200响应ID就像在片段中设置mProgressBar.setVisibility(View.GONE);一样。 但遗憾的是没有任何反应。 我得到了一个响应,它的确定,代码转到mProgressBar.setVisibility(View.GONE);的行,但没有任何反应,mProgressBar仍在显示,但是日志显示mProgressBar not visible。 为什么这样?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_shemas, container, false);
    ls = (Ls) getActivity().getApplicationContext();
    mProgressBar = rootView.findViewById(R.id.pb_fr_schema);

    mClient = ls.createClient(getActivity());
    Log.d(TAG, "Назначили OkHttpClient " + mClient.toString() + " для " + this.getClass().toString() + " в " + this.getClass().toString());

    try
    {
        String url = ls.baseUrl + ls.getLsId() + "/" + ls.getCurrBidId() + "/call-fwd-settings/overview";
        requestOkHttp(url, "GET", "");
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    mSpinner = (Spinner) rootView.findViewById(R.id.sp_fr_schemas_did);
    // Список номеров
    ArrayList<String> didsNames = new ArrayList<>();
    for (Map.Entry<String, Bid> pr : ls.getProducts().entrySet())
    {
        didsNames.add(pr.getValue().getBidName());
    }

    mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_row_products, R.id.tv_bid_name, didsNames);
    // Чтобы был читаемый дизайн при выборе DID
    mAdapter.setDropDownViewResource(R.layout.spinner_row_products);
    //spinner.setAdapter(mAdapter);
    return inflater.inflate(R.layout.fragment_shemas, container, false);
}

这是okhttp响应代码

getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
// Отключаем mProgressBar
mProgressBar.setVisibility(View.GONE);
if (mProgressBar.getVisibility() == View.VISIBLE) {
Log.d(TAG, " mProgressBar visible");
} else {
Log.d(TAG, " mProgressBar not visible");
}
}
});

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 问题发生在return。 我将此return inflater.inflate(R.layout.fragment_shemas, container, false);更改为此return rootView;,现在已确定。