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;
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.
My Fragment is taking so much time in update layout It is because of a lot of code in fragment how to resolve this please help me
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/lyttabs"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:gravity="center"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.bugfree.caviar.fonts.BabesNeueProBold
android:textStyle="bold"
android:layout_weight="1"
android:textColor="#color/black_text"
android:textSize="30dp"
android:layout_width="0dp"
android:text="My Events That I am..."
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/btn_plus"
android:src="#drawable/plus"
android:layout_width="20dp"
android:layout_height="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
app:tabIndicatorColor="#color/button_red_back"
android:id="#+id/tabs"
android:layout_marginTop="15dp"
app:tabBackground="#drawable/tab_indicator_color"
app:tabTextAppearance="#style/MyCustomTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager.widget.ViewPager
android:layout_marginTop="30dp"
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/lytCreateEvent"
android:visibility="gone"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.CreateEvent">
<ImageView
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="30dp"
android:id="#+id/imgBack"
android:src="#drawable/back_black"
android:layout_width="20dp"
android:layout_height="20dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<com.bugfree.caviar.fonts.BabesNeueProBold
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create event"
android:textColor="#color/black"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Event title" />
<EditText
android:singleLine="true"
android:paddingStart="15dp"
android:layout_marginTop="10dp"
android:textColorHint="#color/black"
android:background="#drawable/edit_back"
android:id="#+id/edtEventTitle"
android:textSize="18dp"
android:layout_gravity="center"
android:hint="Beach Yoga!"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Event type" />
</LinearLayout>
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/edit_back"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_centerVertical="true"
android:paddingStart="15dp"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:text="Business"
android:textSize="18dp" />
<ImageView
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="15dp"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_alignParentEnd="true"
android:src="#drawable/down_arrow" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event description" />
<com.bugfree.caviar.fonts.BabesNeueProBoldRegular
android:layout_marginEnd="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="94/300" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/custombutton_border_edittext"
android:orientation="vertical">
<EditText
android:textSize="18dp"
android:id="#+id/edtDescription"
android:textColorHint="#color/black_text_new"
android:padding="10dp"
android:layout_gravity="start"
android:gravity="start"
android:hint="Beach yoga with Alissia is a lifestyle focused on health,
wellness, and self-care through the practice of"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#android:color/transparent" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:tint="#color/black"
android:layout_alignParentEnd="true"
android:src="#drawable/down_arrowsmall"
android:layout_width="10dp"
android:layout_height="10dp" />
</RelativeLayout>
<EditText
android:paddingLeft="10dp"
android:layout_gravity="start"
android:gravity="start"
android:id="#+id/edtSplReq"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:layout_height="100dp"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/lay_without_border"
android:hint="Add a special request[optional]"
android:textColorHint="#848484"
android:textSize="18dp" />
<Button
android:outlineProvider="bounds"
android:stateListAnimator="#null"
style="?android:attr/borderlessButtonStyle"
android:layout_marginBottom="35dp"
android:id="#+id/btnSubmit"
android:layout_marginTop="30dp"
android:textSize="18dp"
android:textAllCaps="false"
android:text="Submit"
android:textColor="#color/space_white"
android:background="#drawable/custombutton"
android:layout_width="match_parent"
android:layout_height="35dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/lytTab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
app:tabBackground="#drawable/tab_indicator_color"
app:tabIndicatorColor="#color/button_red_back"
android:id="#+id/tabsLOc"
android:layout_marginTop="10dp"
app:tabTextAppearance="#style/MyCustomTextAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPagerLoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
and here is my fragment
public class EventFragment extends Fragment {
private View view;
ViewPagerAdapter viewPagerAdapter;
LocationPagerAdapter locationPagerAdapter;
private String current = "";
private String ddmmyyyy = "MMDDYYYY";
private Calendar cal = Calendar.getInstance();
FragmentEventBinding binding;
private OnFragmentInteractionListener mListener;
public EventFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_event, container, false);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Bebas Neue Pro Regular.otf");
binding.lytTopImg.setOnClickListener(v -> {
binding.lytLocDetails.setVisibility(View.VISIBLE);
binding.lytPartLocNew.setVisibility(View.GONE);
});
binding.imgTopEvent.setImageBitmap(SetBrightness(BitmapFactory.decodeResource(getResources(), R.drawable.dummy),-70));
binding.imgBackLocDetail.setOnClickListener(v -> {
binding.lytLocDetails.setVisibility(View.GONE);
binding.lytPartLocNew.setVisibility(View.VISIBLE);
/* Intent intent = new Intent(PartnerLocationActivity.this, LoactionDetails.class);
startActivity(intent);*/
});
binding.btnPlus.setOnClickListener(v -> {
binding.lyttabs.setVisibility(View.GONE);
binding.lytCreateEvent.setVisibility(View.VISIBLE);
});
binding.imgBack.setOnClickListener(v -> {
mListener.changeFragment(2);
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
});
binding.buttonCreateEvent.setOnClickListener(v -> {
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
});
binding.imgBackPartnerLoc.setOnClickListener(v -> {
binding.lytPartLocNew.setVisibility(View.GONE);
binding.lytCreateEvent.setVisibility(View.VISIBLE);
/* Intent intent = new Intent(getActivity(), CreateEvent.class);
startActivity(intent);*/
});
binding.lytTopPartLoc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
binding.lytLocDetails.setVisibility(View.VISIBLE);
binding.lytPartLocNew.setVisibility(View.GONE);
}
});
binding.buttonCreateEvent.setTypeface(font);
binding.edtEventTitle.setTypeface(font);
binding.edtDescription.setTypeface(font);
binding.edtNameLoc.setTypeface(font);
binding.edtAddressLoc.setTypeface(font);
binding.rdbOne.setTypeface(font);
binding.rdbGroup.setTypeface(font);
binding.buttonPartnerloc.setOnClickListener(v -> {
binding.lytPartLocNew.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
/* Intent intent = new Intent(CreateEvent.this, PartnerLocationActivity.class);
startActivity(intent);*/
});
binding.buttonCreateEvent.setOnClickListener(v -> {
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytCreateEvent.setVisibility(View.GONE);
});
binding.edtDob.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
String clean = s.toString().replaceAll("[^\\d.]|\\.", "");
String cleanC = current.replaceAll("[^\\d.]|\\.", "");
int cl = clean.length();
int sel = cl;
for (int i = 2; i <= cl && i < 6; i += 2) {
sel++;
}
if (clean.equals(cleanC)) sel--;
if (clean.length() < 8) {
clean = clean + ddmmyyyy.substring(clean.length());
} else {
int day = Integer.parseInt(clean.substring(0, 2));
int mon = Integer.parseInt(clean.substring(2, 4));
int year = Integer.parseInt(clean.substring(4, 8));
mon = mon < 1 ? 1 : mon > 12 ? 12 : mon;
cal.set(Calendar.MONTH, mon - 1);
year = (year < 1900) ? 1900 : (year > 2100) ? 2100 : year;
cal.set(Calendar.YEAR, year);
day = (day > cal.getActualMaximum(Calendar.DATE)) ? cal.getActualMaximum(Calendar.DATE) : day;
clean = String.format("%02d%02d%02d", day, mon, year);
}
clean = String.format("%s/%s/%s", clean.substring(0, 2),
clean.substring(2, 4),
clean.substring(4, 8));
sel = sel < 0 ? 0 : sel;
current = clean;
binding.edtDob.setText(current);
binding.edtDob.setSelection(sel < current.length() ? sel : current.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
}catch (Exception ignored){
}
}
}).start();
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
binding.viewPager.setAdapter(viewPagerAdapter);
binding.tabs.setupWithViewPager( binding.viewPager);
/*binding.imgBack.setOnClickListener(v -> {
binding.lytLocDetails.setVisibility(View.GONE);
binding.lytPartLocNew.setVisibility(View.VISIBLE);
});*/
locationPagerAdapter = new LocationPagerAdapter(getFragmentManager());
binding.viewPagerLoc.setAdapter(locationPagerAdapter);
binding.tabsLOc.setupWithViewPager(binding.viewPagerLoc);
binding.edtSplReq.setTypeface(font);
binding.edtEmail.setTypeface(font);
binding.edtPhone.setTypeface(font);
binding.edtLastName.setTypeface(font);
binding.edtFirstname.setTypeface(font);
binding.btnSubmit.setTypeface(font);
binding.btnFindTable.setTypeface(font);
binding.btnFindTable.setOnClickListener(v -> {
binding.lytBtnFind.setVisibility(View.GONE);
binding.lytTab.setVisibility(View.GONE);
binding.lytBookTable.setVisibility(View.VISIBLE);
});
binding.btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
binding.lyttabs.setVisibility(View.VISIBLE);
binding.lytLocDetails.setVisibility(View.GONE);
binding.lytPartLocNew.setVisibility(View.GONE);
binding.lytCreateEvent.setVisibility(View.GONE);
binding.lytBookTable.setVisibility(View.GONE);
binding.lytBtnFind.setVisibility(View.VISIBLE);
binding.lytTab.setVisibility(View.VISIBLE);
binding.viewPager.setCurrentItem(1);
binding.tabs.getSelectedTabPosition();
}
});
return view = binding.getRoot();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public Bitmap SetBrightness(Bitmap src, int value) {
// original image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// increase/decrease each channel
R += value;
if(R > 255) { R = 255; }
else if(R < 0) { R = 0; }
G += value;
if(G > 255) { G = 255; }
else if(G < 0) { G = 0; }
B += value;
if(B > 255) { B = 255; }
else if(B < 0) { B = 0; }
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
// return final image
return bmOut;
}
}
There are two parts to this answer.
As per https://developer.android.com/topic/performance/rendering/optimizing-view-hierarchies
Android Layouts allow you to nest UI objects in the view hierarchy. This nesting can also impose a layout cost.
Your layout has multiple levels of nesting, this can have a large cost to layout.
Simplifying you layout with a more flexible layout like a ConstraintLayout Might reduce the layout cost.
Sometimes you are just trying to do too much when creating a view, do some of the work in a background thread and update the view when done (optionally putting up a busy type of progress bar while doing it)
Some idea's based on your code shown.
Instead of scanning every pixel of an image with the SetBrightness method which could take some time depending on the size of the image.
Add the image to the layout without the Brightness adjusted, start background task adjust image Brightness and when done replace the original un-adjusted image with the adjusted image.
This could probably be done without a busy progress bar.
Delay the creation of the offscreen viewpager pages until they are really needed.
Instead of each Fragment in the viewpager creating it's view in onCreateView when the Viewpager is created, create a placeholder view in the Fragment's onCreateView and update it in the Fragment's onResume which is only called when the Fragment is shown on screen (note need a recent'ish version of Viewpager for this behaviour)
I have three RecyclerViews, and all three are lagging in scroll. I have tried many things suggested on the internet, but nothing made my RecyclerView faster.
I have tried the solutions given below,
RecyclerView laggy scrolling
Extremely Laggy RecyclerView Performance
Lags when RecyclerView scrolling
And some more. Some of them are not relevant. But still, it lags.
For the reference, below are the images for RecyclerView items.
The xml of the view (for the single chat bubble). It's almost same as WhatsApp.
<LinearLayout
android:id="#+id/transactionOut"
android:layout_width="#dimen/dp300"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
android:layout_gravity="end"
android:paddingStart="0dp"
android:paddingLeft="0dp"
android:paddingEnd="13dp"
android:paddingRight="13dp"
android:clickable="true"
android:background="#drawable/outgoing_chat_bubble">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:minWidth="#dimen/dp100"
android:orientation="horizontal"
android:elevation="#dimen/general_margin_or_padding_very_small"
android:padding="#dimen/dp10">
<ImageView
android:id="#+id/transactionImageOut"
android:layout_margin="#dimen/dp4"
android:layout_width="#dimen/size_transaction"
android:layout_height="#dimen/size_transaction"
android:padding="3dp"
android:visibility="gone"
android:src="#drawable/ic_transaction_indication_sent"
android:layout_gravity="center_vertical"
android:contentDescription="#string/payment_sent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:textSize="#dimen/general_for_all"
android:text="–"
android:textColor="#color/app_font_detail_color"/>
<style.views.CustomTextView
android:id="#+id/transactionAmountOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/progress_loading"
android:textColor="#color/app_font_detail_color"
android:layout_marginLeft="#dimen/dp4"
android:layout_marginStart="#dimen/dp4"
android:ellipsize="end"
android:maxLines="1"
android:textSize="#dimen/general_for_all"
appUI:font="#string/appNormalFontName"/>
</LinearLayout>
<View
android:id="#+id/separatorMessageOut"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:visibility="gone"
android:layout_marginStart="#dimen/general_margin_or_padding_very_small"
android:layout_marginEnd="#dimen/general_margin_or_padding_very_small"
android:layout_marginRight="#dimen/general_margin_or_padding_very_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_very_small"
android:background="#color/app_font_detail_color"/>
<LinearLayout
android:id="#+id/layoutMessageOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:visibility="gone"
android:paddingTop="#dimen/general_margin_or_padding_very_small"
android:paddingBottom="#dimen/general_margin_or_padding_very_small">
<ImageView
android:layout_width="#dimen/size_extra_icon"
android:layout_height="#dimen/size_extra_icon"
android:layout_marginTop="#dimen/icon_margin_top"
android:src="#drawable/ic_sms"
android:layout_marginStart="#dimen/general_margin_or_padding_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_small"
android:visibility="visible"
android:contentDescription="#string/design"/>
<TextView
android:id="#+id/textMessageOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/general_margin_or_padding_very_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_very_small"
android:textColor="#color/app_font_detail_color"
android:text="Text message"/>
</LinearLayout>
<View
android:id="#+id/separatorNoteOut"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:visibility="gone"
android:layout_marginStart="#dimen/general_margin_or_padding_very_small"
android:layout_marginEnd="#dimen/general_margin_or_padding_very_small"
android:layout_marginRight="#dimen/general_margin_or_padding_very_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_very_small"
android:background="#color/app_font_detail_color"/>
<LinearLayout
android:id="#+id/layoutNoteOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:visibility="gone"
android:paddingTop="#dimen/general_margin_or_padding_very_small"
android:paddingBottom="#dimen/general_margin_or_padding_very_small">
<ImageView
android:layout_width="#dimen/size_extra_icon"
android:layout_height="#dimen/size_extra_icon"
android:layout_marginTop="#dimen/icon_margin_top"
android:src="#drawable/ic_notes"
android:layout_marginStart="#dimen/general_margin_or_padding_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_small"
android:visibility="visible"
android:contentDescription="#string/design"/>
<TextView
android:id="#+id/textNoteOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/general_margin_or_padding_very_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_very_small"
android:textColor="#color/app_font_detail_color"
android:text="Text note"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/app_font_detail_color"
android:layout_marginStart="#dimen/general_margin_or_padding_very_small"
android:layout_marginEnd="#dimen/general_margin_or_padding_very_small"
android:layout_marginRight="#dimen/general_margin_or_padding_very_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_very_small"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="#dimen/dp6">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="start|center_vertical">
<style.views.CircleImageView
android:id="#+id/bankLogoOut"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:src="#drawable/placeholder_default"
appUI:circular_border_color="#color/profile_border_color"
appUI:circular_border_width="#dimen/dp1" />
<TextView
android:id="#+id/bankNameOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/general_margin_or_padding_very_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_very_small"
android:maxLines="1"
android:ellipsize="end"
android:text="#string/progress_loading"
android:textSize="#dimen/small_size" />
</LinearLayout>
<ImageView
android:id="#+id/showDetailImageOut"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dp4"
android:layout_marginEnd="#dimen/dp4"
android:src="#drawable/ic_transaction_more"
android:contentDescription="#string/more"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/messageStatusImageOut"
android:layout_width="#dimen/size_transaction"
android:layout_height="#dimen/size_transaction"
android:layout_centerVertical="true"
android:src="#drawable/ic_pending"
android:layout_marginStart="#dimen/general_margin_or_padding_small"
android:layout_marginLeft="#dimen/general_margin_or_padding_small"
android:visibility="visible"
android:contentDescription="#string/design"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
<style.views.CustomTextView
android:id="#+id/transactionDatetimeOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="end"
android:text="#string/account"
android:textColor="#color/app_font_detail_color"
android:textSize="#dimen/date_time_stamp"
android:layout_toLeftOf="#+id/messageStatusImageOut"
android:layout_toStartOf="#+id/messageStatusImageOut" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Here is it's adapter
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.setIsRecyclable(false);
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder transactionViewHolder = (HeaderViewHolder) holder;
String getTodayDate = (String) filteredList.get(position);
transactionViewHolder.setDate(DateUtility.getFormattedDate(getTodayDate));
} else if (holder instanceof MainViewHolder) {
MainViewHolder viewHolder = (MainViewHolder) holder;
try {
final History history = (History) filteredList.get(position);
viewHolder.setTransaction(history, mContext);
} catch (Exception e){
e.printStackTrace();
}
}
}
And the setTransaction method of ViewHolder
private void setTransaction(final History history, final Context context){
String myId = new Session(context).getStringForKey(Session.prIdKey);
if(history.getTo().equals(history.getFrom())) {
transactionSelf.setVisibility(View.VISIBLE);
transactionOut.setVisibility(View.GONE);
transactionIn.setVisibility(View.GONE);
transactionAmountSelf.setText(Utils.parseIntoPKR(history.getAmount()));
transactionDateTimeSelf.setText(DateUtility.getTimeFromEpoch(history.getEpoch()));
messageStatusImageSelf.setVisibility(View.VISIBLE);
switch (history.getStatus()) {
case Constants.STATUS_PENDING:
messageStatusImageSelf.setImageResource(R.drawable.ic_pending);
break;
case Constants.STATUS_FAILED:
messageStatusImageSelf.setImageResource(R.drawable.ic_transaction_status_blocked);
break;
default:
messageStatusImageSelf.setImageResource(R.drawable.ic_transaction_status_done);
break;
}
LinkedBank associatedBank = history.getSenderBank();
bankNameSelf.setText(associatedBank.fetchAbbreviationAndAccountNumber());
if(history.getReceiverNotes() != null && history.getReceiverNotes().length() > 0) {
separatorNoteSelf.setVisibility(View.VISIBLE);
layoutNoteSelf.setVisibility(View.VISIBLE);
textNoteSelf.setText(history.getReceiverNotes());
} else if(history.getSenderNotes() != null && history.getSenderNotes().length() > 0) {
separatorNoteSelf.setVisibility(View.VISIBLE);
layoutNoteSelf.setVisibility(View.VISIBLE);
textNoteSelf.setText(history.getSenderNotes());
}
transactionSelf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((OnIndividualTransactionSelection) context).onTransactionSelected(history);
}
});
} else if(history.getFrom().equals(myId)) {
//from my number -> cash out
transactionOut.setVisibility(View.VISIBLE);
transactionIn.setVisibility(View.GONE);
transactionSelf.setVisibility(View.GONE);
if(history.getMessage() != null && history.getMessage().length() > 0) {
separatorMessageOut.setVisibility(View.VISIBLE);
layoutMessageOut.setVisibility(View.VISIBLE);
textMessageOut.setText(history.getMessage());
}
if(history.getSenderNotes() != null && history.getSenderNotes().length() > 0) {
separatorNoteOut.setVisibility(View.VISIBLE);
layoutNoteOut.setVisibility(View.VISIBLE);
textNoteOut.setText(history.getSenderNotes());
}
messageStatusImageOut.setVisibility(View.VISIBLE);
switch (history.getStatus()) {
case Constants.STATUS_PENDING:
messageStatusImageOut.setImageResource(R.drawable.ic_pending);
break;
case Constants.STATUS_FAILED:
messageStatusImageOut.setImageResource(R.drawable.ic_transaction_status_blocked);
break;
default:
messageStatusImageOut.setImageResource(R.drawable.ic_transaction_status_done);
break;
}
transactionAmountOut.setText(Utils.parseIntoPKR(history.getAmount()));
transactionDatetimeOut.setText(DateUtility.getTimeFromEpoch(history.getEpoch()));
LinkedBank associatedBank = history.getSenderBank();
bankNameOut.setText(associatedBank.fetchAbbreviationAndAccountNumber());
Picasso.with(context).load(associatedBank.getBankLogo()).placeholder(R.drawable.placeholder_default).error(R.drawable.placeholder_default).into(bankLogoOut);
transactionOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((OnIndividualTransactionSelection) context).onTransactionSelected(history);
}
});
} else if(history.getTo().equals(myId)) {
transactionIn.setVisibility(View.VISIBLE);
transactionOut.setVisibility(View.GONE);
transactionSelf.setVisibility(View.GONE);
if(history.getMessage() != null && history.getMessage().length() > 0) {
separatorMessageIn.setVisibility(View.VISIBLE);
layoutMessageIn.setVisibility(View.VISIBLE);
textMessageIn.setText(history.getMessage());
}
if(history.getReceiverNotes() != null && history.getReceiverNotes().length() > 0) {
separatorNoteIn.setVisibility(View.VISIBLE);
layoutNoteIn.setVisibility(View.VISIBLE);
textNoteIn.setText(history.getReceiverNotes());
}
transactionAmountIn.setText(Utils.parseIntoPKR(history.getAmount()));
transactionDatetimeIn.setText(DateUtility.getTimeFromEpoch(history.getEpoch()));
LinkedBank associatedBank = history.getReceiverBank();
bankNameIn.setText(associatedBank.fetchAbbreviationAndAccountNumber());
Picasso.with(context).load(associatedBank.getBankLogo()).placeholder(R.drawable.placeholder_default).error(R.drawable.placeholder_default).into(bankLogoIn);
transactionIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((OnIndividualTransactionSelection) context).onTransactionSelected(history);
}
});
}
}
It's answer may help me to optimize the other two RecyclerViews as well. Any help is highly appreciated.
Two quick tips
1) You might want to cut down on a lots o processing while it's still scrolling. This will improve scrolling performance substantially
2) You might want to relook at this and similar 'new' later, allocating on every bind will slow down your scroll.
String myId = new Session(context).getStringForKey(Session.prIdKey);
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.
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));
}