SwipeRefreshLayout 配置
SwipeRefreshLayout configuration
我正在做一个网络项目并动态更新信息。我有几个关于 SwipeRefreshLayout
.
的问题
onRefresh()
启动后图标不会停止旋转,即使更新所有数据也不会消失。
- 当我启动我的应用程序时出现白屏(正在加载信息)。如果我尝试进行刷新,它会起作用,但会加载我的数据的 2 个副本。有没有办法强制
onRefresh()
方法或禁用它直到我的数据加载?
- 如何在加载数据之前阻止所有操作?
这是我的代码,以便大家理解我在说什么:
主线程:
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) {
}
}
您不得在 asynctask 中使用新线程。先摆脱那个。其次,您应该在您的 onPostExecute 方法中更新您的 UI,这是您需要调用 na.notifyDataSetChanged();
的地方您还需要调用 swipeLayout.setRefreshing(false);
当您 update/finish 禁用异步任务时loading/refreshing 动画。
关于白屏 - 为什么不在填充列表之前放置一个加载微调器?或者使用旧数据(您在 SharedPreferences 等中保存或预设的数据)填充列表
After the onRefresh() starts the icon won't stop spinning and will not disappear even when all the data is updated.
使用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)
我正在做一个网络项目并动态更新信息。我有几个关于 SwipeRefreshLayout
.
onRefresh()
启动后图标不会停止旋转,即使更新所有数据也不会消失。- 当我启动我的应用程序时出现白屏(正在加载信息)。如果我尝试进行刷新,它会起作用,但会加载我的数据的 2 个副本。有没有办法强制
onRefresh()
方法或禁用它直到我的数据加载? - 如何在加载数据之前阻止所有操作?
这是我的代码,以便大家理解我在说什么:
主线程:
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) {
}
}
您不得在 asynctask 中使用新线程。先摆脱那个。其次,您应该在您的 onPostExecute 方法中更新您的 UI,这是您需要调用 na.notifyDataSetChanged();
的地方您还需要调用 swipeLayout.setRefreshing(false);
当您 update/finish 禁用异步任务时loading/refreshing 动画。
关于白屏 - 为什么不在填充列表之前放置一个加载微调器?或者使用旧数据(您在 SharedPreferences 等中保存或预设的数据)填充列表
After the onRefresh() starts the icon won't stop spinning and will not disappear even when all the data is updated.
使用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)