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

时间:2016-04-01 06:08:56

标签: android android-recyclerview android-cardview

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

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

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

<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类是:

 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很新,所以任何指南都会受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

试试这个。

如果您的图片URI为空。

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

希望这会对您有所帮助。

答案 1 :(得分:1)

为此检查两件事,

首先检查 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;
    }
}