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.
können Sie Ihre Adapter Quelle laden? – myoungjin
Es ist jetzt hinzugefügt! – Umair
Setzen Sie die Sichtbarkeit von imageview auf GONE, wenn URL null ist –