Can RecyclerView be a parent of CardView - android

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.

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

Two columns ListView with items of varying heights

I want to create a two columns listView with items of varying heights like that:
image
I have try this custom class found on GitHub and it was perfect but I have different problems because maybe the class is old (last update: 2014):
Child Item's images with onClickListener block the custom listView's scrolling
SwipeRefreshLayout appear always when scroll up (I want only when I am at the top of the list)
[Edit] Solution:
Proposed by Piyush:
StaggeredGridLayoutManager with RecyclerView
you can use StaggeredGridLayoutManager
you can use StaggeredGridLayoutManager
private StaggeredGridLayoutManager gaggeredGridLayoutManager;
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
for more information follow this link StaggeredGridLayoutManager
Why don't you use a ScrollView instead? So you'll have a parent ScrollView containing just one RelativeLayout. Then you'll place two vertical LinearLayout inside the Relative one in which you are going to place your varying heights cells.
Pseudo-code follows:
<ScrollView>
<RelativeLayout>
<LinearLayout orientation="vertical">
//insert you items here via GroupView.addView inside your activity
</LinearLayout>
<LinearLayout orientation="vertical">
</LinearLayout>
</RelativeLayout>
<ScrollView>

Horizontal list view in 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"/>

How to add a LinearLayout in the same activity which has Listview?

I have an Activity where there is a Linear Layout which occupies nearly half of the screen and below that I need to keep a listview which has hold any number of items. Now initially I was doing like this
<ScrollView>
<LinearLayout>
</LinearLayout>
<ListView>
</ListView>
</ScrollView>
But reading all the threads I think it is not really a good idea keeping listview inside Scrollview. Also I am facing a lot of issues like the height of the Listview is not proper.
So how do I keep the layout and listview inside the same activity without using ScrollView?
It is not recommended to include a scrolling View (ListView) inside a ScrollView. What you want to achieve can be done by adding a HeaderView which can be inflated from another XML.
LinearLayout ll = inflater.inflate(R.layout.my_layout, null);
listView.addHeaderView(ll);

ListView item on form only showing one item

I have a form with a number of EditView fields in it. The data for these fields are loaded from a database (in the onCreate() method). The last object on the screen should be a ListView that should show all related data records to the record being show. All the data is correctly loading, and the adapter for this seems to work ok. It loads the correct data, it formats the data correctly into the two-line layout specified by the .xml used by the adapter.
The problem is that the ListView on the screen is "shrunk" to show only one item, and adds a scrollbar if there's more items. I expected the ListView to expand in size to show all records, and the screen itself being scrollable (everything is wrapped inside a ScrollView).
So, the XML looks like this:
<ScrollView
android:layout_height="wrap_content"
android:id="#+id/ScrollView1">
<RelativeLayout
android:layout_height="wrap_content"
android:id="#+id/RelativeLayout1">
<EditView
android:id="#+id/EditView1>
</EditView>
<ListView
android:id="#+id/ListView1
android:layout_height="wrap_content"
android:layout_below="#id/EditView1>
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
I then use a custom BaseAdapter to fill data into the ListView
listView = (ListView) findViewById(R.id.ListView1);
dbRecords = db.getAllRecordsByRecordId(recordId);
CBA_Records adapter = new CBA_Records(this, dbRecords);
listView.setAdapter(adapter);
This is all the same stuff that I've done before, except this is all wrapped inside the scrollview. The reason for this is that there might be more fields than will fit on a smaller screen (or horizontal screen), so the screen must be scrollable. And, the listview must also be there ...
Any suggestions?
A little more onto what invertigo said. ListView inside a ScrollView is not recommended.
Make your ListView the root, and set it's width and height to "match_parent". Put the other stuff that's above the ListView (the header) in a separate xml. Then inflate the new xml file for the header use the addHeaderView() method to add it as a header to the ListView (it looks like you want everything to scroll).
ListView is vertically scrolling automatically, so you now have two vertical scroll areas, which do you expect to consume the scroll event? Either set your ListView to a static height (not recommended), or design your layout so only the area you want to scroll (ie the ListView) has scrolling capabilities and remove the ScrollView. Also set your base layout, in this case the RelativeLayout, to height=match-parent, and ListView height=0dp weight=1 to prevent the ListView from being "shrunk".
Alternatively, take a look at this solution if you dont want the ListView scrolling independantly: android listview display all available items without scroll with static header

Categories

Resources