Android Studio preview - set an adapter to a RecyclerView - android

Is it possible to set up an Adapter to a RecyclerView on the layout xml file so that it renders it design-time? Similar to the way it uses tools:context=my.package.MyFragment to render other things.

So far I know, you can't really add your own adapter, but is possible to use the decent default preview adapter, specifying your layout item.
Something like that:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/your_item_layout"
/>
</FrameLayout>

Related

How can i merge two recycler views such that they scroll one after another?

I tried to implement this answer and it worked but only if the both list are prepopulated already, but in my case one list is prepopulated and the other is incremented/decremented dynamically.
Here is my layout structure
Black box represents the recycle list which can be incremented/decremented dynamically, and its main action is to show the users favorite fonts.
Red box represents all the fonts which are available and they are prepopulated.
i want both these list to flow each-other in scroll, such that user can not notice that these are actually two different list.
my xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:padding="#dimen/home_section_padding"
android:background="#color/background"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:id="#+id/editText"
android:textColorHint=" #C0C0C0"
android:hint="Paste"
android:textColor="#color/text_color"
android:inputType="text"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:text="Style"
android:textStyle="bold"
android:textColor="#color/text_color"
android:layout_marginBottom="10dp"
android:fontFamily="serif-monospace"
android:textAppearance="?android:textAppearanceLarge"
android:background="#drawable/round_shape_for_button"
android:onClick="fire"
android:id="#+id/button"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyelerViewFav"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyelerView"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
</LinearLayout>
There is 2 options, You can either use ConcatAdapter to combine both adapters in one adapter and use one RecyclerView (Recommended). Or use NestedScrollView to wrap both your RecyclerViews and make sure to set both RecyclerView heights to wrap_content.
But based on what you are trying to achieve, I would recommend to use ConcatAdapter(adapter1, adapter2) to wrap both your adapters and set that new adapter on one RecyclerView. You will still be able to control each adapter by itself Add, Remove and calling notifyDataSetChanged on the changed adapter will reflect changes on the RecyclerView.
I think you should use two lists in one RecyclerView adapter.And have two different view holders.Whenever you need to make a change just make the change in specific array list (1 or 2) and call notifyDatasetChanged() on the single adapter you have.
Something like this answer
Create 2 different xml file apart from activity_main.xml ,create 2 external java file associated with these 2 XML file for user handling code purposes. Give your desired layout for recycler view in those 2 XML file,map those XML file in MainActivity.java class using some java class which will extend RecyclerView.Adapter class and override some methods as onCreateViewHolder(),onBindViewHolder().To inflate the recycler view data ,create 1 more java file which will use some getter() & setter() inorder to populate the recycler view with data by the help of the class extending RecyclerView.Adapter class.

When to use CardViews vs tools:listitem when creating RecyclerViews?

I came across this code:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
tools:listitem="#layout/recyclerview_item" />
where tools:listitem was a layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
style="#style/word_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_light" />
</LinearLayout>
My question is, when creating recyclerviews, when should we choose Cardviews as its items and when should we choose layouts such as the one mentioned above?
What are the advantages one provides over the other or are they the same(Note that the recyclerview_item layout XML doesn't have any Cardview tags, so they aren't atleast literally the same)?
tools:listitem only sets a design-time preview for a list item by providing a specific layout resource, be it a CardView-based layout or something different.
You can use tools:listitem along with other tools attributes and #tools/sample resources to emulate components' runtime or compile-time behaviors such as layouts, dummy data, visibility e.t.c.
Those attributes do not affect your app's runtime behavior in any way.
Additionally, you can read this tutorial on tools attributes.
As for cards vs regular layouts, there are some in using cards as containers: cards support elevation, shadows, rounded corners and have a consistent visual style while supporting different content lengths with no additional actions required e.t.c
Personally, I'd use a CardView-based layouts in cases where I need to show some list items with straightforward layouts in a platform-consistent way.

Android Studio: Can you display a view just for preview?

My app has a lot of views that are containers for fragments (which load an image and other views) and depend on an API to fetch images. To make the development of the design easier, I like to add a sample of that image in my xml. Right now, I'm adding a RelativeLayout with the FragmentContainer and a dummy ImageView using different visibility values for android:visibility and tools:visibility.
Is there a better way to show images just for preview purposes ? I'd like to have the preview Views not compiled in the release version.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:adjustViewBounds="true"
tools:src="#drawable/image"
tools:visibility="visible" />
<RelativeLayout
android:id="#+id/FragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
If I understood your problem correctly, you could do something like this:
instead of using dummy views, use
<include tools:layout="#layout/layout_preview" layout="#layout/layout_actual"/>
where layout_preview.xml is whatever you want to use only in the preview, and layout_actual.xml is what will be used in the app
in case you wanted to only add a view in the preview, but have no view at all in the app, you can use a layout_actual.xml with an empty merge tag
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"/>
if you don't want to include useless layout, you might want to create the dummy ones only for debug build type, it will show an error in the layout because layout_dummy will be missing but since it's tools attribute you should be able to compile and run

Reuse a rendered layout in some other layout at run time

I am using a RecylerView inside a layout l1.xml. I am including this l1.xml inside l2.xml using include tag.
I update this RecyclerView after an api call but l2.xml is not showing the updated RecyclerView.
Is there a way to forcibly ask the parent to refresh?
invalidate(), refreshDrawableState(); on the parent layout didn't help?
Is there a smarter way to use a rendered layout in multiple places?
l1.xml
...
...
<LinearLayout
android:id="#+id/feed"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#color/light_primary_background">
<include layout="#layout/events_list"/>
</LinearLayout>
...
...
events_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/events_recycler_view"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
I update the events_recycler_view after an API call and the events_list.xml is updated but the include in l1.xml is not updated
Yes you can always use LayoutInflater to inflate a view, but the view must have the ids and type matching the id and type defined in your java code.
Check out this link for how to use layout inflater
http://www.programcreek.com/java-api-examples/android.view.LayoutInflater

How to preview Android ListView with custom row and header layout

I have created a list view with a custom layout for the header and rows items many times but what always annoys me is the UI preview in Android Studio does not show a preview. Obviously because the custom layouts are loaded programmatically by the ListView or CursorAdapter but what if I wanted to some how specify a header and footer layout in xml so that I could see a preview. Any one know how to do that?
You can use tools:listitem. Just add the tools namespace in the root of the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
(...)
and then in your ListView set your list item layout
<ListView
android:id="#+id/mylistView"
tools:listitem="#layout/my_list_item"
(...)
Is also possible to set header/footer with listheader/listfooter.
xmlns:tools="http://schemas.android.com/tools" namespace are using for development purposes(preview) and are not compiled in the application.
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listfooter="#layout/item_footer"
tools:listheader="#layout/item_header"
tools:listitem="#layout/item" />

Categories

Resources