如何在刷新refreshLayout时刷新RecyclerView并使用新的Json填充它

时间:2016-01-20 12:44:10

标签: android json android-recyclerview pull-to-refresh

我在ArchiveActivity中有一个RecyclerView,它用json填充,每件事都没问题但是当我向下滑动刷新版面时,没有任何反应,现在我的问题是如何检索新的{{1}并填充json并且与RecyclerView

没有冲突

这是setOnScrollListenerMethod

activity_archive.xml

这是<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e6e6e6" tools:context=".activities.ArchiveActivity"> <android.support.v7.widget.Toolbar android:id="@+id/archiveToolbar" android:background="#34465d" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize"/> <Button android:id="@+id/btnVideo" android:layout_below="@+id/archiveToolbar" android:text="Video Activity" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout_archive" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/archiveRecylerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/btnVideo"/> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>

ArchiveActivity

这是import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Color; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.Button; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.majid.aparat.R; import com.majid.aparat.adapter.ArchiveRecyclerAdapter; import com.majid.aparat.app.AppController; import com.majid.aparat.pojo.VideoData; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import android.os.Handler; import android.widget.Toast; public class ArchiveActivity extends AppCompatActivity { // ============ Global Variables ================ // ProgressDialog progressDialog; List<VideoData> videoDataList = new ArrayList<VideoData>(); RecyclerView archiveRecyclerView; ArchiveRecyclerAdapter adapter; private String after_url; private SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_archive); String TAG = ArchiveActivity.class.getSimpleName(); setUpToolbar(); // ========== btn Video Activity =========== // Button btnVideo = (Button) findViewById(R.id.btnVideo); btnVideo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(ArchiveActivity.this, WatchActivity.class)); } }); //======== SwipeRefreshLayout init ======// swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh_layout_archive); swipeRefreshLayout.setColorSchemeResources(R.color.red , R.color.green , R.color.blue); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { sendJsonRequest(); swipeRefreshLayout.setRefreshing(false); } } , 1500 ); } }); // ========================= sending jsonRequest ======================= // sendJsonRequest(); showPD(); //========================= initialize recyclerView ===========================// archiveRecyclerView = (RecyclerView) findViewById(R.id.archiveRecylerView); archiveRecyclerView.setHasFixedSize(true); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ArchiveActivity.this); archiveRecyclerView.setLayoutManager(linearLayoutManager); archiveRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) { @Override public void onLoadMore(int current_page) { loadMore(); Toast.makeText(ArchiveActivity.this , "onLoadMore is calling" , Toast.LENGTH_SHORT).show(); } }); adapter = new ArchiveRecyclerAdapter(this, videoDataList); archiveRecyclerView.setAdapter(adapter); } //========================= send json request ===========================// private void sendJsonRequest() { //========================= get Category intent ===========================// Intent intent = getIntent(); String catUrl = intent.getStringExtra("category"); String fUrl = String.format("http://www.aparat.com//etc/api/categoryvideos/%s", catUrl); JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, fUrl, (String) null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { videoDataList.clear(); parsJsonResponse(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("TAG", error.toString()); } }); AppController.getInstance().addToRequestQueue(request); } //========================= parse json response ===========================// private void parsJsonResponse(JSONObject response) { if (response != null) { try { JSONObject ui = response.getJSONObject("ui"); after_url = ui.getString("pagingForward"); JSONArray categoryViedeos = response.getJSONArray("lastvideos"); for (int i = 0; i < categoryViedeos.length(); i++) { JSONObject currentVideo = categoryViedeos.getJSONObject(i); VideoData viNew = new VideoData(); viNew.setVideoThumbnail(currentVideo.getString("small_poster")); viNew.setVideoTitle(currentVideo.getString("title")); viNew.setVideoCount(currentVideo.getString("visit_cnt")); viNew.setVideoDate(currentVideo.getString("sdate")); videoDataList.add(viNew); } adapter.addMoreItems(videoDataList); hidePD(); } catch (JSONException e) { e.printStackTrace(); } adapter.notifyDataSetChanged(); } } //========================= LoadMore for infinitie scroll ===========================// private void loadMore() { //Toast.makeText(ArchiveActivity.this , "we are loading !!!" ,Toast.LENGTH_SHORT).show(); showPD(); JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, after_url, (String)null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { hidePD(); parsJsonResponse(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //Error } }); AppController.getInstance().addToRequestQueue(request); } //========================= setUpToolbar ===========================// private void setUpToolbar() { Intent intent = getIntent(); String catName = intent.getStringExtra("catName"); Toolbar archiveToolbar = (Toolbar) findViewById(R.id.archiveToolbar); //Toast.makeText(this , catUrl , Toast.LENGTH_SHORT).show(); setSupportActionBar(archiveToolbar); archiveToolbar.setTitleTextColor(Color.WHITE); getSupportActionBar().setTitle(catName); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_left); } //========================= showPD Method ===========================// private void showPD() { if (progressDialog == null) { progressDialog = new ProgressDialog(this); progressDialog.setMessage("در حال بارگزاری "); progressDialog.setCancelable(false); progressDialog.setCanceledOnTouchOutside(false); progressDialog.show(); } } //========================= hidePD Method ===========================// private void hidePD() { if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } } }

ArchiveRecyclerAdapter

1 个答案:

答案 0 :(得分:0)

只需调用adapter.notifyDataSetChanged();,就不会在当前的适配器数据源中添加新项目。

要使其正常工作,请在ArchiveRecyclerAdapter中创建一个方法,以便在数据源中添加新项目,如:

public void addMoreItems(List<VideoData> videoDataList){
   this.videoDataList.addAll(videoDataList);
}

现在在调用addMoreItems

之前调用parsJsonResponse中的adapter.notifyDataSetChanged();方法