The last item in the recyclerView list - java

How can I display a message to the user after scrolling the recyclerview items and viewing the last item in the list?
The code below does not work
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val count= recyclerView.layoutManager?.itemCount
if (customerModels.size==count){
Toast.makeText(applicationContext,"true",Toast.LENGTH_LONG).show()
}
}
})

This is what I usually use when I want to check if the user is reached to the bottom of the recyclerView
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
LinearLayoutManager lm=LinearLayoutManager.class.cast(recyclerView.getLayoutManager());
int items= lm.getItemCount();
int last = lm.findLastVisibleItemPosition();
boolean bottom= last+ 5 >= items;
if (items> 0 && bottom) {
//you have reached to the bottom of your recycler view
}
}
});

Related

Notify the recyclerview item when it is onMoving without interruption

I've expandable row layouts in RecyclerView, normally they're expanded.
Atteched ItemTouchHelper.SimpleCallback to the RecyclerView for moving the rows with onMove(). Everythings is right and moving the rows correctly.
ItemTouchHelper.SimpleCallback:
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
Collections.swap(adapter.getShelves(), fromPosition, toPosition);
adapter.notifyItemMoved(fromPosition, toPosition);
return true;
}
// Minifying the rows before the onMove is started:
#Override
public boolean isLongPressDragEnabled() {
adapter.minifyRow(position);
return true;
}
It should minify the rows when long pressing, before onMoving() is started. Then should start the moving without any interrupting. The problem is starting dragging and minifiying the row when long press but interrupting the dragging at the moment. So it doesn't dragging or moving the row. How to simulate onMove with notify?
In adapter class:
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
boolean rowIsExpanded = rows.get(position).isExpanded();
if(rowIsExpanded){
holder.rowLayout.setVisibility(View.VISIBLE);
} else {
holder.rowLayout.setVisibility(View.GONE);
}
}
public void minifyRow(boolean situation, int adapterPos){
RowModel row = rows.get(adapterPos);
row.setExpanded(situation);
notifyItemChanged(adapterPos);
}
You shouldn't call adapter.minifyRow(position); inside isLongPressDragEnabled it only flags to check your adapter enable long-press or not. You should override onSelectedChanged. Remove method in isLongPressDragEnabled and Try this
#Override
public void onSelectedChanged(#Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
((YourViewHolder)viewHolder).rowLayout.setVisibility(View.GONE);
}
}

How to reset the background buttons (swipe menu) of the recyclerview programmatically?

