数据加载后,我想停止刷新recyclerview,但它一直显示。我希望SwipeRefreshLayout在recyclerview的数据完全加载后消失
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_likely_problems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/zerodimen"
android:paddingLeft="@dimen/zerodimen"
android:paddingRight="@dimen/zerodimen"
android:paddingTop="@dimen/zerodimen"
android:background="@color/colorBackground"
android:orientation="vertical"
tools:context="com.example.system2.tranxav.LIkelyProblemsActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="@color/background" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/likelyProblemSwipeToRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_below="@id/toolbar"
android:id="@+id/likelyProblemRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
LikelyProblemActivity
public class LIkelyProblemsActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_problem);
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.likelyProblemSwipeToRefresh);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
if (swipeRefreshLayout !=null){
swipeRefreshLayout.setRefreshing(true);
}
loadLikelyProblems();
}
}, 3000);
public void loadLikelyProblems(){
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, LIKELY_PROBLEM_URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("info", response);
try {
JSONArray jsonArray = new JSONArray(response);
for (int i=0; i<=jsonArray.length()-1; i++){
JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
likelyProblem = likelyProblemJson.getString("problems");
LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
mlikelyProblems.add(likelyProblems);
mRecyclerView.setAdapter(likelyProblemAdapter);
}
likelyProblemAdapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
}
}
我已经浏览了代码,但是我无法弄清楚问题出在哪里。需要帮助
答案 0 :(得分:1)
在 swipeRefreshLayout.setRefreshing(false);
方法内使用 onResponse(String response)
通知窗口小部件刷新状态已更改。
示例代码
StringRequest stringRequest = new StringRequest(Request.Method.GET, LIKELY_PROBLEM_URL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("info", response);
try {
JSONArray jsonArray = new JSONArray(response);
for (int i=0; i<=jsonArray.length()-1; i++){
JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
likelyProblem = likelyProblemJson.getString("problems");
LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
mlikelyProblems.add(likelyProblems);
mRecyclerView.setAdapter(likelyProblemAdapter);
}
likelyProblemAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
swipeRefreshLayout.setRefreshing(false);
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
swipeRefreshLayout.setRefreshing(false);
}
});
int socketTimeout = 30000;
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
requestQueue.add(stringRequest);
}
编辑
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadLikelyProblems();
}
});
答案 1 :(得分:0)
从https://sync.almacgroup.com/rest/callback.html?display=mobile&code=(THEN THE CODE FOLLOWS)
块中放入swipeRefreshLayout.setRefreshing(false);
,并将其复制到try-catch
方法中。