I am getting this error "Syntax error on tokens, AnnotationName expected instead".Can anyone resolve it.-- Android
mRecyclerView.setLayoutManager(new LinearLayoutManager(products.this){
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if(totalItemCount!=array1.size()-1){
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
}
});
}
});
I m getting the error on line 2 i.e.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
Thanks, in advance.
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()){
// what ever you want to do here is only for LinearLayoutManager methods
});
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// implement your scrolled code here
}
});
Related
I am practicing to build a chat app. When I insert an item to the recyclerview, I need it to scroll to the bottom. My recyclerview code for now looks like this. I tried adding an onScroll listener and that approach was wrong. Thanks!
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
((SimpleItemAnimator) binding.chatRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
linearLayoutManager.setReverseLayout(true);
binding.chatRecyclerView.setLayoutManager(linearLayoutManager);
binding.chatRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
Log.i(TAG, "onScrolled: " + firstVisiblePosition);
if (dy < 0) {
binding.chatDate.setVisibility(View.VISIBLE);
binding.chatDate.setText(sfdMainDate.format(new Date(chatadapter.getItem(firstVisiblePosition).getTimestamp())));
} else if (dy > 0) {
binding.chatDate.setVisibility(View.GONE);
}
}
});```
Add the data observer to your adapter and override onItemRangeInserted.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
binding.chatRecyclerView.smoothScrollToPosition(0);
}
});
I'm trying to implement pagination using code below, and it work well with fragments, but when i implemented it in Nested Fragment it load all data (load first page and all next pages continuously). The problem is that my total items is 12 and visible in time is 3 but layoutManager.getChildCount() give me 12 same as total item (layoutManager.getItemCount()) so it start loading next pages continuously.
Here is oNScroll code :
public PaginationScrollListener(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= getTotalPageCount()) {
loadMoreItems();
}
}
}
When scroll and request next pages :
recyclerView.addOnScrollListener(new PaginationScrollListener(layoutManager) {
#Override
protected void loadMoreItems() {
isLoading = true;
currentPage += 1;
// mocking network delay for API call
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadNextPage();
}
}, 1000);
}
#Override
public int getTotalPageCount() {
return TOTAL_PAGES;
}
#Override
public boolean isLastPage() {
return isLastPage;
}
#Override
public boolean isLoading() {
return isLoading;
}
});
change your scroll listener like below:
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy>0){ //if is scrolling vertically
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()) {
if (firstVisibleItemPosition + visibleItemCount >= totalItemCount) {
//make your request here
}
}
}});
I tried implementing addOnScrollListener() but this thing fetch data and loading the recycler view from the beginning every time.i want to load the data after the current position.Here is my sample code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_load, null);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
myClickHandler();
business_rv = (RecyclerView) view.findViewById(R.id.business_rv);
business = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
business_rv.setHasFixedSize(true);
business_rv.setLayoutManager(business);
if (business_rv.getLayoutManager() instanceof LinearLayoutManager) {
business = (LinearLayoutManager) business_rv.getLayoutManager();
business_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
UpdateRecyclerView(url);
}
});
}
return view;
}
what you need is endless recycler view ,which will load the data while scrolling . Here I have attached link for that implementation.
Follow this link
use this as
_recycler.addOnScrollListener(_recyclerViewOnScrollListener);
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = 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) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading && !isLastPage) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= PAGE_SIZE) {
loadMoreItems(++currentPage);
}
}
}
};
Which statement should i put in "Recyclerview_position_is_bottem" to satisfy condition that now recyclerview position is bottem to execute function
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (Recyclerview_position_is_bottem){
isScrolled=0;
skip = skip + 5;
remove = 1;
feedItems.add(null);
eventsRecyclerView.notifyItemInserted(feedItems.size());
handler.postDelayed(new Runnable() {
#Override
public void run() {
Loadmore(eventsRecyclerView,1);
}
}, 500);
}
}
});
first / last visible child depends on the LayoutManager.
If you are using LinearLayoutManager or GridLayoutManager, you can use
int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
For example:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may be prefered to layout more items than visible for caching.
You can use LayoutManager's object which you set on your recyclerView like this:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int totalItems = layoutManager.getItemCount();
int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
if (totalItems - 1 == lastVisibleItem){
isScrolled=0;
skip = skip + 5;
remove = 1;
feedItems.add(null);
eventsRecyclerView.notifyItemInserted(feedItems.size());
handler.postDelayed(new Runnable() {
#Override
public void run() {
Loadmore(eventsRecyclerView,1);
}
}, 500);
}
}
});
I created Recycler View Grid. And I want realize pagination. But I do not understand how do it. I Found one answer enter link description here
But it not work for me. I have not method mLayoutManager.findFirstVisibleItemPosition(); in my LayOutManager. And method mRecyclerView.setOnScrollListener is deprecated. How can I realize pagination in Recycler View Grid?
Late answer but you can try this..and It works perfectly
allMovie_recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
val layoutManager = allMovie_recycler_view.layoutManager as GridLayoutManager
val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition()+1
if (visibleItemCount == layoutManager.itemCount){
//Load more data
}
}
}
})
Here is the proper way to paginate with StaggeredGridLayoutManager.
The only difference is in findFirstVisibleItemPositions(), which returns an int[] for the first visible position in each span.
mRecyclerView.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) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = mGridLayoutManager.getChildCount();
int totalItemCount = mGridLayoutManager.getItemCount();
int[] firstVisibleItemPositions = mGridLayoutManager.findFirstVisibleItemPositions(null);
if (!mIsLoading && !mIsLastPage) {
if ((firstVisibleItemPositions[0] + visibleItemCount) >= totalItemCount
&& firstVisibleItemPositions[0] >= 0
&& totalItemCount >= Config.PAGE_SIZE) {
loadMorePosts();
}
}
}
});
I know the answer is late. For others who may encounter this, use this solution.
mRecyclerView.addOnScrollListener(mRecyclerViewOnScrollListener);
private RecyclerView.OnScrollListener
mRecyclerViewOnScrollListener = 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) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading && currentPage < totalPages) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= numberOfItemsInAPage) {
loadMoreItems();
}
}
}
};