I have a recyclerview which has a swipe menu (background buttons) on my android app.
The article that I referred to is: https://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28
This article was quite helpful and I succeeded to embed the basic function of swipe menu features.
However, after the filter function for the list of the recyclerview was embedded, I noticed that I have to figure out a way how to reset/clear the swipe menu manually/programmatically because after the list of the cardviews are filtered, the swipe menu is lingered in the background of each list but I have no clues how to solve this issue. (as shown in the attached pictures)
The things that I tried are:
notifyDataSetChanged by Adapter - not work
notifyItemChanged by Adapter - not work
reset the ItemTouchHelper (put null and reattach) - not work
recyclerview.performClick to reset the state of the ItemTouchHelper - not work
According to the article, it seems clicking a recyclerview resets the state of the ItemTouchHelper.
Could you please teach me how to click a recyclerview programmatically or any other solutions if available?
The following code is a fraction of the source from the article that I assumed relevant to solve this issue...
private void setTouchUpListener(final Canvas c,
final RecyclerView recyclerView,
final RecyclerView.ViewHolder viewHolder,
final float dX, final float dY,
final int actionState, final boolean isCurrentlyActive) {
recyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
SwipeController.super.onChildDraw(c, recyclerView, viewHolder, 0F, dY, actionState, isCurrentlyActive);
recyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
setItemsClickable(recyclerView, true);
swipeBack = false;
buttonShowedState = ButtonsState.GONE;
}
return false;
}
});
}
After the swipe menu emerged by swiping
Swipe menus remained after filtering the list
I found out that adding the layout-change listener on the recyclerview could remove the swipe menu.
#Override
public int getMovementFlags(#NotNull final RecyclerView recyclerView, #NotNull final RecyclerView.ViewHolder viewHolder) {
myRecyclerView = recyclerView;
myRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
clearSwipeMenu();
}
});
}
....
#Override
public void onChildDraw(#NotNull Canvas c, #NotNull RecyclerView recyclerView, #NotNull RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ACTION_STATE_SWIPE) {
if (buttonShowedState != ButtonsState.GONE) {
if (buttonShowedState == ButtonsState.LEFT_VISIBLE) dX = Math.max(dX, buttonWidth);
if (buttonShowedState == ButtonsState.RIGHT_VISIBLE) dX = Math.min(dX, -buttonWidth);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
} else {
setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
if (buttonShowedState == ButtonsState.GONE) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
currentItemViewHolder = viewHolder;
}
....
public void clearSwipeMenu() {
if(currentItemViewHolder!=null){
clearView(myRecyclerView, currentItemViewHolder);
currentItemViewHolder = null;
}
}

How do I detect when it comes to the end in recyclerview?

I'm filing my data with json to recyclerview.
I want new data in every 10 items in recyclerview.
So
10 data -> arrived 10 more data in recyclerview last ....
So how do I detect that the user is at the end of the recyclerview?
Or, if there is another solution, you can submit your ideas. I use it for now, but sometimes it doesn't work ...
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// MyLog.log("rcv1 " + newState);
}
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
MyLog.log("rcv1 dy: "+ dy);
if (dy>=0 ){
llHeading.setVisibility(View.GONE);
}else {
llHeading.setVisibility(View.VISIBLE);
}
totalItemCount = layoutManager.getItemCount();
visibleItemCount = layoutManager.getChildCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if (!loadingItems && (visibleItemCount + pastVisiblesItems) >= totalItemCount ) {
loadingItems = true;
MyLog.log("rcv1 came to an end.");
getDataClient("",rccount*10,ranking_sort_by,ranking_desc,1);
}
}
});
In your adapter, you could use onBindViewHolder then check the position.
public void onBindViewHolder(ViewHolder holder, int position) {
if (position == getItemCount() - 1) {
// You are at the end of list
}
}
Use this:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
Log.d("tag","scrolled to end");
}
}
});

Hard task - Detect when scrolling from RecyclerViewAdapter no Fragment or activity

Hello I am working on an Android project and i need to detect when scrolling Recycler View, I know you can add OnscrollListener to RecyclerView from Fragment or Activity, but I need to do it from adapter in order to pass data from there to the Fragment, Its something about Realtime Animation, Here is what i have for now in my Adapter
holder.linearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Intent intent = new Intent("custom-message");
intent.putExtra("address",shop.getAddress());
LocalBroadcastManager.getInstance(v.getContext()).sendBroadcast(intent);
}
});
I used the AddonLayoutChangeListener but it doesn`t work as i expect, I cant figure out how to get this, hope some one can help me
I solved it adding a method in Adapter
public Shop getItem(int position) {
Shop shop = shopsList.get(Integer.valueOf(position));
return shop;
}
and then from Fragment adding the following
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (recyclerView.getScrollState() == recyclerView.SCROLL_STATE_IDLE)
{
int postitionCentereitem = carouselaLyoutManager.getCenterItemPosition();
Shop item = (Shop) mAdapter.getItem(postitionCentereitem);
int Id = item.getId();
String Address = item.getAddress();
lblMessage.setText(Address);
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
Hope this can help someone else.

Found when user keep scrolling when is in the top, Android

I want to know when the user keep scrolling (like when you want to update your timeline in Twitter?
Actually I just can use onScrollListener because I'm using this library that make listview capable to implements drag and drop of the listviews
What I want to do, is being capable to "minimize" this listview when the user make the "top to bottom" event, but its imposible to modify the OnTouchListener
What i am doing right now is the next one:
listOrders.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == recyclerView.SCROLL_STATE_DRAGGING) {
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
int marginBottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams layPra = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
layPra.addRule(ALIGN_PARENT_BOTTOM);
layPra.bottomMargin = marginBottom;
frameOrders.setLayoutParams(layPra);
listOrders.setScrollingEnabled(false);
listOrders.setEnabled(false);
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
});
try this:
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.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) {
// Recycle view scrolling up...
} else if (dy > 0) {
// Recycle view scrolling down...
}
}
});

Categories