删除回收站视图中的 cardview 项目(如果它为 null)

Removing a cardview item inside a recycler view if it is null

我有一个回收站视图,我在其中填充了一个由图像和两个文本字段组成的卡片视图。它工作正常。我想知道的是有一些方法可以从 cardview 中删除图像,如果它为 null(并且不显示图像的空白 space)并且只在回收器视图中显示该特定项目的文本字段。

我正在解析包含图像和文本的 JSON 服务。但是在解析数据时有些图像丢失了,我想在显示数据时删除该图像 space。

下面是我的 Cardview.xml 我正在使用内部 Recyclerview 适配器。

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@android:color/black"
        android:clickable="true"
        android:contextClickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:longClickable="true"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:background="@android:color/black"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:textIsSelectable="true"
        android:textSize="30dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_below="@+id/heading"
        android:background="#CCC" />

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view"
        android:background="#000000"
        android:clickable="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
        android:textIsSelectable="true"

        />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_below="@+id/date"
        android:background="#CCC" />

    <TextView
        android:id="@+id/brief"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view1"
        android:background="#000000"
        android:clickable="true"
        android:contextClickable="true"
        android:elegantTextHeight="true"
        android:hyphenationFrequency="full"
        android:linksClickable="true"
        android:text="Large Text"
        android:textAlignment="viewStart"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"
        android:textColorHighlight="#ffffff"
        android:textStyle="italic" />

    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_below="@+id/brief"
        android:background="#CCC" />


</RelativeLayout>
</android.support.v7.widget.CardView>

RecyclerViewAdapter class 是:

 private LayoutInflater inflater;
 Context context;
 List<Data> dataArray;
 private int lastPosition = -1;

public RecyclerViewAdapter(Context context) {
    //this.dataArray = dataArray;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

public void setDataArray(List<Data> dataArray) {
    this.dataArray = dataArray;

}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);

    View view = inflater.inflate(R.layout.cardview, parent, false);
    CustomViewHolder holder = new CustomViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    Data current = dataArray.get(position);
    holder.textView1.setText(current.heading);
    holder.textView2.setText(current.date);
    holder.textView3.setText(current.brief);

    // Using picasso to fetch image as the user scrolls down ... No need to store
    // all the images during start up.

    Uri uri = Uri.parse(current.getLImage());
        //System.out.println("URIiii issss::::"+uri);
        Picasso.with(context).load(uri).into(holder.image);
        // Animation
        setAnimation(holder.relativeLayout, position);

}


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


public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView image;
    RelativeLayout relativeLayout;
    TextView textView1, textView2, textView3;

    public CustomViewHolder(View itemView) {
        super(itemView);
        textView1 = (TextView) itemView.findViewById(R.id.heading);
        textView2 = (TextView) itemView.findViewById(R.id.date);
        textView3 = (TextView) itemView.findViewById(R.id.brief);
        image = (ImageView) itemView.findViewById(R.id.imageView);
    }

    @Override
    public void onClick(View v) {

    }
}

private void setAnimation(View viewToAnimate, int position) {
    // If the bound view wasn't previously displayed on screen, it's animated

    if (position > lastPosition || position < lastPosition) {

        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }

}
}

我真正想要的是删除 carview 中的 imageview 项目(如果它为 null)。我对 android 比较陌生,因此非常感谢您提供指导。

试试这个。

如果您的图片 URI 为空。

if(uri == null){
    Picasso.with(this.context).cancelRequest(holder.image);
    holder.image.setVisibility(View.GONE);
}

希望对您有所帮助。

为此检查两件事,

首先检查您的 limage 是否为空,如果为空则隐藏您的图像视图。

另一件事是检查图像是否已经存在于您的服务器上 url。如果服务器上不存在,请隐藏您的图像视图。

你可以试试下面的代码,

if(current.getLImage() == null || !imageExists(**yourImageUrl**)){
    Picasso.with(this.context).cancelRequest(holder.image);
    holder.image.setVisibility(View.GONE);
}


public static boolean imageExists(String URLName){
    HttpClient client= new DefaultHttpClient();
    HttpHead headMethod = new HttpHead(urlToImage);
    HttpResponse response = client.execute(headMethod);
    if(response.getStatusLine().getStatusCode== HttpStatus.SC_NOT_FOUND) {
        return false;
    } else {
        return true;
    }
}