This code works as intended:
LinearLayoutManager layoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.scrollToPosition(2);
The positioning and scroll are vertical and third item is displayed.
Now, if in the above code VERTICAL is replaced with HORIZONTAL, items are displayed horizontally and scroll is horizontal (as intended), but setScrollToPosition(2) doesn't work anymore.
How to scrollToPosition with HORIZONTAL LinearLayoutManager?
My XML file is mentioned below:
< android.support.v7.widget.RecyclerView
android:id="#+id/rvPosters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp" >
Related
I'm using recycler view with a layout that consists of 4 text views but I want to display more than 5 columns in the layout with horizontal scroll view? Is it feasible and possible? If yes, can anyone share any link or code?
Thank you.
For Horizontal scroll view, set your RecylerView layoutManager as follows,
LinearLayoutManager linearLayoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView=(RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(linearLayoutManager);
In RecyclerView ,
android:layout_width="match_parent"
android:layout_height="0dp"
In RecyclerView item layout,
android:layout_width="wrap_content"
android:layout_height="wrap_content"
I am using a Recycler view inside a Horizontal Scroll View to display threaded comments like below:
Comment1
Comment1Child1
Comment1Child2
Comment1Child2Child1
Comment1Child3 Comment2
etc.
I have done this with the following XML:
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbarSize="2dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/commentRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="47dp"
android:minWidth="200dp" />
</HorizontalScrollView>
You'll see I set a min width of 200dp so that the comments never get too small and the deeper down the thread they go the further off screen it will be pushed.
It is displaying perfectly but I am unable to scroll horizontally. I can see the scroll bars but I cannot scroll horizontally.
I have fiddled with disabling Touch and nestedScrollingEnabled in the RV but not sure what the real solution is. Nothing seems to work?
Any help would be much appreciated! Thank you
Remove HorizontalScrollView you can give LinearLayoutManager with horizontal orientation like this
LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
reclyclerView.setLayoutManager(layoutManager);
//recyclerView.setMinimumWidth(200); optional for width if u need
I want to result like this:
which can do horizontal-scrolling.
But in fact, like this below:
Which can't display all item and not horizontal-layout.
who can help me ? thanks. other way can reach my wanted result here?
You need to add Layout manager to RecylerView.
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recylcerView.setLayoutManager(layoutManager);
This will make the RecylerView horizontal.
To set recyclerView as horizontal scrollView you can use LinearLayoutManager. by default it sets vertical scroll behaviour.
LinearLayoutManager layout = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);//0 Horizontal , 1 vertical
recyclerView.setLayoutManager(layout);
in layout.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/yourRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/layout_past_round_header"
android:layout_centerInParent="true"
android:overScrollMode="never" />
in your NestedScrollView add this
android:fillViewport="true"
I'm developping a Android app, and I got a vertical NestedScrollView, who take all my screen, and inside multiple hoizontal RecyclerView. It's work, but the horizontal scroll is really hard to achieve.
I mean, when I scroll horizontally the gesture is catch by the NestedScrollView, and the view move up/down. I need to focus and do a real horizontal movement to scroll the RecyclerView, it's killing UX...
Here my NestedScrollView :
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/scrollLayout"
android:orientation="vertical" />
</android.support.v4.widget.NestedScrollView>
I'm inflating RecyclerView programmatically, because the number is define in network data, here the inflated layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:textSize="#dimen/secondary_text_size"
android:textAllCaps="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="#dimen/recycler_view_size" />
</LinearLayout>
And how I set my RecyclerView in Java :
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.recycler_view_layout, null, false);
RecyclerView recyclerView = (RecyclerView) layout.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(this);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setNestedScrollingEnabled(false);
I tried to change the "velocity" NestedScrollView, but I can't find good advice/post, maybe I don't find the good one. Someone can help ?
Hope you find it useful, as I think you need to implement one of these libraries (on GitHub you would find more):
View Flow for Android: https://github.com/pakerfeldt/android-viewflow
HorizontalListView: https://github.com/MeetMe/Android-HorizontalListView
TwoWayGridView https://github.com/jess-anders/two-way-gridview
If you want to do it with standard Android libraries, RecyclerView might be useful if you want to scroll horizontally
To prove that it has horizontally scrolling support, take a look at:
public boolean canScrollHorizontally ()
Query if horizontal scrolling is currently supported. The default
implementation returns false.
Returns True if this LayoutManager can scroll the current contents horizontally
or
public int computeHorizontalScrollExtent (RecyclerView.State state)
Override this method if you want to support scroll bars.
Read computeHorizontalScrollExtent() for details.
Default implementation returns 0.
From: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager.html
Check also subclass of RecyclerView called HorizontalGridView
Hope it help.
I'm using a RecyclerView inside a SwipeRefreshLayout in Android.
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/mySwipeRefresh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
In this RecyclerView i'm using a LinearLayoutManager with reverse layout and stack from end:
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
myRecyclerView.setLayoutManager(layoutManager);
So, the scrolling content starts from bottom and items are added by adapter from bottom to top.
With this configuration, i'd like to show SwipeRefreshLayout when RecyclerView is at the bottom and user try scrolling more down. Instead, the results is that SwipeRefreshLayout has the standard behavior and is shown from top to bottom animation when user scrolls to top and RecyclerView is at the top.
I didn't find apis to do this, how can i solve this issue?
Thanks
I resolved using this library since SwipeRefreshLayout doesn't support reverse layout
Refresh layout bottom