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

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.

Related

The last item in the recyclerView list

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
}
}
});

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");
}
}
});

implement On click listener in RecyclerView Scroll listener

I'm using Recycleview to show contacts images and name in Horizontal RecyclerView and it load fine. I have implemented Click listener which on click opens new activity and show details os user based on position of RecyclerView.
But I want autoscroll for RecyclerView such that on every 5 sec, recycleview automatically scrolls and I implemented below code and it works fine.
top.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastItem = llm.findLastCompletelyVisibleItemPosition();
if(lastItem == llm.getItemCount()-1){
mHandler.removeCallbacks(SCROLLING_RUNNABLE);
Handler postHandler = new Handler();
postHandler.postDelayed(new Runnable() {
#Override
public void run() {
top.setAdapter(null);
top.setAdapter(staggeredBooksAdapter);
mHandler.postDelayed(SCROLLING_RUNNABLE, 5000);
}
}, 5000);
}
}
});
mHandler.postDelayed(SCROLLING_RUNNABLE, 5000);
But now when I click on any contact then nothing happens as before where new activity launches with user details. But when I remove scroll listener then again I can click on RecyclerView items.
My RecyclerView is
final LinearLayoutManager llm = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);
top.setLayoutManager(llm);
top.setHasFixedSize(true);
staggeredBooksAdapter = new TopAdapter(this, bookslist);
top.setAdapter(staggeredBooksAdapter);
and from adapter class is
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
database = FirebaseDatabase.getInstance();
g = bookslist.get(position);
holder.teacher_location.setText(g.getBlocas());
holder.teacher_name.setText(g.getSellername());
holder.profile_details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
g = bookslist.get(position);
Intent intent = new Intent(v.getContext(), gender_details.class);
intent.putExtra(KEY_IMAGE, g.getPics());
v.getContext().startActivity(intent);
}
});
How can I solve this..Thanks in advance.
Add this class to your project and then add below code:
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
this, top, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Log.i("recyclerView ", "onItemClick " + position);
}
#Override
public void onLongItemClick(View view, int position) {
Log.i("recyclerView ", "onLongItemClick " + position);
}
}));
Let me know if it works.

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