I have a RecyclerView setup like that:
<androidx.recyclerview.widget.RecyclerView
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarThumbVertical="#android:color/transparent"
app:fastScrollEnabled="true"
app:fastScrollHorizontalThumbDrawable="#drawable/thumb_drawable"
app:fastScrollHorizontalTrackDrawable="#drawable/line_drawable"
app:fastScrollVerticalThumbDrawable="#drawable/thumb_drawable"
app:fastScrollVerticalTrackDrawable="#drawable/line_drawable"/>
// Linear layout manager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
// Touch helper for scrolling
ItemTouchHelper touchHelper = new ItemTouchHelper(touchHelperCallback);
touchHelper.attachToRecyclerView(recyclerView);
// Simple adapter
MyAdapter adapter = new MyAdapter(myDataset);
recyclerView.setAdapter(adapter);
Every item has a circle that, if long pressed, changes the visualization to only allow other items to be selected.
Sporadically, and this is why I can't understand what's going wrong, the long pressed items get an overlapped duplicated "dead" item without ViewModel data that is stuck on the view. If that item gets scrolled off screen (I suppose when it's recycled) it disappears and everything seems ok. You can see it in the next animation.
This is so annoying and I've tried everything but can't track it down. Any idea?
EDIT: New hint, it happens starting from Android 9!
Related
Scroll in Recyclerview list of card view default when scroll faster than scroll more than one card.I need only shows next of current card when scrolling even user how much scroll amount is applied in android.I Googled but not getting a solution. thanks for helping
my code contained MyRecyclerView and LinearLayoutManager looks following lines
MyRecyclerView = (RecyclerView) view.findViewById(R.id.cardView);
MyRecyclerView.setHasFixedSize(true);
LinearLayoutManager MyLayoutManager = new LinearLayoutManager(getActivity());
MyLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (listitems.size() > 0 & MyRecyclerView != null) {
MyRecyclerView.setAdapter(new MyAdapter(listitems));
}
MyRecyclerView.setLayoutManager(MyLayoutManager);
1. You should use smoothScrollToPosition(int position)
Starts a smooth scroll to an adapter position.
recyclerview.smoothScrollToPosition(position);
2. Use scrollToPosition()
void scrollToPosition (int position)
Convenience method to scroll to a certain position. RecyclerView does not implement scrolling logic, rather forwards the call to
RecyclerView.scrollToPosition(postion)
3. rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter).
4. scrollToPositionWithOffset()
void scrollToPositionWithOffset (int position,int offset)
Scroll to the specified adapter position with the given offset from resolved layout start. Resolved layout start depends on getReverseLayout(), getLayoutDirection(android.view.View) and getStackFromEnd().
linearLayoutManager.scrollToPositionWithOffset(1, 10);
You can achieve this by using object of linearLayoutManager.Use this line to move the selected item to show on top of recycler screen.
linearLayoutManager.scrollToPositionWithOffset(YOUR_ITEM_POSITION,OFFSET_YOU_WANT);
For more information on how to use scrollToPositionWithOffset check this link .
https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#scrollToPositionWithOffset(int,%20int)
i add basically some properties to recyclerview to set reverse such as this code:
<android.support.v7.widget.RecyclerView
android:id="#+id/messagesView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
app:reverseLayout="true"
app:stackFromEnd="true"/>
what i try to do? i'm trying to show last item in recyclerview without scroll to last position, in pasted code i have first item in start and i have to scroll to last position programmatically
how can i change this xml layout to have this ability?
You haven't posted any Java code. So try this:
Set this LayoutManager to the RecyclerView.
recyclerView.setLayoutManager(new LinearLayoutManager(context, VERTICAL, true));
Did you try to set the attributes from code? According to this question, that seems to work.
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
Its worked for me , Moves the focus on Last item of the RecyclerView.
mRecyclerView.getLayoutManager().smoothScrollToPosition(mRecyclerView,null, finalCustomItems.size()-1);
or,
mPagerAdapter.highlightPosition(listView.getAdapter().getItemCount() - 1);
I have n items in a list. i.e Item1 ,Item2,Item3..Item-n. I want to open them on the click of a button and make them visible in a horizontal manner. For Example
ITEM1 ITEM2 ITEM3......ITEM-N
How to draw the same in xml design in android.
Please help to solve the same.
Use like it
xml layout
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
Use in Activity class:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView_home_10);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
// If using recylerview in srollview
recyclerView.setNestedScrollingEnabled(false);
// set Adapter
recyclerView.setAdapter(adapter);
LayoutManager is the class that layout views in RecyclerView. So change recyclerView.setLayoutManager(LayoutManager) if you want to change layout. In your case, if you use LinearLayoutManager, do this by calling:
LinearLayoutManager layoutManager = ...
recyclerView.setLayoutManager(layoutManager);
//when you want horizontal
layoutManager.setOrientation(context,LinearLayoutManager.HORIZONTAL,false);
//when you want vertical
layoutManager.setOrientation(context,LinearLayoutManager.VERTICAL,false);
I am trying to programmatically scroll to a position in my RecyclerView, this is what I've tried until now nothing worked in my case:
musicList.getLayoutManager().smoothScrollToPosition(musicList, null, position);
musicList.scrollToPosition(position);
mLayoutManager.scrollToPosition(position);
musicList.getLayoutManager().scrollToPosition(position);
Is there something I missed?
I have a LinearLayoutManager mLayoutManager; defined and set it to RecyclerView as this:
mLayoutManager = new LinearLayoutManager(this);
musicList.setLayoutManager(mLayoutManager);
RecyclerView XML:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/musicList"
android:scrollbars="vertical"
android:scrollbarThumbVertical="#drawable/custom_scrollbar_vertical"/>
Did you try with LinearLayoutManager method scrollToPositionWithOffset
//position 2 and second param is offset 20
mLayoutManager.scrollToPositionWithOffset(2, 20);
this works for me. Hope to work and for you !
Addendum:
The proposed scroll functions do not work correctly in a scenario with a collapsing toolbar while the toolbar is expanded.
It seems that the recycler view has a fixed height, and while the toolbar is expanded, the recycler is moved down, partially out of the screen. As a consequence also the results of the functions findLast[Completely]VisibleItemPosition() are too high. Example: The last visible item is told to be #9, but in fact item #7 is the last one that is really on screen.
I finally chose a workaround like this:
if (newPos > 2 /* or other number > 0 */)
{
AppBarLayout l = findViewById(R.id.appbar);
if (l != null)
{
l.setExpanded(false, true); // collapse the bar, with animation
}
}
mRecyclerView.scrollToPosition(newPos);
Any hints for a better solution are appreciated!
I've got a RecyclerView using the default Linear Layout Manager.
llm = new LinearLayoutManager(getActivity().getApplicationContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
ItemListView = (RecyclerView) rootView.findViewById(R.id.Itemlist);
ItemListView.setLayoutManager(llm);
final float scale = getActivity().getBaseContext().getResources().getDisplayMetrics().density;
I am trying to change the height of that one item view to display additional ui elements when the label is clicked.
I tried this with no success
vh.itemView.setMinimumHeight((int)(375 *(scale/160)));
Can I even change the item height? would I need to implement my own layoutManager?
Hide additional ui elements from item view initially by calling
(some_ui_element).setVisibility(View.GONE);
On item click, unhide the elements by calling
(some_ui_element).setVisibility(View.VISIBLE);
and make sure that the item layout in xml is set to wrap-content for its layout-height. The item cardview (or listview) got resized to show the ui elements.