RecyclerView изменяет высоту при прокрутке, как решить эту проблему?
Я использую обзор переработчика, чтобы показать некоторые фотографии продуктов, когда я открываю приложение, оно работает потрясающе, но когда я прокручиваю, высота каждой карты становится намного больше, а затем, когда я открываю клавиатуру, высота достигает своего наилучшего размера. и затем, когда я прокручиваю, он снова получает этот плохой размер, я не могу понять, как это исправить.
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" />
адаптер
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>
1 ответ
Решение
Я исправил проблему, все было связано с wrap_content и match_parent. Я сделал очень глупую ошибку, извините всех. Я изменил match_parent на wrap_content в cardview, и теперь это работает отлично. Спасибо, парни.