سلام بچه ها من یک برنامه تمرینی با api آپارات نوشتم . هم چیز ردیفه به جز یک چیز که نمیدونم چجوری انجامش بدم و اون اینه که نمیدونم چجوری با کشیدن swipeRefreshLayout . کاری کنم که ویدئو های جدید و چیز های قبلی (مثل تعداد بازدید و ...) آپدیت بشن و به recyclerView اضافه بشن و. این اکتیویتی هستش که من با یک recyclerView ویدئوها را نشون میدم و json را پارس میکنم .
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() {
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
} , 2500 );
}
});
// ========================= 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.addOnScrollListener(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);
videoDataList.clear();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, fUrl, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
parsJsonResponse(response);
adapter.notifyDataSetChanged();
}
}, 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("categoryvideos");
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);
}
hidePD();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//========================= 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) {
Log.d("app", after_url);
hidePD();
parsJsonResponse(response);
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Error
}
});
AppController.getInstance().addToRequestQueue(request);
}