Added Endless Scrolling

This commit is contained in:
Stefan Schueller 2018-03-03 02:02:00 +01:00
parent 3217cbf5d8
commit 6b61c92a7e
2 changed files with 63 additions and 32 deletions

View File

@ -9,7 +9,6 @@ import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException; import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.GooglePlayServicesUtil;
@ -20,24 +19,25 @@ import net.schueller.peertube.adapter.VideoAdapter;
import net.schueller.peertube.model.VideoList; import net.schueller.peertube.model.VideoList;
import net.schueller.peertube.network.GetVideoDataService; import net.schueller.peertube.network.GetVideoDataService;
import net.schueller.peertube.network.RetrofitInstance; import net.schueller.peertube.network.RetrofitInstance;
import net.schueller.peertube.services.RecentlyAddedVideosService;
import net.schueller.peertube.model.Video;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import static org.webrtc.ContextUtils.getApplicationContext;
public class VideoListActivity extends AppCompatActivity { public class VideoListActivity extends AppCompatActivity {
private VideoAdapter videoAdapter; private VideoAdapter videoAdapter;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private int currentStart = 0;
private int count = 12;
private String sort = "-createdAt";
private boolean isLoading = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -46,11 +46,52 @@ public class VideoListActivity extends AppCompatActivity {
// fix android trying to use SSLv3 for handshake // fix android trying to use SSLv3 for handshake
updateAndroidSecurityProvider(this); updateAndroidSecurityProvider(this);
/*Create handle for the RetrofitInstance interface*/ createList();
}
private void createList() {
recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(VideoListActivity.this);
recyclerView.setLayoutManager(layoutManager);
videoAdapter = new VideoAdapter(new ArrayList<>(), VideoListActivity.this);
recyclerView.setAdapter(videoAdapter);
loadVideos(currentStart, count, sort);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
// is at end of list?
if(!recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN)){
if (!isLoading) {
currentStart = currentStart + count;
loadVideos(currentStart, count, sort);
}
}
}
}
});
}
private void loadVideos(int start, int count, String sort) {
isLoading = true;
GetVideoDataService service = RetrofitInstance.getRetrofitInstance().create(GetVideoDataService.class); GetVideoDataService service = RetrofitInstance.getRetrofitInstance().create(GetVideoDataService.class);
/*Call the method with parameter in the interface to get the employee data*/ Call<VideoList> call = service.getVideoData(start, count, sort);
Call<VideoList> call = service.getVideoData(0, 12, "-createdAt");
/*Log the URL called*/ /*Log the URL called*/
Log.wtf("URL Called", call.request().url() + ""); Log.wtf("URL Called", call.request().url() + "");
@ -58,35 +99,19 @@ public class VideoListActivity extends AppCompatActivity {
call.enqueue(new Callback<VideoList>() { call.enqueue(new Callback<VideoList>() {
@Override @Override
public void onResponse(Call<VideoList> call, Response<VideoList> response) { public void onResponse(Call<VideoList> call, Response<VideoList> response) {
Log.wtf("Response", response + ""); videoAdapter.setData(response.body().getVideoArrayList());
generateVideoList(response.body().getVideoArrayList()); isLoading = false;
} }
@Override @Override
public void onFailure(Call<VideoList> call, Throwable t) { public void onFailure(Call<VideoList> call, Throwable t) {
Log.wtf("err", t.fillInStackTrace()); Log.wtf("err", t.fillInStackTrace());
Toast.makeText(VideoListActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show(); Toast.makeText(VideoListActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
isLoading = false;
} }
}); });
} }
/*Method to generate List of employees using RecyclerView with custom adapter*/
private void generateVideoList(ArrayList<Video> vidDataList) {
recyclerView = findViewById(R.id.recyclerView);
videoAdapter = new VideoAdapter(vidDataList, VideoListActivity.this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(VideoListActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(videoAdapter);
}
/** /**
* Force android to not use SSLv3 * Force android to not use SSLv3
* *

View File

@ -3,12 +3,10 @@ package net.schueller.peertube.adapter;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
@ -18,6 +16,8 @@ import net.schueller.peertube.model.Video;
import java.util.ArrayList; import java.util.ArrayList;
import static java.util.Collections.addAll;
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> { public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
@ -29,15 +29,16 @@ public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHol
this.context = context; this.context = context;
} }
@NonNull
@Override @Override
public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.row_video, parent, false); View view = layoutInflater.inflate(R.layout.row_video, parent, false);
return new VideoViewHolder(view); return new VideoViewHolder(view);
} }
@Override @Override
public void onBindViewHolder(VideoViewHolder holder, int position) { public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
Picasso.with(this.context) Picasso.with(this.context)
.load("https://troll.tv" + videoList.get(position).getPreviewPath()) .load("https://troll.tv" + videoList.get(position).getPreviewPath())
@ -47,6 +48,11 @@ public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHol
holder.description.setText(videoList.get(position).getDescription()); holder.description.setText(videoList.get(position).getDescription());
} }
public void setData(ArrayList<Video> data) {
videoList.addAll(data);
this.notifyDataSetChanged();
}
@Override @Override
public int getItemCount() { public int getItemCount() {
return videoList.size(); return videoList.size();