I have a RecyclerView
with a Cardview
. I got it to work if there is no image in the ArrayList
, but it still puts the text at the bottom.
Like this:
I am wondering if its possible (and if - how please) to get the text of this CardView
in the middle if there is no image. Maybe with Java
or in the XML
file.
Here is my XML
file:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:contentDescription="@null"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/image"
android:padding="5dp"
android:gravity="center"
android:text="@string/app_name"
android:textStyle="bold" />
<TextView
android:id="@+id/more_text5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/app_name"
android:padding="5dp"
android:gravity="center"
android:text="@string/app_name"
android:textStyle="bold" />
</RelativeLayout>
And here is the adapter:
public class SnapRecyclerAdapter extends RecyclerView.Adapter<SnapRecyclerAdapter.ReyclerViewHolder> {
private LayoutInflater layoutInflater;
private Context context;
private ArrayList<Item> items;
public SnapRecyclerAdapter(Context context, ArrayList<Item> items) {
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
this.items = items;
}
@Override
public ReyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = layoutInflater.inflate(R.layout.item_recycler_view, parent, false);
return new ReyclerViewHolder(item);
}
@Override
public void onBindViewHolder(final ReyclerViewHolder holder, int position) {
Item item = items.get(position);
if(holder.image == null){
holder.image.setVisibility(View.GONE);}
else {
holder.image.setImageResource(item.getDrawable());
holder.appName.setText(item.getName());
holder.moreInfo.setText(item.getMore_info());}}
@Override
public int getItemCount() {
return items.size();
}
class ReyclerViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView appName;
private TextView moreInfo;
private ReyclerViewHolder(final View v) {
super(v);
image = (ImageView) v.findViewById(R.id.image);
appName = (TextView) v.findViewById(R.id.app_name);
moreInfo = (TextView) v.findViewById(R.id.more_text5);
}
}
}
Needless to say that as always help (and patience with our new guys lol) is really appreciated.