Layout: How to put a list below a card view? - android

I have this layout (picture below). This CardView shows the information of a member. I have a list in the Room database. I want to let this layout show the list below the CardView. How can I do that?
This card here with a,b and c shows the member. Below that, I want the list shown.

1st add to library in your gradle:
compile 'com.android.support:recyclerview-v7:23.3.+'
compile 'com.android.support:cardview-v7:23.3.+'
You can create a one activity and in activity take one recyclerview.
After that you can create a Row of recyclerview and in this row take a cardview and in this card view take widgets.
Create a model class and set gatter & setter method.
Create a Adapter class to set a row and bind the List.
No in activity initialize a reyclerview and set a adapter also set some static value in list.
Follow This link for perfect example : https://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/

Use a LinearLayout that holds a card and then the list below that:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Your Current Card Layout Here -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.cardview.widget.CardView>
<!-- Your list of more cards -->
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" /
</LinearLayout>
Use a viewholder which uses the card view as its layout with the adapter

take a linear layout and set it orientation to vertical and after that put your card view inside this linear layout first and then put your list view or recycler view.
result will be like card view will be shown first and the list view will be shown below the card view.

Related

Why listview not showing up in xml?

Everything is done i.e adapter is created, the layout is created and data is passed but when I add ListView to the main activity, then nothing appears. I think the problem is somewhere in xml. Please help
Activity2.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.student.shopifysalespediasample.Activity2"
android:orientation="vertical"
android:id="#+id/list"
android:drawSelectorOnTop="true">
<!-- android:drawSelectorOnTop="true" shows a visual effect while clicking on listItem views-->
It shows blank screen instead of showing a sample list item in xml
tools:listitem | tools:listheader | tools:listfooter
These attributes are intended for <AdapterView> (and its subclasses like <ListView> and <RecyclerView>) and used to specify the layout that should be drawn inside that adapter as a list item, header or footer. For example, fragment_contacts_xml layout of our Contacts+ app declares <RecyclerView> and this is how it looks like before and after adding tools:listitem=”#layout/contact_item”
tools:itemCount
This attribute is intended solely for <RecyclerView> and used to specify the number of list items the layout editor should render in the layout preview.
By default Android Studio shows 10 list items for <RecyclerView>.
Therefore usually after adding tools:listitem attribute the <RecyclerView> covers the entire layout screen and you can no longer see other view elements below it. In such cases tools:itemCount attribute will help you to see the elements below the <RecyclerView>.
More you can find in hidden gems article.
You need to add ArrayAdapter or CustomAdapter along with list item layout to populate list view. If after adding adapter problem persists then please share complete code. Use this link for reference. https://www.vogella.com/tutorials/AndroidListView/article.html

Two List View inside scrollview

I want to create multiple list view inside ScrollView in Android. I have created the two list view inside ScrollView. In first List View each row contains a single text upto 5 rows will be presented. Whereas, in second list view each row will contains multiple paragraph text, ie, text very long . In my case I am unable to scroll the second list to view fully.
Is any other way available to handle this scenario ?
Its Work in My RecyclerView try this:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>
You need to disable nested scrolling programatically. It doesn't seem to work correctly if done in xml.
recyclerView.setNestedScrollingEnabled(false);
Don't use the listview inside the scroll view, the listview is already scrollable . Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.

select one view to display between 2 views in android?

i have two view
<VideoView
android:id="#+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/lighter_gray"
android:id="#+id/imageView"
/>
I want to select only one view to shown at run time , is there container for them to do the job ?
i searched and found something like viewswitcher ,but i dont want to switch between them i only want to display one of them
You can set video view and image view visibility to gone in xml by adding android:visibility="gone"
and setVisibility to visible in java code based on the requirement or which view you want to show.
If you have hundreds of items in list.
You can simply managed by list adapter with one own type field and create custom adapter with the list and switch the views on each position as per your requirement.

View invisibility when RecyclerView is empty

I am using a LinearLayout with some views inside. The second last is a RecyclerView, and the last one is an <include> tag linked with a RelativeLayout. I want the last view to be visible permanently, as I want to use it to add items to the RecyclerView.
My problem is that the <include> view below the RecyclerView disappears whenever the adapter is empty. Is there any way to avoid this behaviour?
EDIT
There is a constraint in the way I want the RecyclerView and the "add" View to work together. I would like that it feels as the "add" view is always the last item in the RecyclerView, as it happens in Google Keep lists.
Example Google Keep list example (I cannot add images yet)
You could try to replace your LinearLayout with a RelativeLayout; then you anchor the bottom view to the bottom of the screen, and the RecyclerView would be set above that view. Something like this (just a skeleton):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent>
<-! other views here -->
<include layout="#layout/something"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#bottomView" />
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/some_view_above_recyclerview"
android:layout_above="#+id/bottomView" />
</RelativeLayout>

How to add multiple rows of one linear layout into a single parent layout dynamically in program

I have a row.xml file which define a row whose content and number of rows will be dynamic.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/room_detail_row_in_hotel_detail">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/sz_blue"
android:id="#+id/price"/>
</LinearLayout>
The best solution as suggested by #Hell is to use a Listview.
Say you are operating within a fragment, add a listview in the xml for the same.
The fragment (or activity) containing the listview will dynamically create list(preferrable)/array of data.
In your case the dataset will essentially either be a list of class object which uniquely have two objects, price and name.
Create an adapter, could be a BaseAdapter or any other form depending on your specific use-case. This adapter would essentially inflate your row layout (as specified in your question) and dynamically add the data.
Add the data into the adapter using it's constructor. Following which assign this adapter as the listview's adapter and call the "notify" method for the adapter indicating that a new dataset has been loaded and the list needs to be refreshed.
Also note, it's highly suggestible to add smoothScroll to your listview for obvious reasons.
I guess what you are looking for is a RecyclerView.
You should use the Viewholder-Pattern to reuse your layouts

Categories

Resources