I have an application that is basically a video player, in the gallery it happens that when I change the orientation of the screen or return of the player to the fragment the bottom of the RecyclerView gets "polluted":
Asyoucansee,it'sasiftheviewsgetstuckinthebackground
Album:
publicclassAlbumGalleryextendsFragmentimplementsGalleryListener{publicstaticfinalStringTAG="ALBUM_GALLERY_FRAGMENT";
private static HashMap<String, GalleryItem> mFolders;
private RecyclerView rvAlbumGallery;
private AlbumGalleryAdapter mAdapter;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(mFolders == null){
mFolders = new HashMap<>();
}
listFolders();
rvAlbumGallery.setHasFixedSize(true);
rvAlbumGallery.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rvAlbumGallery.setAdapter(new AlbumGalleryAdapter(getActivity(), this, mFolders));
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View layout = inflater.inflate(R.layout.fragment_album_gallery, container, false);
rvAlbumGallery = (RecyclerView) layout.findViewById(R.id.rvAlbumGallery);
return layout;
}
...
}
Adapter :
public class AlbumGalleryAdapter extends RecyclerView.Adapter<AlbumGalleryAdapter.GalleryHolder> {
private FragmentActivity mActivity;
private GalleryListener mListener;
private ArrayList<GalleryItem> mItems;
private static int listThumbSize = 0;
public AlbumGalleryAdapter(@NonNull FragmentActivity activity, @NonNull GalleryListener listener, @NonNull HashMap<String, GalleryItem> items){
this.mActivity = activity;
this.mListener = listener;
this.mItems = new ArrayList<>(items.values());
if(listThumbSize == 0)
listThumbSize = (int)(activity.getResources().getDimension(R.dimen.rowVideoThumbnailSize) / activity.getResources().getDisplayMetrics().density);
}
@NonNull
@Override
public GalleryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new GalleryHolder(inflater.inflate(R.layout.album_gallery_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull GalleryHolder holder, int position) {
GalleryItem item = mItems.get(position);
holder.listener = mListener;
DisplayMetrics dm = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
holder.thumbnail.getLayoutParams().height = dm.heightPixels / 4;
if (item.isFolder) {
holder.isFolder = true;
holder.name.setText(item.getFolderName());
for (int i = 0; i < mItems.size(); i++) {
if (mItems.get(i).containsFolder(item.getFolderName())) {
RequestOptions options = new RequestOptions().override(holder.thumbnail.getWidth(), dm.heightPixels / 4).centerCrop();
Glide.with(mActivity).load(mItems.get(i).getFile()).apply(options).into(holder.thumbnail);
break;
}
}
} else {
RequestOptions options = new RequestOptions().override(holder.thumbnail.getWidth(), dm.heightPixels / 3).centerCrop();
Glide.with(mActivity).load(item.getFile()).apply(options).into(holder.thumbnail);
holder.name.setText(item.getName());
}
}
@Override
public int getItemCount() {
return mItems.size();
}
public void clean(){
final int size = mItems.size();
mItems.clear();
notifyItemRangeRemoved(0, size);
notifyDataSetChanged();
}
public void addItem(GalleryItem item, int pos){
}
static class GalleryHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
boolean isFolder = false;
TextView name;
ImageView thumbnail;
GalleryListener listener;
GalleryHolder(@NonNull View v) {
super(v);
name = (TextView)v.findViewById(R.id.item_gallery_movies_Name);
thumbnail = (ImageView)v.findViewById(R.id.item_gallery_movies_Thumb);
final ImageButton btnPopup = (ImageButton)v.findViewById(R.id.rowBtnPopup);
btnPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemContextClick(btnPopup, getAdapterPosition());
}
});
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onItemClick(getAdapterPosition(), isFolder);
}
}
}
Representation to item :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="@dimen/card_elevation"
app:cardUseCompatPadding="false"
app:cardPreventCornerOverlap="true"
app:cardCornerRadius="@dimen/card_corner_radius"
android:layout_margin="4dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_margin="8dp"
android:id="@+id/item_gallery_movies_Thumb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_gallery_movies_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_weight="1"
android:maxLines="1"
android:textSize="14sp"
android:padding="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp" />
<ImageButton
android:id="@+id/rowBtnPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@android:color/transparent"
app:srcCompat="@drawable/more_vert_btn"
android:layout_weight="0"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Gallery layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvAlbumGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>