Reputation: 601
I am working on a network project and dynamically update info. I have a few questions about the SwipeRefreshLayout
.
onRefresh()
starts the icon won't stop spinning and will not disappear even when all the data is updated.onRefresh()
method OR to disable it until my data is loaded?Here is my code so that everyone understand what I am talking about:
Main Thread:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_page);
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
mSwipeRefreshLayout.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
na.eraseList();
new NewsParser().execute();
}
});
mSwipeRefreshLayout.setColorScheme(R.color.red);
recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
na = new NewsAdapter();
recList.setAdapter(na);
new NewsParser().execute();
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
AsyncTask:
protected Void doInBackground(Void... params) {
try {
doc = Jsoup.connect("http://www.gamedev.net/page/index.html").get();
Element e = doc.getElementById("featured");
Elements es = e.getElementsByClass("article_content_inner");
for (Element el : es) {
Element forHeader = el.getElementsByTag("strong").first().getElementsByTag("a").first();
String URLforImg = el.getElementsByTag("img").first().attr("src");
String forDesc = el.getElementsByClass("article_desc").first().html();
forDesc = new String(forDesc.substring(forDesc.indexOf("</a>") + 7,forDesc.length()));
na.changeList(new NewCard(forHeader.html(), forDesc, URLforImg, forHeader.attr("href")));
runOnUiThread(new Runnable()
{
@Override
public void run() {
na.notifyDataSetChanged();
}
}
);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void params) {
}
}
Upvotes: 0
Views: 1517
Reputation: 2340
After the onRefresh() starts the icon won't stop spinning and will not disappear even when all the data is updated.
Use SwipeRefreshLayout.setRefreshing(false)
When I launch my app there is a white screen (while information is loading). If I try to make the refresh it will work but will load 2 copies of my data. Is there a way to force the onRefresh() method OR to disable it until my data is loaded?
How do I block all actions before the data is loaded?
SwipeRefreshLayout.setEnabled(false)
Upvotes: 2
Reputation: 978
You must not use a new thread within asynctask. Get rid of that first. Secondly, you should update your UI in your onPostExecute method, that is where you need to call na.notifyDataSetChanged();
Also you need to call swipeLayout.setRefreshing(false);
when you update/finish with the asynctask for disabling the loading/refreshing animation.
Regarding the white screen - why don't you put a loading spinner before you populate the list? Or populate the list using old data (that you have saved or preset in SharedPreferences ect.)
Upvotes: 0