2016-04-01 7 views
0

Ich habe eine Recycleransicht, in die ich ein cardview bestücke, das aus einem Bild und zwei Textfeldern besteht. Es funktioniert gut. Was ich wissen möchte, ist, dass es eine Möglichkeit gibt, ein Bild aus cardview zu entfernen, wenn es null ist (und nicht den leeren Platz für das Bild anzeigt) und nur die Textfelder für dieses bestimmte Objekt in der Recycler-Ansicht anzuzeigen.Entfernen eines Kartenansichtsobjekts in einer Recycleransicht, wenn es null ist

Ich analysiere einen JSON-Dienst mit Bildern und Text. Aber einige der Bilder fehlen beim Analysieren von Daten, und ich möchte diesen Bildbereich entfernen, während Daten angezeigt werden.

Unten ist meine Cardview.xml ich benutze in Recyclerview Adapter.

<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> 

Die RecyclerViewAdapter Klasse:

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; 
    } 

} 
} 

Was ich will, ist wirklich das Imageview Element in Carview zu entfernen, wenn es null ist. Ich bin relativ neu in Android, so dass jede Richtlinie sehr geschätzt werden würde.

+0

können Sie Ihre Adapter Quelle laden? – myoungjin

+0

Es ist jetzt hinzugefügt! – Umair

+2

Setzen Sie die Sichtbarkeit von imageview auf GONE, wenn URL null ist –

Antwort

1

Scheck zwei Dinge dafür,

zuerst überprüfen Sie Ihre limage null ist oder nicht, wenn es als verstecken Sie Ihre Bildansicht null ist.

eine andere Sache ist Scheck Bild ist bereits auf Ihrem Server URL vorhanden. Wenn nicht auf dem Server existiert verstecken Sie Ihre Bildansicht.

Sie unter Code für die versuchen,

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; 
    } 
} 
+0

entfernt alle Bilder, auch wenn sie existieren. – Umair

+0

versuche mein aktuelles ans. @Umair – Tejas

+1

versuchen Sie es in OnBindViewHolder-Methode oder nicht? und überprüfen Sie Ihre URL mit comlpete Bild Pfad nicht Uri .. @Umair – Tejas

2

Versuchen Sie es mit diesem.

Wenn Ihre Image-URI null ist.

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

Hoffe das hilft Ihnen.

+0

Ich sehe, was Sie hier getan haben, aber es hat nicht funktioniert. Immer noch wird die Bildansicht als leer mit dem Hintergrund angezeigt, den ich dafür eingestellt habe. Versuchte das View.Invisible, aber immer noch das gleiche Ergebnis. – Umair

+0

@Umair Kann Ihr leeres Bild URL Hintergrund Teil sein. zeige irgendeine deiner leeren Bild URL an. –

+0

@Umair Entfernen Sie diesen Teil 'android: background =" @ android: color/black "' aus Ihrer XML 'Image View'. –