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.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
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.network.GetVideoDataService;
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.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static org.webrtc.ContextUtils.getApplicationContext;
public class VideoListActivity extends AppCompatActivity {
private VideoAdapter videoAdapter;
private RecyclerView recyclerView;
private int currentStart = 0;
private int count = 12;
private String sort = "-createdAt";
private boolean isLoading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -46,11 +46,52 @@ public class VideoListActivity extends AppCompatActivity {
// fix android trying to use SSLv3 for handshake
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);
/*Call the method with parameter in the interface to get the employee data*/
Call<VideoList> call = service.getVideoData(0, 12, "-createdAt");
Call<VideoList> call = service.getVideoData(start, count, sort);
/*Log the URL called*/
Log.wtf("URL Called", call.request().url() + "");
@ -58,35 +99,19 @@ public class VideoListActivity extends AppCompatActivity {
call.enqueue(new Callback<VideoList>() {
@Override
public void onResponse(Call<VideoList> call, Response<VideoList> response) {
Log.wtf("Response", response + "");
generateVideoList(response.body().getVideoArrayList());
videoAdapter.setData(response.body().getVideoArrayList());
isLoading = false;
}
@Override
public void onFailure(Call<VideoList> call, Throwable t) {
Log.wtf("err", t.fillInStackTrace());
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
*

View File

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