У меня проблема с FixedGridLayoutManager, я не могу знать, когда пользователь достиг последнего элемента в RecyclerView
Вот мой код
clubAdapter = new ClubAdapter(CaptainDetails.this, captainList, CaptainDetails.this);
FixedGridLayoutManager manager = new FixedGridLayoutManager();
manager.setTotalColumnCount(1);
rvClub.setLayoutManager(manager);
rvClub.setAdapter(clubAdapter);
rvClub.addItemDecoration(new DividerItemDecoration(CaptainDetails.this, DividerItemDecoration.VERTICAL));
rvClub.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrollX += dx;
headerScroll.scrollTo(scrollX, 0);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
Мой класс адаптера:
public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubViewHolder> implements Filterable {
private static final int TYPE_ROW = 0;
private static final int TYPE_ROW_COLORFUL = 1;
private List<Club> clubList;
private List<Club> filteredClubList;
private Context context;
private Activity activity;
public ClubAdapter(Context context, List<Club> clubList, Activity activity) {
this.context = context;
this.clubList = clubList;
this.filteredClubList = clubList;
this.activity = activity;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return TYPE_ROW_COLORFUL;
}
return TYPE_ROW;
}
@Override
public ClubViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType == TYPE_ROW) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_club, viewGroup, false);
return new ClubViewHolder(view);
} else {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_club_colorful,
viewGroup, false);
return new ClubViewHolder(view);
}
}
@Override
public void onBindViewHolder(ClubViewHolder holder, int position) {
Club club = filteredClubList.get(position);
holder.captainNametv.setText(club.captainName);
holder.captainMobileNumbertv.setText(club.getCaptainMobileNumber());
holder.ridesCounttv.setText(String.valueOf(club.getRidesCount()));
}
@Override
public int getItemCount() {
return filteredClubList.size();
}
public class ClubViewHolder extends RecyclerView.ViewHolder {
TextView captainNametv, captainMobileNumbertv, ridesCounttv;
ImageView captainProfilePicFlag, captainDLFlag, captainRCFlag, captainActiveFlag;
LinearLayout captainDetailsMainLL;
public ClubViewHolder(View view) {
super(view);
captainNametv = view.findViewById(R.id.captainNametv);
captainMobileNumbertv = view.findViewById(R.id.captainMobileNumbertv);
ridesCounttv = view.findViewById(R.id.ridesCounttv);
//captainMobileFlag = view.findViewById(R.id.captainMobileFlag);
captainProfilePicFlag = view.findViewById(R.id.captainProfilePicFlag);
captainDLFlag = view.findViewById(R.id.captainDLFlag);
captainRCFlag = view.findViewById(R.id.captainRCFlag);
captainActiveFlag = view.findViewById(R.id.captainActiveFlag);
captainDetailsMainLL = view.findViewById(R.id.captainDetailsMainLL);
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
filteredClubList = clubList;
} else {
List<Club> filteredList = new ArrayList<>();
for (Club club : clubList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name
if (club.captainMobileNumber.contains(charString.toLowerCase())) {
filteredList.add(club);
}
}
filteredClubList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredClubList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredClubList = (ArrayList<Club>) filterResults.values;
// refresh the list with filtered data
notifyDataSetChanged();
}
};
}
}