I use a RecyclerView that shows a list of entries. Each entry hosts another RecyclerView that is a list of images.
I now want to make this nested RecyclerView clickable, not the items of it, but the whole view.
How can I achieve that?
Problem:
setting the onClickListener for the main view of the nested RecyclerView does work, but only if I click outside the RecyclerView itself
clicking on the nested RecyclerView does not hand on the clicks to the parent view (I even tried to set clickable, focusable, focusableIntouch to false, still, the touch is not delegated but consumed by the nested RecyclerView...
Here's the view for the wrapping adapter:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="0dp"
android:layout_margin="5dp"
android:foreground="?android:attr/selectableItemBackground" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rlTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Datum"
android:textStyle="bold"
android:id="#+id/tvDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Info"
android:id="#+id/tvInfo" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvData"
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"
android:layout_below="#+id/rlTop"
android:layout_width="match_parent"
android:layout_height="68dp"
android:scrollbars="horizontal" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I looked into RecyclerView source and this helped:
recyclerView.setLayoutFrozen(true)
This is an old question, but i have the same task and didn't find good solution.
I just placed the transparent view over recyclerview and set click listener to this view.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/dataLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/history_margin_v"
android:paddingEnd="#dimen/history_margin_h"
android:paddingLeft="#dimen/history_margin_h"
android:paddingRight="#dimen/history_margin_h"
android:paddingStart="#dimen/history_margin_h"
android:paddingTop="#dimen/history_margin_v">
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/widget_margin_v"
android:textColor="#color/font_blue"
android:textSize="#dimen/font_size_large"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/images"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:id="#+id/clickView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:layout_alignLeft="#+id/dataLayout"
android:layout_alignRight="#+id/dataLayout"
android:layout_alignStart="#+id/dataLayout"
android:layout_alignEnd="#+id/dataLayout"
android:layout_alignTop="#+id/dataLayout"
android:layout_alignBottom="#+id/dataLayout"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
In order to do the entire row clickable and not every image u need to implement a custom onItemClickListener (name comes from listview , sorry).
Take a look at this link
this worked out perfectly for me.
Edit:
The nested recycler steals your clicks. in order to fix that u will need to create a cusom touch listener and pass it to the nested recycler. Just like the one that u put to the outer ercycler but pass the event to the outer.
Edit 2:
Add a selector with two states just like a button to the entire row, then at the onclick on the inner recycler setSelected(true) the row and postDelay with 50ms for setSelected(false) this will give the "click" effect.
Related
I have a RecyclerView in an xml and a LinearLayout at the bottom of that Activity. This activity works as a chat window where a question appears as the chat message and user gets some random multiple choice options in the view, but when the RecyclerView gets populated with a page full of data, I want to make that RecyclerView always above the layout where options are created, but what I have done so far does not work as expected, the RecyclerView data always gets hidden under the layout and each time I need to scroll down to see what's written there. I have used the method
adapter.notifyItemInserted(array.size());
recyclerView.scrollToPosition(array.size());
This adds item in the RecyclerViewand also scrolls down but it gets hidden under the layout, but I have set it always above the layout in the xml, I think the problem is I am creating some views dynamically in that layout which's size is wrap content, so the RecyclerView is not being updated time to time with the height of the layout in the bottom. Here is the xml code of that Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChatBotActivity">
<include
android:id="#+id/header"
layout="#layout/header"></include>
<android.support.v7.widget.RecyclerView
android:id="#+id/rcv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/ll"
android:layout_below="#+id/header" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:visibility="invisible"
android:weightSum="7">
<EditText
android:id="#+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="Type a message"
android:paddingLeft="#dimen/ten_dp"
android:textColorHint="#color/fade" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginRight="#dimen/five_dp"
android:layout_weight="2"
android:background="#android:color/transparent"
android:onClick="sendClick"
android:src="#drawable/ic_send_black_24dp"
android:text="send" />
</LinearLayout>
</RelativeLayout>
The RecyclerView is always above of the LinearLayout which id is ll but I am populating that layout from the java and it's height changes time to time, but RecyclerView height doesn't changed accordingly, please help!
Finally I myself solved the question, It was a funny mistake I was doing, the layout was all right, but my recylerview was just not scrolling, I had to change this:
Previous:
adapter.notifyItemInserted(array.size());
recyclerView.scrollToPosition(array.size());
Later:
adapter.notifyItemInserted(array.size()-1);
recyclerView.scrollToPosition(array.size()-1);
hello I'm trying to use Recyclerview,
below code results only a blank screen, can you please tell me where I'm going wrong
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top|center_vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/ImageSelectorRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
<ImageView
android:id="#+id/ImageViewSelectPhoto"
android:layout_width="80dp"
android:layout_height="76dp"
android:src="#drawable/ic_add_a_photo_black_361px"
android:background="#drawable/back"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
Since you have provided
android:layout_width="match_parent"
android:layout_height="match_parent"
for your RecyclerView, its taking the full width and height of your device or parent view. And the ImageView is outside the device screen.
Now you have just declared the RecyclerView in your XML. You need a RecyclerView Row Layout which should define the layout for the RecyclerView items.
To show something in the RecyclerView you can set static data in the Row Layout layout iteself or need to create a RecyclerView adapter and model for dynamic/static data.
You need to make a list item layout for your Horizontal-scrollview and a seperate adapter also for it.
Try adding android:viewfillport="true"
I too asked a similar type of question ,please have a look to it yu will surely get a simple way to implement this .
Horizontal Listview Not Working from github
I think it should be similar to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="top|center_vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/ImageSelectorRecycler"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/ImageViewSelectPhoto"
android:layout_width="80dp"
android:layout_height="76dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:src="#android:drawable/ic_dialog_alert"
android:background="#android:color/holo_green_dark" />
</LinearLayout>
I've got a tabbed viewpager, where swiping to the next page is disabled. The reason is, inside the fragment, I have a horizontal RecyclerView that should get the touch event.
Fragment XML :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Federal Representatives"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:paddingTop="15dp"
android:paddingLeft="15dp"
android:textColor="#000000"
xmlns:android="http://schemas.android.com/apk/res/android" />
<android.support.v7.widget.RecyclerView
android:id="#+id/federalPager"
android:scrollbars="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
The problem is this RecyclerView won't show up... unless I delete the TextView.
At that point it looks like this :
But If I keep them both, only the TextView shows up, but not the RecyclerView.
What am I missing?
Your TextView has this attribute:
android:layout_height="match_parent"
Which tells it to be as tall as the parent is... so that pushes the RecyclerView off the bottom... try changing it to wrap_content instead.
I am struck into a situation where I am finding no way to proceed. Actually I have a RecyclerView of cardViews and there is an adapter that populates the card with data. I have to show some restaurants in the card. On click of a button in the Cardview, I have to show another recyclerview that will be populated with cardViews containing the branches of that restaurant.
Here i have just one Model that can return both type of data. I am not getting whether I should use 1 Adapter with multiple viewHolders or 2 Adapters. My View is also a single layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_medium">
<android.support.v7.widget.CardView
android:id="#+id/recipient_card"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:elevation="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/receipient_card_height"
android:orientation="horizontal"
android:id="#+id/linear_layout">
<ImageView
android:id ="#+id/arrow_button"
android:layout_width="#dimen/size_info_icon"
android:layout_height="#dimen/size_info_icon"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_gravity="center" />
<com.inquirly.opinify.View.CustomTextView
android:id="#+id/business_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/label_small"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_gravity="center"
android:layout_weight="10"
android:text="JustBake"/>
<ImageView
android:id="#+id/recipient_business_logo"
android:layout_marginLeft="#dimen/margin_small"
android:layout_gravity="center"
android:layout_width="#dimen/receipient_business_logo_size"
android:layout_height="#dimen/receipient_business_logo_size"/>
<ImageView
android:id="#+id/expand_button"
android:layout_marginLeft="#dimen/margin_small"
android:layout_gravity="center"
android:padding="#dimen/margin_medium"
android:layout_width="#dimen/height_button"
android:layout_height="#dimen/height_button"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_recipient_branch_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visible= "false"
android:padding="#dimen/margin_medium"/>
</RelativeLayout>
I have made the inner recyclerView invisible in the beginning. When the user clicks on the arrow button in the main recycler's CardView button, the second recylerview should appear with the list of branches.
This is the screen shot of what i am trying to achieve.
Why don't you do a implement a expandable RecyclerView instead. Check out this article. Also it's recommended that you check this question out.
I have a main Scrollview layout with a LinearLayout including several elements:
ExpandableListView #1 (1 children for yet)
TextView
ExpandableListView #2 (1 children for yet)
Several text views
The issue I encounter is when I click on any of the ExpandableListView, le display by itself is not expanding.
You can see the non-expanded view in this screen-capture (not clikced yet):
https://plus.google.com/photos/101037819512700772601/albums/5870915782494751761/5870915784486932322
You can see the should be expanded view (once I clicked on the ExpandableListViews) in this screen captures:
https://plus.google.com/photos/101037819512700772601/albums/5870915782494751761/5870915780856077570
There you have my layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/expandableView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
<TextView
android:id="#+id/providerNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="ABCDEF1"
android:textSize="25sp"
android:textStyle="bold" />
<ExpandableListView
android:id="#+id/expandableView2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
<TextView
android:id="#+id/providerNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="ABCDEF2"
android:textSize="25sp"
android:textStyle="bold" />
. . .
</LinearLayout>
</ScrollView>
Do you have a solution for my issue?
First things first. You can't nest multiple vertical scrolling views. Otherwise you're going to have a field day trying to override the appropriate touch events to get everything right.
If you need other views to scroll as the list scrolls, you can simply add header/footer views. If you need that to be 'sticky' a cursory Google search came up with Sticky headers library.