Reputation: 2685
I want to add in my app ListView
, inside LinearLayout
that when is loading instead of list has progress bar. This is something similar like in system app, where you can manage your apps.
I found some info about it, but none was ok. The one, that seems the best was saying to create two layouts (one with progress bar and another with ListView) and then in onPreExecute()
(I am using AsyncTask
for this) show the progress bar. Then in onPostExecute()
hide it and that's all.
I was trying to implement it, but I have ScrapView error, and no more info, how it can be done.
So, my question is, how would you recommend me to do it? I don't want any progress dialog.
EDIT:
This are fragments of my code, showing what I've done so far:
TestActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist_layout);
wordList = (ListView) findViewById(R.id.wordsList);
//...
}
private class AsyncDBDownload extends AsyncTask<String, Integer, String> {
LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
@Override
protected String doInBackground(String... params) {
try {
refreshList();//upload of contetn and set of adapter
} catch (Exception ex) {
Log.e(TAG, ex.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
linlaHeaderProgress.setVisibility(View.GONE);
wordList.setVisibility(View.VISIBLE);
}
@Override
protected void onPreExecute() {
linlaHeaderProgress.setVisibility(View.VISIBLE);
}
}
private void refreshList() {
//List content download into Cursor - adapterCursor
//...
wordList.setAdapter(adapterCursor);
}
word_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pbHeaderProgress"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
</LinearLayout>
<ListView
android:id="@+id/wordsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true" >
</ListView>
Two extra things:
onResume()
there is call for AsyncTask to be execute;mylist_layout.xml
in place where should list be there is include which includes into this layout word_list.xml
layoutOther things:
I am doing app Android 2.2/2.3 compatible, I forget to mention, so if some thing isn't inside android.support.v4.app.Fragment
, than I won't use it.
Upvotes: 2
Views: 8852
Reputation: 29436
You can use ListFragment
. Use setListShown()
with true
/false
to hide/show a loader instead of a list.
Upvotes: 7