So I've spent the last few hours trying to create a horizontal scrolling Recyclerview.
I'm pretty sure that I'm following the correct techniques for setting the LayoutManager for the Recyclerview, but it just won't work. on the preview it works, but once I push it to the Emulator, it still renders the list in vertical mode instead of horizontal.
I haven't made any references/changes to the Recyclerview programmatically (I tried setting the LayoutManager before, but it also didn't work).
Screenshot showing the issue:
try adding this code to your java class in the onCreate Method
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));//set your recyclerview layout to hoizontal
also try posting your java or kotlin code next time to get better feedback.
You should call Layoutmanager from fragment/activity. If you are using Kotlin, do this
XML
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Fragment/Activity
val layout = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recycler.layoutManager = layout
*Change this with requireContext() if in fragment
Related
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);
Such as ListView, it invokes smoothScrollToPosition(index)
Android Developer Doc doesn't have HorizontalListView. I downloaded this custom HorizontalListView from GitHub, but I'm confused about how to use HorizontalListView on scrolling automatically.
could recyclerview scroll loop, and it scroll in specified time
I recommend using recyclerview with a linear layout manager that is set in horizontal mode
If you need only a HorizontalListView RecycleView is the way to go.
Just add this line of code to convert it to a HorizontalListView
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);
Refrence can be found here https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
you can use recycle view and set layout manager as follows
listView= (RecyclerView)findViewById(R.id.hlistview);
LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
listView.setLayoutManager(layoutManager);
See this example on github
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);
RecyclerView is shown with vertical orientation, and can not set this property, because orientation belongs to the LinearLayoutManager object. Any way to simulate HORIZONTAL layout?
Anyway the linearLayoutManager created in code is set to HORIZONTAL.
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
try to add these parameters to your RecyclerView inside your layout xml:
android:orientation="horizontal"
android:scrollbars="horizontal"
app:layoutManager="LinearLayoutManager"
RecyclerView won't show your actual data in the Editor Layout as the data you add in your RecyclerView is added Dynamically in your java code through Adapter and you implement a layout manager for your recyclerView in your java code and your Editor Layout displays the elements that are in your xml, so in your XML file its just a plain recyclerView which has no idea of what data is added in your Java implementation or what LayoutManager is to be implemented on it.
AFAIK, there is no such facility editor provide yet.
although you can set the orientation of LinearLayoutManager Horizontal at runtime, this will fulfill your requirement.
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
I'm implementing an Android TV app and I'm using HorizontalGridView from the Leanback library. I have a custom layout.
I have to scroll the HorizontalGridView to specific position, after activity is created, but unfortunately the scrollToPositio(position) method is not working on this layout at all. It just do nothing. I found, that when I specifically set the layout manager to LinearLayoutManager it works. But the problem is, that when I'm not using leanback default HorizontalGridView LayoutManager, there is a problem with focusing next items using D-pad.
Basically if I use normal RecyclerView, the control with D-pad is not working as expected, so I decided to go with leanback implementation, where this problem is solved, but so far I cannot make it work with scrollToPosition method.
Any ideas?
Snippet of my code:
Layout:
<android.support.v17.leanback.widget.HorizontalGridView
android:id="#+id/photo_gallery_recycler"
android:layout_width="match_parent"
android:layout_height="#dimen/gallery_image_size"
android:clipChildren="false"
app:itemView="#{viewModel.photoItemView}"
app:items="#{viewModel.photosUrl}"/>
Code [Kotlin]:
binding.photoGalleryRecycler.scrollToPosition(position)
binding.photoGalleryRecycler.getChildAt(position)?.requestFocus()
And I also tried some hacks like this:
// save default leanback layout manager
var defaultLayoutManager = binding.photoGalleryRecycler.layoutManager
// set LinearLayoutManager
binding.photoGalleryRecycler.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
// scroll to position and request focus
binding.photoGalleryRecycler.scrollToPosition(position)
binding.photoGalleryRecycler.getChildAt(position)?.requestFocus()
// set default layout manager back to the view
binding.photoGalleryRecycler.layoutManager = defaultLayoutManager
you need to be using setSelectedPosition(position)
If animation is required you can try setSelectedPositionSmooth(position)
setSelectedPosition Developer docs.