Horizontal list view in Android? - android

Looking for a horizontal list view GUI componnet in Android similar to UICollectionView in iOS. I trid ListView but was not able to set up to scroll it horizontally.
I need only one line, so no grid like multi line 'table'. I know about TableLayout and GridLayout. Which one is more proper for my purpose?
Looking for not a 3rd party solution but a standard Android one without loading in and library.

You should be using the RecyclerLayout, which allows you to specify your own LayoutManager (then you can specify a horizontal one, or a vertical one, or a grid one, or...)
Here is a good SO with a simple example: How to build a Horizontal ListView with RecyclerView?

You can use RecycleView and add horizontal layoutmanager
RecyclerView listView= (RecyclerView)findViewById(R.id.hlistview);
LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
listView.setLayoutManager(layoutManager);
and in xml
<android.support.v7.widget.RecyclerView
android:id="#+id/hlistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>

Related

Center RecycleView in Parent - Android

I am following a tutorial on grid layout. In particular, I am replicating Suragch's answer.
The code itself is working fine, except I would like to center the RecyclerView in the activity.
I've tried
android:gravity="center"
and
android:layout_gravity="center"
on both the RelativeLayout AND RecyclerView, but neither works.
Also, I would like to eliminate the gaps between each column. Does anyone know how to go about doing this?
Try android:layout_centerInParent=true into <RecyclerView>
Depending on your parent Layout
If its RelativeLayout then
android:layout_centerHorizontal=true in RecyclerView
if its LinearLayout then
android:layout_gravity=center_horizontal in RecyclerView
One more interesting thing you can do in Adapter
the main parent Layout in adapter
try doing this
android:gravity=center_horizontal in main layout of adapter

Recycler view with wrap content items horizontal and vertical scroll

hi i want to make a recycler view to take items horizontal as many as the screen fit but with vertical scroll like this image as example
I try with StaggeredGridLayoutManager but it must specify the number of column want the column take as screen size any help will be good
You can use FlexLayoutManager for this kind of design FlexLayout
Here is a example snippet to use FlexLayoutManager in RecycleView
RecyclerView recyclerView = (RecyclerView) context.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
layoutManager.setJustifyContent(JustifyContent.FLEX_END);
recyclerView.setLayoutManager(layoutManager);
Their are many attributes to use FlexLayout Go through the documents on github
As Burhanuddin Rashid commented, you should use the FlexBoxLayout. It has a LayoutManager for RecyclerViews, so the additional code will be minimal.
https://github.com/google/flexbox-layout#flexboxlayoutmanager-within-recyclerview

How to Create a curved scrollbar in android wear 2.0?

I am using WearableRecyclerView to create a curved layout,but the default scrollbar is still vertical.Is there a way to create a curved scrollbar like android wear 2.0 launcher?
Actually, the scrollbars are circular for any scrollable View that takes up the whole screen. It's a framework feature for Wear 2.0.
If the scrollbars are still vertical, make sure your View really does fill up the whole screen - set it to match_parent and as a top level root View.
use boxinsetlayout
//
android.support.wearable.view.BoxInsetLayout
app:layout_box="left|bottom|right"
...Your list View and other contents
android.support.wearable.view.BoxInsetLayout>
and if you are using wearableRecyclerView
do CircularChildLayoutManager mChildLayoutManager = new CircularChildLayoutManager(mContext);
and set this as layout manager for your recycler view.
mRecyclerView.setLayoutManager(mChildLayoutManager);
This may solve for you.
The API was renamed to CurvedChildLayoutManager
So use
val layoutManager = CurvedChildLayoutManager(this)
recyclerView.layoutManager = layoutManager
PS: as for topic question, you dont need app:layout_box just use android:scrollbars="vertical" on your WearableRecyclerView
https://developer.android.com/reference/android/support/wearable/view/CurvedChildLayoutManager.html

Can RecyclerView be a parent of CardView

I'm new to Android development so I intend to use REST API to fetch blog content. I'll be using RecyclerView for sake of memory and I don't intend to use lists, but rather CardView. Because I want the CardView to inherit all the goodies of RecyclerView.
So in the layout, CardView will the child of RecyclerView instead of ListView.
<android.support.v7.widget.RecyclerView
...>
<android.support.v7.widget.CardView
...>
...
...
Is that possible?
You definitely can have CardViews as items of RecyclerView. But this is not how the XML layout should look like.
Define your list item XML layout in a separate file where CardView will be the root tag. Use recycler view adapter to create items and put them into your recycler view.

How to center RecyclerView in the center (and without scrolling) when possible?

This is a short question:
Suppose I have a RecyclerView that has undefined number of items, what should I do to it, so that if there is a small number of items that (all) can fit the screen, they will be centered and the user won't be able to scroll ?
Of course, if there are too many items, that cannot fit the screen, I would like to have the RecyclerView to show them all as normal (from the beginning to how much it can show, and allow to scroll).
To understand what I mean, I think such a thing is possible when using ScrollView (or HorizontalScrollView if in horizontal), together with a LinearLayout that sets the gravity to be centered .
OK, I think I've found a way:
first, we wait for the RecyclerView to finish its layout process, as I've found about here .
Then, you need to check which child views are shown (available in the LayoutManager that you use), and look at the first and last ones.
If both of them are exactly the same as those that of the total items, it means all needed views are shown, so I can add margins on both sides of the RecyclerView (or padding on its container), according to the space that's left.
I found one complicate way to achieve that.
Main concept: set the height of RV dynamicly in code
RecyclerView rv= (RecyclerView) findViewById(R.id.rv);
rv.setAdapter(new MySlideUpAdapter());
rv.setLayoutManager(new LinearLayoutManager(this));
ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
layoutParams.height=100;
rv.setLayoutParams(layoutParams);
You may need to calculate the RV's height by childcount*childheight to get pricise value. And don't forget to compare the height to the Parent Layout height, make sure RV's height is less than its Parent Layout height.
Here is my Layout
<RelativeLayout...>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_centerInParent="true"
android:layout_width="match_parent"
<-height will be changed in code, ignore the 50dp->
android:layout_height="50dp">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Categories

Resources