[英]Hide RecyclerView item when image path is empty?
我的RecyclerView
顯示了一個徽標列表,如果它的路徑為空,我想隱藏一個項目。 我嘗試使用vh.itemView.setVisibility(View.GONE);
不工作,如果高度為 0,我嘗試應用 layoutparam,也沒有工作。
我無法過濾掉列表以僅包含非空路徑,因為我稍后會收到項目 position 問題
@Override
public void onBindViewHolder( StudioAdapter.StudioViewHolder vh, int position) {
final String path = StudioLogoPaths.get(position);
if (StudioLogoPaths.get(position).isEmpty()) {
vh.itemView.setVisibility(View.GONE);
} else {
Glide.with(vh.itemView.getContext()).load(path).into(vh.logoImage);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="55dp"
android:layout_height="55dp"
android:foreground="@drawable/transparent_ripple">
<ImageView
android:id="@+id/studio_logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:scaleType="fitCenter">
</ImageView>
</FrameLayout>
這是對我有用的解決方案
@Override
public void onBindViewHolder( StudioAdapter.StudioViewHolder vh, int position) {
final String path = StudioLogoPaths.get(position);
File file = new File(path);
if (file.exists()){
Glide.with(vh.itemView.getContext()).load(path).into(vh.logoImage);
} else {
ViewGroup.LayoutParams params = vh.itemView.getLayoutParams();
params.height = 0;
params.width = 0;
vh.itemView.setLayoutParams(params);
vh.itemView.setVisibility(View.GONE);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.