Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView changes the height when scrolled how to solve this?

I'm using a recyclerview in order to show some photos of products, when I open the app it works amazing but when I scroll, the height of each card gets a lot bigger and then when I open the keyboard the height gets it's best size and then when I scroll, it gets this bad size again, I can't figure out how to fix that.

xml

<android.support.v7.widget.RecyclerView
   android:id="@+id/recycler_view"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:clipToPadding="false"
   android:scrollbars="vertical" />

Adapter

public  class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ViewHolder>{

private Context mContext;
private List<Product> productList;

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.product_card, parent, false);

    return new ProductsAdapter.ViewHolder(itemView);

}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    Product album = productList.get(position);
    holder.title.setText(album.getName());
    holder.price.setText(album.getPrice() + " DZA");

    // loading album cover using Glide library
    Glide.with(mContext).load(album.getThumbnail()).into(holder.thumbnail);

    holder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopupMenu(holder.overflow);
        }
    });
}

@Override
public int getItemCount() {
    return productList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView title, price;
    public ImageView thumbnail, overflow;

    public ViewHolder (View itemView){
        super (itemView);
        title = (TextView) itemView.findViewById(R.id.title);
        price = (TextView) itemView.findViewById(R.id.count);
        thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
        overflow = (ImageView) itemView.findViewById(R.id.overflow);
    }
}

public ProductsAdapter(Context mContext, List<Product> productList) {
    this.mContext = mContext;
    this.productList = productList;
}
private void showPopupMenu(View view) {
    // inflate menu
    PopupMenu popup = new PopupMenu(mContext, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_product_card, popup.getMenu());
    popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
    popup.show();
}
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

    public MyMenuItemClickListener() {
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_add_favourite:
                Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_play_next:
                Toast.makeText(mContext, "Play next", Toast.LENGTH_SHORT).show();
                return true;
            default:
        }
        return false;
    }
}
}

product_card.xml `

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardMaxElevation="8dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="@dimen/album_cover_height"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:paddingTop="@dimen/album_title_padding"
                android:textColor="@color/album_title"
                android:textSize="@dimen/album_title" />

            <TextView
                android:id="@+id/count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:paddingBottom="@dimen/songs_count_padding_bottom"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:textSize="@dimen/songs_count" />

            <ImageView
                android:id="@+id/overflow"
                android:layout_width="@dimen/ic_album_overflow_width"
                android:layout_height="@dimen/ic_album_overflow_height"
                android:layout_alignParentRight="true"
                android:layout_below="@id/thumbnail"
                android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
               android:scaleType="centerCrop"
                android:src="@drawable/ic_dots" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>
like image 669
Valentina_Siii Avatar asked Mar 17 '17 17:03

Valentina_Siii


People also ask

How do you solve the performance problem in a RecyclerView?

Use the setHasFixedsize method If the height of our RecyclerView items is fixed then we should use the setHasFixedsize method in our XML of our card item. This will fix the height of our RecyclerView item and prevent it from increasing or decreasing the size of our Card Layout.

Can scroll vertically RecyclerView?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .

Does RecyclerView automatically scroll?

There is a method in RecyclerView which is smoothScrollToPosition() which take position of recyclerview to scroll that position to current position. So I use a thread to increment the position automatically so an autoscroll is happening because position is incremented automatically.


1 Answers

I fixed the problem, it was all about wrap_content and match_parent I made a very stupid mistake sorry everyone. I changed match_parent to wrap_content in cardview and this works perfectly now. Thanks guys.

like image 63
Valentina_Siii Avatar answered Sep 18 '22 13:09

Valentina_Siii