How to make Recyclerview scroll with its parent? - java

I have a header at the top and a recyclerview underneath it. Like so:
<LinearLayout>
<LinearLayout>
<ImageView/>
<TextView/>
<TextView/>
</LinearLayout>
<RecyclerView/>
</LinearLayout>
I also have a view with a header and two recyclerviews. Like so:
<LinearLayout>
<LinearLayout>
<ImageView/>
<TextView/>
<TextView/>
</LinearLayout>
<Recyclerview/>
<Recyclerview/>
</LinearLayout>
When I scroll up on the recyclerview on the first layout, the header stays there. I want the header to go up and out of the view as I scroll down the recyclerview.
On the second layout, each list is short and could fit on the screen individually, but together the second one goes offscreen (i'm using this layout manager to make the layout wrap content). This would be fine, however when you scroll up on the first recyclerview, it only tries to scroll itself (which is can't because there's nothing to scroll) so you can't scroll to see what's offscreen. You also can't scroll on the header to go offscreen.
In both cases, how can I get the recyclerview to scroll with its parent?

In the first case, you can use CollapsingToolBarLayout.
There're number of nice tutorials/examples about it:
antonioleiva.com/collapsing-toolbar-layout
http://android-developers.blogspot.co.uk/2015/05/android-design-support-library.html
CoordinatorLayout with RecyclerView & CollapsingToolbarLayout
https://www.youtube.com/watch?v=kXmESQ0v6fU
In the second case, I'd suggest to replace 2 RecyclerViews by 1 with different viewTypes for the sake of keeping views recycled during scrolling.

<NestedScrollView>
...your views here...
<RecyclerView
android:NestedScrollViewEnabled="false"/>
</NestedScrollView>

Related

How can I change the width of an image button in a horizontal scroll view?

I'm trying to create a page that looks like the contribution page of GitHub (like each square represents a day, and the transparency of the square represents the number of commits). I want the page to look something like this:
So, I made a horizontal scroll view because I want the section of squares can be scrolled horizontally. And I tried to add the squares into the HSV. Here's the code:
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="295dp"
android:layout_marginTop="10dp"
android:layout_marginRight="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/time">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="20dp" <!-- the problem -->
android:layout_height="35dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/square" />
</HorizontalScrollView>
The problem is, I cannot set the layout_width of the image button, no matter what I change it to, the design always remain like this:
As you can see, the height is correct, but the width is fixed at a weird place. Is there anyway I can change it?
Any help would be appreciated.
You should use the layout inside of the HorizontalScrollView instead of adding the image button directly. For example LinearLayout or GridLayout
The point is that the scroll view can only have one child, because the scroll view extended FrameLayout
Quote from the documentation:
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A HorizontalScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a horizontal orientation, presenting a horizontal array of top-level items that the user can scroll through.

TableLayout - where is my grid?

I have just started work with Android Studio 2.2.3. I picked TableLayout and everything looks fine, but I cant drag and drop anything on this layout except TableRow. Even the grid doesn`t show up. Has anybody had the same issue?
I am not clear what exactly you want to implement, if you want to implement grid view then instead of taking table layout take recycler view and implement it as a grid view. to know more about thie follow this link http://www.android-examples.com/android-recyclerview-with-gridview-gridlayoutmanager/
I think you are TableLayout as root layout with these properties: android:layout_width="match_parent"
android:layout_height="match_parent"
So it will occupies you entire Activity. It allows only TableRow because it is child layout for TableLayout.
Try to change the properties as following or change the TableLayout from Root layout and later add the views to layout.
android:layout_width="wrap_content"
android:layout_height="wrap_content"

LinearLayout inside RelativeLayout uses super-parent as parent

I have the following layout:
<LinearLayout width:fill height:fill >
<RelativeLayout width:fill height:wrap >
<ImageView width:fill height:wrap >
<LinearLayout width:wrap height:wrap />
</RelativeLayout>
</LinearLayout>
The innermost LinearLayout seems to have no idea that it is inside the RelativeLayout when it comes to vertical properties. Gravity works only horizontally. For all intents and purposes (vertically), the inner LinearLayout thinks that the outer LinearLayout is its parent. Aligning parent Top/Bottom stretches it and the RelativeLayout to fill the outer LinearLayout.
Ultimately what I want here is for the relative layout height to wrap the imageview almost as if it was its background (though it's meant to be an overlay on top of another view on top of the background), and the linear layout to simply work inside the constraints of the relative layout.
Furthermore, I want to be able to clip anything that goes outside the bounds of the relative layout wrapping the imageview. What's my best option here?
I think your issue here is that wrap_content does not bound the upper limit of the View (at least by the parent). As such, the height of your RelativeLayout can go on to the full size of it's parent. If it's parent wasn't wrap_content too, it would stretch out to it's parent and so forth until it reaches a parent that has an upper-bound.
In this case, your top parent is bound by the size of the phone, so it stops there. Instead of using gravity on your inner-most LinearLayout. Use the attribute layout_centerInParent="true" on your inner-most LinearLayout. Unless the layout becomes bigger than your ImageView it should conform to the center of your RelativeLayout.
EDIT:
You may also want to experiment with the different Scale Types of the ImageView. There are some that will scale the image as big as it can get without cropping or distorting the ratio. There are some that will always fill the entire view but may crop or distort the image.
There are a lot of things that are unclear from your post, probably you should post the complete version of your XML code.
From my understanding, I guess you're not using RelativeLayout constraints such as layout_alignParentBottom="true" or layout_centerVertical="true". Those will work, unlike the gravity properties.

Can i extend the size of a listview dynamically in java code

I am using a listview in a layout with a size- not in full size of layout.
Below the layout there are components like button.
I need the layout to get extended when more number of listitems adds dynamically.
I have kept the listview and buttons in scrollview.
Is there any attribute or option to make listview of variable length
You should not have a listview inside a scrollview. Instead of having a listview you should add your items to a linearlayout, which will grow to accomodate the items and not enable the scrollbar within itself. This will solve your UI issue. You can set the onclickListeners in a for looop.
Update
<ScrollView.....>
<LinearLayout .....>
<LinearLayout ..../> // this has your list items
<Button .... /> // you can have a layout here if you have multiple buttons.
</LinearLayout>
</ScrollView>
Set the ListView's layout_height value to wrap_content, and it will stretch to accomodate all the items' views in its adapter.

I can't scroll my customized view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<ScrollView android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<adam.music.testfont.NwcDrawingArea android:id="#+id/NwcDrawingArea"
android:layout_height="2000dp"
android:layout_width="2000dp"
/>
</ScrollView>
</LinearLayout>
I created Android Project and edited main.xml as above.
And then I customized view to draw image.
To see the effect of scrolling customized view I set width and height as above.
Now I can see the image in a customized view but it doesn't scroll.
Could you help me what the problem is?
Should I add something more?
There's an interplay between ScrollView and its contents. For example, if, instead of your NwcDrawingArea widget, you inserted this TextView into your ScrollView:
<TextView android:text="8This is a lot of text 7This is a lot of text 6This is a lot of text 5This is a lot of text 4This is a lot of text 3This is a lot of text 2This is a lot of text 1This is a lot of text"
android:layout_width="10dp"
android:layout_height="2000dp"
android:background="#FF00FF00"
/>
You'll see a skinny green vertical TextView whose text is longer than the screen, and the ScrollView shows scrollbars that let you see the hidden part of the TextView to the extent of the text. Now, change the TextView layout_width="2000dp". The TextView becomes a full-screen green area that has a single line of text that runs off the right side of the screen. ScrollView, of course, shows no horizontal scroll bars. However, ScrollView shows no vertical scroll bars either, even though we sized the TextView to be much longer than the screen. ScrollView is attempting to determine the visually interesting portion of its contents, so you need to understand the behavior for whatever widget you are subclassing.
For example, ScrollView respects the layout sizes of LinearLayout and RelativeLayout in the way that you expected it to behave with TextView. In fact, it is common to make a LinearLayout or RelativeLayout -- rather than a view such as TextView -- the child of a ScrollView. Drop your TextView in a fixed-height LinearLayout and you should get the behavior that you expected.

Categories