I am trying to change height of android appBarLayout to make effect like this (example is from iOS but I am trying to do similar things in android):
When we scroll up appBar height of appBar should decrese and picture should scaled.
Example 1: http://take.ms/nxVJ7
Example 2: http://take.ms/jXQst
After some research and several attemptes I have some progress, but I have no idea what to do next.
This is my activity_profile:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:theme="#style/AppTheme.AppBarOverlay"
android:minHeight="250dp"
app:elevation="0dp"
app:layout_behavior=".utils.behaviors.DisappearBehavior"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:id="#+id/title"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="#+id/app_bar_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="online"/>
<ImageButton
android:id="#+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_circle"
android:src="#drawable/ic_edit"
android:tint="#color/colorPrimary"
android:visibility="gone"/>
<ImageView
android:id="#+id/vip_status_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/status"
android:src="#drawable/ic_vip"
android:tint="#color/colorPrimary"/>
</RelativeLayout>
<ImageView
android:id="#+id/avatar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_avatar_placeholder"
android:adjustViewBounds="true"
android:layout_weight="1"/>
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="Демин Сергей"
android:textColor="#color/colorBlack"
android:textSize="18sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_avatar_placeholder"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_avatar_placeholder"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_avatar_placeholder"
/>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frame_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
app:layout_behavior="#string/appbar_scrolling_view_behavio">
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"/>
<android.support.v4.widget.NestedScrollView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
And DisappearBehavior:
public class DisappearBehavior extends CoordinatorLayout.Behavior<AppBarLayout> {
public DisappearBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onStartNestedScroll(
CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
if (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL) {
return true;
} else {
return false;
}
}
#Override
public void onNestedPreScroll(
CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
consumed = new int[2];
consumed[1] = dy;
ViewGroup.LayoutParams params = child.getLayoutParams();
Log.d("Behavior", "dy = " + dy);
params.height -= dy;
child.setLayoutParams(params);
}
#Override
public void onNestedScroll(
CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed,
int dxUnconsumed,
int dyUnconsumed) {
ViewGroup.LayoutParams params = child.getLayoutParams();
child.setLayoutParams(params);
int xxx = child.getBottom();
Log.d("Behavior", "height = " + params.height + " " + xxx);
}
#Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) {
super.onStopNestedScroll(coordinatorLayout, child, target);
ViewGroup.LayoutParams params = child.getLayoutParams();
child.setLayoutParams(params);
int xxx = child.getBottom();
Log.d("Behavior", "stop height = " + params.height + " " + xxx);
}
}
In the result I have effect when both appBarLayout and FrameLayout with content scrolling simultaneously (I'll try to add images if I'll found a way how to do it. I need more than 10 reputation. image3, image4).
Also there is some starnge effect. When you scroll up (for example) slowly you could see text on screen moving up and the again down. Smth like quake effect.
As you see I have logs in the code and they says me that dy is positive and negative when I scroll in one direction.
Log for slow up scroll for example:
dy = 42
height = 924 1026
dy = -36
height = 960 984
dy = 43
height = 917 1020
dy = -37
height = 954 977
dy = 42
height = 912 1014
dy = -36
height = 948 972
dy = 48
height = 808 916
dy = -44
height = 852 868
dy = 46
height = 806 912
dy = -46
height = 852 866
stop height = 852 866
I understand that all problems come AppBarLayout$ScrollingViewBehavior but I do not understand what to change.
Related
I have a view in my android project like :
I want to show a header when I scroll down when the Title (lblHeaderJobTitle in the code below) reaches the top, and hide it back when the title is visible in the screen when I scroll up.
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayoutHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:paddingStart="#dimen/margin4"
android:paddingEnd="#dimen/margin4"
android:layout_gravity="top"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/lblHeaderJobTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_semibold"
android:textSize="#dimen/t4"
android:includeFontPadding="false"
android:textColor="#color/darkGrey"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:MvxBind="Text Title"/>
<TextView
android:id="#+id/lblHeaderJobCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/margin2"
android:fontFamily="#font/mulish_bold"
android:textSize="#dimen/t2"
android:textColor="#color/darkGrey"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/lblHeaderJobTitle"
app:layout_constraintEnd_toStartOf="#id/verticalSeparatorHeader"
app:MvxBind="Text CompanyName"/>
<TextView
android:id="#+id/verticalSeparatorHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:fontFamily="#font/mulish_regular"
android:textSize="#dimen/t2"
android:textColor="#color/darkGrey"
app:layout_constraintTop_toTopOf="#id/lblHeaderJobCompanyName"
app:layout_constraintStart_toEndOf="#id/lblHeaderJobCompanyName"
app:layout_constraintEnd_toStartOf="#id/lblHeaderJobLocation"/>
<TextView
android:id="#+id/lblHeaderJobLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/mulish_regular"
android:textSize="#dimen/t2"
android:textColor="#color/darkGrey"
android:layout_marginLeft="#dimen/margin2"
app:layout_constraintTop_toTopOf="#id/lblHeaderJobCompanyName"
app:layout_constraintStart_toEndOf="#id/verticalSeparatorHeader"
app:MvxBind="Text LblLocation"/>
<View
android:id="#+id/imgHeaderSeparator"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#color/silver"
android:layout_marginTop="11dp"
app:layout_constraintTop_toBottomOf="#id/lblHeaderJobLocation"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.core.widget.NestedScrollView
android:id="#+id/nestedScrollViewJobDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/margin4"
android:paddingEnd="#dimen/margin4">
<ImageView
android:id="#+id/imgBrandingLogo"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerInside"
android:background="#drawable/bg_roundrect_ripple_light_border"
app:MvxBind="DrawableName DefaultCompanyPhotoDrawable"/>
<TextView
android:id="#+id/lblJobTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin3"
android:layout_marginBottom="#dimen/margin2"
android:fontFamily="#font/poppins_semibold"
android:textSize="#dimen/t8"
android:includeFontPadding="false"
android:textColor="#color/darkGrey"
app:layout_constraintTop_toBottomOf="#id/relativeLayoutJobDetail"
app:MvxBind="Text Title"/>
<ImageView
android:id="#+id/imgJobLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin1"
android:src="#drawable/ic_location_on_black_24dp"
app:layout_constraintTop_toBottomOf="#id/lblJobTitle"/>
<TextView
android:id="#+id/lblJobCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin2"
android:layout_marginRight="#dimen/margin2"
android:fontFamily="#font/mulish_bold"
android:textSize="#dimen/t4"
android:textColor="#color/darkGrey"
app:layout_constraintStart_toEndOf="#id/imgJobLocation"
app:layout_constraintTop_toTopOf="#id/imgJobLocation"
app:layout_constraintEnd_toStartOf="#id/verticalSeparator"
app:MvxBind="Text CompanyName"/>
<TextView
android:id="#+id/verticalSeparator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:fontFamily="#font/mulish_regular"
android:textSize="#dimen/t4"
android:textColor="#color/darkGrey"
app:layout_constraintTop_toTopOf="#id/lblJobCompanyName"
app:layout_constraintStart_toEndOf="#id/lblJobCompanyName"
app:layout_constraintEnd_toStartOf="#id/lblJobLocation"/>
<TextView
android:id="#+id/lblJobLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="#font/mulish_regular"
android:textSize="#dimen/t4"
android:textColor="#color/darkGrey"
android:layout_marginLeft="#dimen/margin2"
app:layout_constraintTop_toTopOf="#id/lblJobCompanyName"
app:layout_constraintStart_toEndOf="#id/verticalSeparator"
app:MvxBind="Text LblLocation"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayoutBtnGroup"
android:layout_width="match_parent"
android:layout_height="63dp"
android:visibility="invisible"
android:paddingStart="#dimen/margin4"
android:paddingEnd="#dimen/margin4"
android:paddingBottom="#dimen/margin2"
android:layout_gravity="center_horizontal|bottom">
<Button
style="#style/Button.Tertiary"
android:id="#+id/btnSaveNow"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:layout_marginLeft="#dimen/margin2"
android:fontFamily="#font/poppins_semibold"
app:icon="#drawable/ic_star_black_24dp"
app:layout_constraintTop_toTopOf="#id/frameLayoutApplyFix"
app:layout_constraintStart_toEndOf="#id/frameLayoutApplyFix"
app:layout_constraintBottom_toBottomOf="#id/frameLayoutApplyFix"
app:layout_constraintEnd_toEndOf="parent"
app:MvxBind="Text BtnSave; Click SaveCommand"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In my activity I tried this:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
_constraintLayoutHeader = FindViewById<ConstraintLayout>(Resource.Id.constraintLayoutHeader);
_constraintLayoutBtnGroup = FindViewById<ConstraintLayout>(Resource.Id.constraintLayoutBtnGroup);
((CoordinatorLayout.LayoutParams)_constraintLayoutBtnGroup.LayoutParameters).Behavior = new StickyBottomBehavior(Resource.Id.frameLayoutApply, Resource.Id.nestedScrollViewJobDetail, EPosition.Bottom);
((CoordinatorLayout.LayoutParams)_constraintLayoutHeader.LayoutParameters).Behavior = new StickyBottomBehavior(Resource.Id.lblJobLocation, Resource.Id.nestedScrollViewJobDetail, EPosition.Top);
}
In my custom behavior:
public class StickyBottomBehavior : CoordinatorLayout.Behavior
{
#region Properties
private int _anchorId;
private int _scrollViewId;
private EPosition _position;
#endregion
#region Constructor
public StickyBottomBehavior(int anchorId, int scrollViewId, EPosition ePosition)
{
_anchorId = anchorId;
_scrollViewId = scrollViewId;
_position = ePosition;
}
#endregion
public override bool OnStartNestedScroll(CoordinatorLayout coordinatorLayout, Java.Lang.Object child, View directTargetChild, View target, int axes, int type)
{
return (axes == ((int)Vertical));
}
public override void OnNestedPreScroll(CoordinatorLayout coordinatorLayout, Java.Lang.Object child, View target, int dx, int dy, int[] consumed, int type)
{
var anchor = coordinatorLayout.FindViewById(_anchorId);
NestedScrollView scrollView = coordinatorLayout.FindViewById<NestedScrollView>(_scrollViewId);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)scrollView.LayoutParameters;
int[] anchorLocation = new int[2];
View childView = child.JavaCast<View>();
anchor.GetLocationInWindow(anchorLocation);
switch (_position)
{
case EPosition.Top:
if (anchorLocation[1] < 0)
{
childView.Visibility = ViewStates.Visible;
layoutParams.TopMargin = childView.Height;
scrollView.LayoutParameters = layoutParams;
}
else
{
childView.Visibility = ViewStates.Invisible;
layoutParams.TopMargin = 0;
scrollView.LayoutParameters = layoutParams;
}
break;
case EPosition.Bottom:
if (anchorLocation[1] < 0)
{
childView.Visibility = ViewStates.Visible;
layoutParams.BottomMargin = childView.Height + 8;
scrollView.LayoutParameters = layoutParams;
}
else
{
childView.Visibility = ViewStates.Invisible;
layoutParams.BottomMargin = 0;
scrollView.LayoutParameters = layoutParams;
}
break;
default:
break;
}
}
}
public enum EPosition
{
Top,
Bottom
}
For the sticky buttons in the bottom it works very well but for the header there is smt wrong when I reach the Title in the top the screen is moving weirdly.
Demo: https://www.veed.io/view/6e1c9b71-b10d-46c7-a284-18b974a0bb7e
Is there another way to do that ? or there is smt wrong in my code ?
Thanks for the help.
I'm trying to add a parallel effect in between bottom sheet layout and map view.but google log in map view is hidden when expending the bottom sheet layout. [Google logo is hidden]
show google logo when bottom sheet collapse
I'm using below code:
layout
<android.support.design.widget.CoordinatorLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Green"
tools:context=".ui.activities.ChooseLocationActivity">
<RelativeLayout
android:id="#+id/map_rlyt"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.koya.android.ui.widget.CollapseBehavior"
app:layout_anchor="#+id/bottom_sheet_layout">
<fragment
android:id="#+id/g_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/category_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/category_search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/custom_edit_text_margin_top"
android:layout_marginLeft="#dimen/custom_edit_text_margin_left_right"
android:layout_marginRight="#dimen/custom_edit_text_margin_left_right"
android:background="#android:color/transparent">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/ic_back_iv"
android:padding="#dimen/location_activity_back_button_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#mipmap/ic_back_black"/>
<EditText
android:id="#+id/editTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/custom_edit_text_padding"
android:textColor="#color/black"
android:drawablePadding="8dp"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="1"
android:drawableLeft="#mipmap/ic_search_gray"
android:textCursorDrawable="#drawable/edit_text_cursor_drawable"
android:drawableRight="#mipmap/ic_close_gray"
android:textSize="#dimen/custom_edit_text_text_size"
android:background="#drawable/drawable_custom_edittext"
/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Button
android:id="#+id/redo_search_in_map_area_button"
style="#style/CategoryChooseButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/category_search_box"
android:layout_centerInParent="true"
android:layout_marginTop="#dimen/fifteen_text_size"
android:animateLayoutChanges="true"
android:paddingEnd="#dimen/redo_search_button_padding"
android:paddingStart="#dimen/redo_search_button_padding"
android:text="#string/redo_search_in_map_area"
android:textAllCaps="false"
android:visibility="gone"
/>
</RelativeLayout>
<include
android:id="#+id/bottom_sheet_layout"
layout="#layout/bottom_sheet"/>
<Button
android:id="#+id/select_location_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/use_selected_location"
android:visibility="gone"
tools:visibility="visible"
style="#style/CategoryChooseButtonStyle"/>
<include android:id="#+id/error_layout"
layout="#layout/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
/>
And Implement Coordinatorlayout.Behavior
public class CollapseBehavior<V extends ViewGroup> extends CoordinatorLayout.Behavior<V>{
public CollapseBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {
if (isBottomSheet(dependency)) {
BottomSheetBehavior behavior = ((BottomSheetBehavior) ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior());
int peekHeight = behavior.getPeekHeight();
// The default peek height is -1, which
// gets resolved to a 16:9 ratio with the parent
final int actualPeek = peekHeight >= 0 ? peekHeight : (int) (((parent.getHeight() * 1.0) / (16.0)) * 9.0);
if (dependency.getTop() >= actualPeek) {
// Only perform translations when the
// view is between "hidden" and "collapsed" states
final int dy = dependency.getTop() - parent.getHeight();
ViewCompat.setTranslationY(child, dy/2);
return true;
}
}
return false;
}
private static boolean isBottomSheet(#NonNull
View view) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof CoordinatorLayout.LayoutParams) {
return ((CoordinatorLayout.LayoutParams) lp)
.getBehavior() instanceof BottomSheetBehavior;
}
return false;
}
}
I need this type of scrolling in my application -->
https://streamable.com/g0tf9
How to scroll google map when bottom sheet expend with parallax effect? I would appreciate it a lot!
Thanks in Advance.
Helo, I create a recyclerview with complex items (its CardView with nested RecyclerView with grid layout manager). And I have problem with lags durring scroll. To fix that, I try to put main recyclerView in NestedScrollView, and add scroll listener(when NestedScrollView scrolled to end of content) for pagination. When I scroll first page, it works perfect, but, when new page was loaded, NestedScrollView change own height with freeze. And its look likes some catastrophe. Maybe you faced with some problem?
My recyclerView Item:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/news_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/white"
android:layout_margin="5dp"
app:cardElevation="3dp"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/news_item_avatar_img"
android:layout_margin="15dp"
android:layout_width="40dp"
android:layout_height="40dp"/>
<TextView
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/news_item_avatar_img"
android:layout_toLeftOf="#+id/news_item_more_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/news_item_full_name"
android:textStyle="bold"
android:textSize="12sp"
android:text="Кот черный"
android:textColor="#color/colorAccent"/>
<TextView
android:layout_marginRight="10dp"
android:layout_below="#+id/news_item_full_name"
android:layout_toRightOf="#+id/news_item_avatar_img"
android:layout_toLeftOf="#+id/news_item_more_img"
android:id="#+id/news_item_date"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:textColor="#color/gray_middle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:visibility="gone"
android:id="#+id/news_item_more_img"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_more_vert_gray_18dp"/>
</RelativeLayout>
<TextView
android:maxLines="6"
android:id="#+id/news_item_content_txt"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/light_black"/>
<android.support.v7.widget.RecyclerView
android:visibility="visible"
android:layout_marginBottom="5dp"
android:id="#+id/news_item_image_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_centerVertical="true"
android:id="#+id/news_item_like_count_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_favorite_black_14dp"/>
<TextView
android:layout_toRightOf="#+id/news_item_like_count_img"
android:padding="5dp"
android:layout_centerVertical="true"
android:text="0"
android:textSize="14sp"
android:textColor="#color/gray_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/news_item_like_count_txt"/>
<ImageView
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/news_item_like_count_txt"
android:layout_centerVertical="true"
android:id="#+id/news_item_comment_count_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_chat_bubble_black_14dp"/>
<TextView
android:layout_centerVertical="true"
android:padding="5dp"
android:layout_toRightOf="#+id/news_item_comment_count_img"
android:text="0"
android:drawablePadding="5dp"
android:textSize="14sp"
android:textColor="#color/gray_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/news_item_comment_count_txt"/>
<ImageView
android:visibility="gone"
android:layout_toLeftOf="#+id/news_item_label_txt"
android:layout_centerVertical="true"
android:id="#+id/news_item_label_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_label_black_14dp"/>
<TextView
android:visibility="gone"
android:gravity="right"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:id="#+id/news_item_label_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/gray_middle"
android:text="#string/group_str"/>
</RelativeLayout>
</LinearLayout>
#Override
public void onBindViewHolder(NewsItemHolder holder, final int position) {
final PostEntity postEntity = newsList.get(position);
holder.fullName.setText(userFullName);
holder.date.setText(postEntity.getCreateAt());
holder.content.setText(text);
holder.likeCount.setText(String.valueOf(postEntity.getLikesList().size()));
holder.commentsCount.setText(String.valueOf(postEntity.getCommentsList().size()));
if(postEntity.getUserAvatar() != null) {
Picasso.with(context)
.load(postEntity.getUserAvatar().getThumb())
.fit()
.centerCrop()
.into(holder.avatar);
}
if(postEntity.getPhotosList() != null && postEntity.getPhotosList().size() > 0) {
holder.imageContainer.setVisibility(View.VISIBLE);
int cardWidth = width - dpToPx(16);
ArrayList<PhotoEntity> shortListOfPhoto = new ArrayList<>();
if(postEntity.getPhotosList().size() > 5){
shortListOfPhoto.addAll(postEntity.getPhotosList().subList(0, 5));
} else {
shortListOfPhoto.addAll(postEntity.getPhotosList());
}
final GalleryAdapter galleryAdapter = new GalleryAdapter(context,shortListOfPhoto, postEntity.getPhotosList(), cardWidth);
GridLayoutManager mLayoutManager = new GridLayoutManager(context, 4);
holder.imageContainer.setLayoutManager(mLayoutManager);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return ClubUtils.getSpanLookup(galleryAdapter.getItemCount(), position);
}
});
galleryAdapter.setHasStableIds(true);
holder.imageContainer.setNestedScrollingEnabled(false);
holder.imageContainer.setAdapter(galleryAdapter);
holder.imageContainer.setHasFixedSize(true);
} else {
holder.imageContainer.setVisibility(View.GONE);
}
}
Here scroll is NestedScrollView
scroll.getViewTreeObserver().addOnScrollChangedListener(() -> {
View view = (View) scroll.getChildAt(scroll.getChildCount() - 1);
Log.d("CategoryNeswList", scroll.getChildCount() + " child");
int diff = (view.getBottom() - (scroll.getHeight() + scroll
.getScrollY()));
if (diff == 0) {
// getPlaylistFromServer("more");
Toast.makeText(mContext, "Load More", Toast.LENGTH_SHORT).show();
}
});
try to do pagination like this.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
if (totalItemCount != TOTAL_JOBS) {
loading = false;
Log.e("total", "ite" + totalItemCount + " count" + TOTAL_JOBS);
PAGE_COUNT = PAGE_COUNT + 10;
serviceCall();
} else {
loading = false;
}
}
}
}
}
});
in this code TOTAL_JOBS : total no of jobs coming from server,
totalItemCount : is the total items in recyclerView ,
set loading = true everytime if there is still response coming for recycleView;
SOLVED CHECK ANSWER BELOW...
So I am trying to create a comments functionality for my Android app and I want to display the comments inside a recyclerview and then have a button and textview below the recyclerview to add comments. I want to have the recyclerview a certain height and make it scrollable if there are lots of comments because I dont want the users to have to scroll down the screen to find the add button.
I couldn't get it to work so I was wondering if anyone else had that issue.
I have all the adapter and everything set up, I am just having trouble with the recyclerview.
Thanks.
I prob was not clear on what I was trying to accomplish. I am trying to create a cardview where it will display all the comments and the ability to add a new comment. The recyclerview will take up approx 80% of the height and then the last 20% is for the edittext and button.
My XML (Scroll to last cardview where the recyclerview is)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollview">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfilePageActivity"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profilepagetoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginTop="35dp"
android:layout_below="#+id/profilepagetoolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:id="#+id/aboutCard">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_alignTop="#+id/aboutCard"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="-60dp"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/text"
android:textSize="20sp"
android:text="ABOUT" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/dividers" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<ImageView
android:id="#+id/nameicon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="8dp"
android:transitionName="appIcon"
android:background="#drawable/ic_account_circle_black_24dp"/>
<TextView
android:id="#+id/Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/secondary"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<ImageView
android:id="#+id/locationicon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="8dp"
android:transitionName="appIcon"
android:background="#drawable/ic_map_black_24dp"/>
<TextView
android:id="#+id/Location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="11dp"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/secondary"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<ImageView
android:id="#+id/websiteIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="8dp"
android:transitionName="appIcon"
android:background="#drawable/ic_explore_black_24dp"/>
<TextView
android:id="#+id/Website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="11dp"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/secondary"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginTop="35dp"
android:layout_below="#+id/aboutCard"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:id="#+id/writeComment"
android:layout_alignParentTop="false"
android:layout_alignParentBottom="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="-100dp"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/text"
android:textSize="20sp"
android:text="Comments" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/dividers"
android:id="#+id/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:layout_width="244dp"
android:layout_height="wrap_content"
android:id="#+id/editComment"
android:layout_below="#+id/divider"
android:textColor="#color/text"
android:hint="Write a comment..."/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create"
android:id="#+id/btnComment"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_marginTop="20dp"
android:layout_below="#+id/writeComment"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:id="#+id/commentsCard">
<LinearLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="0dp"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#color/text"
android:textSize="20sp"
android:text="Comments" />
<android.support.v7.widget.RecyclerView
android:id="#+id/commentsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
The second and first comment are being cut off.
So what my problem was that for some reason, Recyclerview didnt wrap_contents. I did some research (thanks stackoverflow) and found out a lot of people were having this issue and they posted solutions for this issue.
Basically, I had to use a customized linearlayoutmanger to fix the issue.
I will post the solution they posted and links to their questions. thanks for whoever tried to help, I appreciate it.
This is the extra file I needed. And then I had to set my recyclerview to use this layout instead of the default one.
public class MyLinearLayoutManager extends LinearLayoutManager {
public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
#Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
}
After that. Change this:
mProductsRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
To this:
mRecyclerView.setLayoutManager(new MyLinearLayoutManager(getApplicationContext(),1,false));
Three new paramets(Context, int Orientation, boolean reverse);
basically, i put 1 for orientation so it shows vertiacally, and false for revers so it shows up how it is ordered in my List.
Links to other people's question with same problem as me.
Nested Recycler view height doesn't wrap its content
Nested Recycler view height doesn't wrap its content
Thanks once again guys. Hope this helps someone else
Add this code in your activity, will set the height of your recycler view to the 90% of user's screen window.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int a = (displaymetrics.heightPixels*90)/100;
recylcerView.getLayoutParams().height =a;
and your comment layout below the your recyclerView
like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mudit"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:scrollbars="vertical" />
</LinearLayout>
<RelativeLayout
android:layout_below="#+id/mudit"
android:id="#+id/rl_commentWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:ems="10" />
<Button
android:id="#+id/plusButton"
style="android:buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Send"
android:textColor="#FFFFFF"
android:translationZ="5dp" />
</RelativeLayout>
</RelativeLayout>
In my situation I had a DialogFragment, in which there are 2 recyclers and 2 horizontal linear layouts with 2 buttons.
Dialog from top to bottom looks like this:
RecyclerView A
Horizontal linear layout X with 2 buttons
RecyclerView B
Horizontal linear layout Y with 2 buttons
On top of that both recyclers size is dynamic.
Recycler A size can change based on user swipes in both recyclers.
Recycler B size can change based on its item click listener.
What I wanted to achieve was to cut the Dialog from top to bottom
and have each of those four views its constant part of the Dialog height.
What worked was to divide it first to 2 separate relative layouts, say lTOP and lBottom. Set lTOP alignParentTop, lBottom alignParentBottom and layout_below lTOP.
Both of them width match_parent, height wrap_content.
Then in each of them create 1 relative (say lINTop) and 1 linear (say lINBottom) layouts. lINTop has recycler, lINBottom has 2 buttons.
Set both lINTop lINBottom width match_parent, height wrap_content.
Set lINTop alignParentTop, lINBottom alignParentBottom and below lINTop.
Set lINTop orientation vertical, lINBottom orientation horizontal.
Set recyclers width match_parent, height wrap_content.
Then i putted this all inside single relative layout with width/height wrap_content and orientation vertical.
Then this inside scrollview with width/height match_parent and orientation vertical.
Then needed a little code magic for setting up recycler in onCreateView:
For Recycler A:
private void setupRecyclerA() {
// use a linear layout manager
RecyclerView.LayoutManager layManUS = new LinearLayoutManager(setupActivity().get());
recyclerA.setLayoutManager(layManUS);
recyclerA.addItemDecoration(new DividerItemDecoration(
setupActivity().get(), LinearLayoutManager.VERTICAL));
recyclerA.setNestedScrollingEnabled(true);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerA.setHasFixedSize(true);
}
For Recycler B:
private void setupRecyclerB() {
// use a linear layout manager
RecyclerView.LayoutManager layManUS = new LinearLayoutManager(setupActivity().get());
recyclerB.setLayoutManager(layManUS);
recyclerB.addItemDecoration(new DividerItemDecoration(
setupActivity().get(), LinearLayoutManager.VERTICAL));
recyclerB.setNestedScrollingEnabled(true);
// set fixed height
DisplayMetrics displaymetrics = new DisplayMetrics();
setupActivity().get().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
// set height to 30 percent of dialog
int a = (displaymetrics.heightPixels*30)/100;
recyclerB.getLayoutParams().height =a;
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerB.setHasFixedSize(true);
}
I didnt invented it, I just combined solutions from many different Stack Overflow questions which I dont even remember now, sorry and thanks to the Creators.
I had a similar problem and solved it with RelativeLayout. I have this layout:
I wanted the top and bottom cardview to stay in place while scrolling the central recyclerView. Tried with LinearLayout and ConstraintLayout but didn't work, the bottom CardView only was shown when I scrolled to the end of the recycler.
This is my code, notice the layout_above, layout_below, and layout_alignParentBottom attributes:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grisMasOscuro">
<androidx.cardview.widget.CardView
android:id="#+id/cv1"
android:layout_width="match_parent"
android:layout_height="50dp" >
<!-- cardview content -->
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/cv3"
android:layout_below="#id/cv1">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cv3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true">
<!-- cardview content -->
</androidx.cardview.widget.CardView>
</RelativeLayout>
I found another solution.
If you will wrap recyclerview in RelativeLayout, it will solve the problem easily.
Thanks.
How to implament "Parallax Animations" http://imgur.com/ah4l5oj.gif
if my scollable view (listview and scrollview) are placed inside fragments in viewpager?
MainLayout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/header_holder"
android:layout_width="match_parent"
android:layout_height="#dimen/video_holder_size"
android:background="#color/dark_gray">
<ImageView
android:id="#+id/im_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/tv_artist_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dip"
android:layout_marginLeft="16dip"
android:fontFamily="sans-sarif"
android:text="#string/artist_bio"
android:textColor="#color/white"
android:textSize="25sp" />
</RelativeLayout>
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
xmlns:slider="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/controll_size"
slider:pstsIndicatorHeight="0dip"
slider:pstsPaddingMiddle="true"
slider:pstsTextAllCaps="true"
slider:pstsTextColorSelected="#color/white" />
<android.support.v4.view.ViewPager
android:id="#+id/video_body_pager"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:animationCache="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#drawable/shadow_concierge_list_upper"
android:gravity="left|center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="#+id/ibtn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:background="#drawable/ibtn_transparent"
android:padding="8dip"
android:src="#drawable/ic_action_back" />
</LinearLayout>
I tried to change view size by setting LayoutParams according to scroll delta, by performance is very poor
relatedHeight -= dY;// dY is a scroll delta
Log.e(getClass().getSimpleName(),"height "+relatedHeight+", dY "+dY);
if(relatedHeight < 0){
relatedHeight = 0;
} else if(relatedHeight > originalHeight){
relatedHeight = originalHeight;
} else{
LinearLayout.LayoutParams params =
(LinearLayout.LayoutParams) headerHolder.getLayoutParams();
params.height = (int) relatedHeight;
headerHolder.setLayoutParams(params);
}
I solved it using observableListview , otto bus and ViewHelper.
In pager fragment, I attach list.setScrollViewCallbacks(this); to my list.
In callback I pass offset to Bus
#Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging){
BusProvider.getInstance().post(new ScrollOffset(0, scrollY));
}
#Override
public void onDownMotionEvent(){
}
#Override
public void onUpOrCancelMotionEvent(ScrollState scrollState){
}
In main fragment, I use viewhelper to translate header and viewpager
#Subscribe
public void moveHeader(ScrollOffset event){
ViewHelper.setTranslationY(headerHolder, -event.getY() / 2);
ViewHelper.setTranslationY(pager,headerHolder.getHeigh() -event.getY()/2);
int baseColor = getResources().getColor(R.color.dark_gray);
float alpha = Math.min(1, (float) event.getY()*10 / (originalHeight ));
appBarHolder.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor));
}