I am trying to retrieve data from firebase then display it in recycler view. I set the layout to be reverselayout = true. However, when I run the activity, the view starts at the bottom. I tried to change the initial position when the application is run using scrollToPosition as commented below, but still nothing changes. Does anyone have a solution related to this problem?
I've tried this: https://stackoverflow.com/a/26876044/7825519
But still start from bottom.
mRecyclerView!!.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
(mRecyclerView!!.layoutManager as LinearLayoutManager).reverseLayout = true
(mRecyclerView!!.layoutManager as LinearLayoutManager).stackFromEnd = true
mRecyclerView?.scrollToPosition(3)
// mRecyclerView?.smoothScrollToPosition(3)
// (mRecyclerView!!.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(3,0)
// mNestedScrollView?.scrollTo(0,3)
mAdapter = DiscoverAdapter(mItems, mPostKey, Static.mLikedPosts, this)
mRecyclerView!!.adapter = mAdapter
val spacingInPixels = resources.getDimensionPixelSize(R.dimen.margin_between_card)
mRecyclerView!!.addItemDecoration(SpacesItemDecoration(spacingInPixels))
First of all delete the both lines that make the recyclerview start from the botom.
Set the scrolling after you set the adapter:
mRecyclerView!!.adapter = mAdapter
mRecyclerView?.scrollToPosition(3)
Try to set scrollToPosition using post method of View
Related
I created a ViewPager2 view with a DiffUtil adapter, that is updated when I get data from several APIs.
However, the problem is that the views overlap when I update the adapter.
As you can see in this video.
Click to view the video on Vimeo
//viewpager
binding?.realtimeTrending?.apply {
orientation = ViewPager2.ORIENTATION_HORIZONTAL
adapter = adapter
offscreenPageLimit = OFFSCREEN_PAGE_LIMIT_DEFAULT
isNestedScrollingEnabled = true
}
// adding value to viewpager
adapter?.updateRealtime(it.value)
fun updateRealtime(value: List){
// Update the adapter when new data is available
val callback = DiffUtil.calculateDiff(DiffCallback(list, newlist))
list.clear()
list.addAll(newlist)
callback.dispatchUpdatesTo(this)
}
Please help me with the issue. I am stuck with this for a while now.
I have a RecyclerView that receive values from a API.
and the current configuration that apply is this:
recyclerView.apply {
layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
addItemDecoration(GridItemDecoration(4, 2))
setHasFixedSize(true)
adapter = this#BusinessActivity.adapter
}
however when i refresh the data
refreshBusiness.setOnRefreshListener {
refreshBusiness.isRefreshing = true
skeleton.showSkeleton()
businessViewModel.retrieveBusiness()
}
the whole view goes blank.
If i apply another LayoutManager such as (LinearLayoutManager or GridLayoutManager) instead of the StaggeredGrid, everything goes well.
and im assuming that the problem is related with the param that the Staggered won't receive and that is the context.
Anyone know how to solve this issue.
The recycler view sometimes show or not show the data when I start the activity. This is how it's programmed, I have a main activity and an activity that has a recycler view in it. To test it I hardcoded a data using a for loop in the onCreate method. It will only loop and add data to the array before it is set to the recycler view. Now the problem is that, sometimes the data shows up and sometimes there's nothing in the list. I can easily reproduce the issue by going back and fort to the main activity and the activity with recycler view. I think I'm doing it wrong by adding it to the onCreate.
Here's what I have in the onCreate:
for(i in 1..5){
val newDeliveredAsset = AssetDelivered()
newDeliveredAsset.id = i
newDeliveredAsset.accountNumber = 1
newDeliveredAsset.barCode = "BB" + i.toString()
newDeliveredAsset.description = "test " + i.toString()
assets.add(newDeliveredAsset)
}
rvAsset = findViewById(R.id.ce_recycler_view)
layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
rvAsset.layoutManager = layoutManager
rvAdapter = AssetRecyclerViewAdapter(this, assets as List<Asset>)
rvAsset.adapter = rvAdapter
I also tried to log the getItemSize of the adapter and it shows the correct number.
Update: Left the phone on the activity that's not showing the recycler view items. After few minutes, the items showed up.
I need a recycler view with different number of columns in a row. For that I have used a gridlayout manager with dynamic span size. Here is my code:
rvSubCat.layoutManager = StaggeredGridLayoutManager(4, 1)
val viewTreeObserver = rvSubCat.getViewTreeObserver()
viewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener { calculateSize() })
categoryAdapter = CategoryAdapter(categoryList, true, this)
rvSubCat.adapter = categoryAdapter
private fun calculateSize() {
val spanCount = Math.floor((rvSubCat.getWidth() / SizeUtils.dp2px(sColumnWidth.toFloat())).toDouble()).toInt()
(rvSubCat.getLayoutManager() as StaggeredGridLayoutManager).spanCount = spanCount
}
But the desired result is not good. It is fixing the number of columns in every row. I need the number of columns should depend upon the total width of the row items.
Need this (Desired Result):
Getting This
Best light weight library i use
compile 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
Benifit is you dont need to change your existing code, you just need to change recyclerview's LayoutManager like
recyclerView.setLayoutManager(new FlowLayoutManager());
You are all set, Happy coding :)
Update You know already how to populate recycler view. But verify by following
private void setAdapter() {
recyclerView = (RecyclerView) fragment_view.findViewById(R.id.rv);
recyclerView.setLayoutManager(new FlowLayoutManager());
myRecyclerAdapter = new AdapterOrders(getActivity(), list);
recyclerView.setAdapter(myRecyclerAdapter);
}
Call myRecyclerAdapter.notifyDataSetChanged(); after list is change from any source.
What you need is flow Layout. I think these library will help you solve the purpose :
https://github.com/nex3z/FlowLayout
https://github.com/ApmeM/android-flowlayout
Adjusts according to size of texts. Hope this helps !
How can I get a current item in RecycerView? In my use-case RV item takes all screen space, so there is only one item at a time.
I've tried to google before, but nothing helpful was found.
The whole use case:
UI. The bottom navigation has a button that should call a certain method (let's say a foo() method) in fragment
this foo() method should get a visible item from the adapter and call a specific method in a presenter.
use this in your code
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
and get your visible item using below code
mLayoutManager.findFirstVisibleItemPosition();
You could also use an OnItemClickListener on your RecyclerView's ListAdapter to determine the current item. In that case the item needs to be clicked though.
My solution
val position = (recyclerView?.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
val product = adapter?.getItemByPosition(position)
FirebaseCrash.logcat(Log.DEBUG, TAG, "onNavClickEvent = $product")
product?.let { productListViewModel?.order(it.id) }
The result is:
onNavClickEvent = Product(id=1, title='Cap', description='some descr.', price=10, photo='http://lorempixel.com/200/200/sports/1/')