endless RecyclerView Adapter

File name: EndlessRecyclerViewScrollListener
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 2;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;

RecyclerView.LayoutManager mLayoutManager;

public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
}

public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}

public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}

public int getLastVisibleItem(int[] lastVisibleItemPositions) {
int maxSize = 0;
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i];
}
else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i];
}
}
return maxSize;
}

// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
int lastVisibleItemPosition = 0;
int totalItemCount = mLayoutManager.getItemCount();

if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
// get maximum element within the list
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
} else if (mLayoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
} else if (mLayoutManager instanceof LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
}

// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) {
this.loading = true;
}
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}

// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount, view);
loading = true;
}
}

// Call this method whenever performing new searches
public void resetState() {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = 0;
this.loading = true;
}

// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);

}



create xml:layout_progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">

<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>


Custom Adapter


import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;


@Keep
public class VideoAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
public Context context;
private ArrayList<Cat_Object> arrayList;
Click click;
Myclick myclick;
Myclick myclick2;
Myclick myclick3;

public VideoAdapter(Context context, ArrayList<Cat_Object> arrayList, Click click, Myclick myclick, Myclick myclick2, Myclick myclick3) {
this.context = context;
this.arrayList = arrayList;
this.click = click;
this.myclick = myclick;
this.myclick2 = myclick2;
this.myclick3 = myclick3;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cat_item_file, parent, false);
return new MyViewHolder(itemView);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_progressbar, parent, false);
return new ProgressViewHolder(v);
}
}

@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof MyViewHolder) {

Picasso.get().load(arrayList.get(position).getUri()).into(((MyViewHolder) holder).avtar);
((MyViewHolder) holder).title.setText(arrayList.get(position).getTitel());
((MyViewHolder) holder).avtar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click.getId(arrayList.get(position).getVideo_link());
}
});
((MyViewHolder) holder).dw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myclick.getdw(arrayList.get(position).getVideo_link(),arrayList.get(position).getTitel());
}
});
((MyViewHolder) holder).wp.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
myclick.getdw3(arrayList.get(position).getVideo_link(),arrayList.get(position).getTitel());
}
});
((MyViewHolder) holder).sh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myclick.getdw2(arrayList.get(position).getVideo_link(),arrayList.get(position).getTitel());

}
});


} else {
if (getItemCount() == 1) {
ProgressViewHolder.progressBar.setVisibility(View.GONE);
}
}
}

@Override
public long getItemId(int id) {
return id;
}

@Override
public int getItemCount() {
return arrayList.size() + 1;
}

public void hideHeader() {
ProgressViewHolder.progressBar.setVisibility(View.GONE);
}

public boolean isHeader(int position) {
return position == arrayList.size();
}

@Override
public int getItemViewType(int position) {
return isHeader(position) ? VIEW_PROG : VIEW_ITEM;
}

private static class ProgressViewHolder extends RecyclerView.ViewHolder {
private static ProgressBar progressBar;

private ProgressViewHolder(View v) {
super(v);
progressBar = v.findViewById(R.id.pb);
}
}

class MyViewHolder extends RecyclerView.ViewHolder {
ImageView avtar;
ImageView play;
TextView title;
ImageView dw,sh,wp;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
avtar = itemView.findViewById(R.id.avtar2);
title = itemView.findViewById(R.id.titel2);
play = itemView.findViewById(R.id.play);
dw=itemView.findViewById(R.id.dw);
sh=itemView.findViewById(R.id.sh);
wp=itemView.findViewById(R.id.wp);
}
}

public interface Click {

void getId(String id);
}
public interface Myclick{
void getdw(String link, String tital);

void getdw3(String video_link, String titel);

void getdw2(String video_link, String titel);
}


}


call Adapter:
cat_adapter = new VideoAdapter(getActivity(), recentlist, (VideoAdapter.Click) this,this,this,this);
rv.setAdapter(cat_adapter);


Comments

Popular posts from this blog

The Movie Downloader