我想在片段中创建一个空白加载屏幕。代码完成了一半,但问题是首先加载布局,然后加载检索到的数据。
示范:https://www.youtube.com/watch?v=eGR8mkNLFqI
我希望在加载时有一个空白屏幕,然后立即显示所有内容。那么如何在onPostExecute
下扩充布局呢?或者我应该如何进行加载屏幕?
感谢。
public class FragmentSrvInfo extends Fragment {
public View rootView;
SampQuery query = AddServerActivity.query;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_srv_info2, container, false);
new getServerInfo().execute();
return rootView;
}
public class getServerInfo extends AsyncTask < String, Integer, Boolean > {
String[] serverInfo = null;
@Override
protected Boolean doInBackground(String...params) {
if (query.connect()) {
serverInfo = query.getInfo();
return true;
}
return false;
}
@Override
protected void onPostExecute(Boolean strings) {
if (serverInfo != null) {
TextView hostname = (TextView) rootView.findViewById(R.id.id_hostname);
TextView players = (TextView) rootView.findViewById(R.id.id_players);
TextView gamemode = (TextView) rootView.findViewById(R.id.id_mode);
TextView lang = (TextView) rootView.findViewById(R.id.id_lang);
hostname.setText(serverInfo[3]);
players.setText(serverInfo[1] + "/" + serverInfo[2]);
gamemode.setText(serverInfo[4]);
lang.setText(serverInfo[5]);
}
}
}
}
答案 0 :(得分:1)
将您的布局换行FrameLayout
并添加空白&#34;加载屏幕&#34;在它的顶部(这意味着你的framelayout中的最后一个条目),宽度和高度为match_parent。
<FrameLayout>
<BLA>Layout for your data you load in Background</BLA>
<LinearLayout android:width="match_parent"
android:height="match_parent"
android:id="@+id/loading"/>
</FrameLayout>
(这个布局XML只是为了说明这个想法,你当然必须写它以使它符合你的布局)
然后你只需要一个片段来处理它 - 你可以简单地将加载屏幕的可见性设置为View.GONE
public class getServerInfo extends AsyncTask<String, Integer, Boolean> {
String[] serverInfo = null;
@Override
protected Boolean doInBackground(String... params) {
if (query.connect()) {
serverInfo = query.getInfo();
return true;
}
return false;
}
@Override
protected void onPostExecute(Boolean strings) {
rootView.findViewById(R.id.loading).setVisibility(View.GONE);
if(serverInfo != null) {
// your code to show data
}
}
}
如果您多次加载数据,您还可以在View.VISIBLE
onPreExecute();