首先,我确切地检查了每一个SwipeToRefreshLayout问题来解决这个问题,但没有一个对我有用。所以我请求你的帮助来解决这个问题。 我希望复习能够消失#34;一旦刷新完成"。到目前为止我所尝试的要么是它过早消失,要么根本不消失。以下是相关代码:(请注意,已定义mSwipeRefreshLayout。)
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
final double latitude = 39.9179;
final double longitude = 32.8627;
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
handler.post(refreshing);
getForecast(latitude, longitude);
}
});
getForecast(latitude, longitude);
}
private final Runnable refreshing = new Runnable(){
public void run(){
try {
if(mSwipeRefreshLayout.isRefreshing()){
handler.postDelayed(this, 500);
}else{
mSwipeRefreshLayout.setRefreshing(false);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};
和数据获取功能:
private void getForecast(double latitude, double longitude) {
String apiKey = "7d22cdb138cd70f2e9e8d2006cd0461c";
String forecastUrl = "https://api.forecast.io/forecast/" + apiKey
+ "/" + latitude + "," + longitude;
if (isNetworkAvailable()){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(forecastUrl).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
alertUserAboutError();
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
} catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
else {
Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
swipeContainer = (SwipeRefreshLayout) v.findViewById(R.id.swipeContainer);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
getForecast(latitude, longitude); //call ur function here
swipeContainer.setRefreshing(false);
}
});