0

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:

This is how it looks now:

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.

alex
  • 5,516
  • 2
  • 36
  • 60
Carel Kat
  • 131
  • 1
  • 13

2 Answers2

0
@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());
    holder.appName.setGravity(Gravity.CENTER);
    holder.moreInfo.setGravity(Gravity.CENTER); 

}}
0

first things first, thanks for the responses, but none of them seems to work. From what I found is, you have to create a RecyclerView with two viewTypes.

See the questions here: How to create RecyclerView with multiple view type?

And also here: Recyclerview and handling different type of row inflation

But what I did in the end is to make an Image with the text on. I know its a shortcut, and I might run into some other trouble in the long run, but for the time its working, and I just realized today that there can be more than one way of doing a thing. Yes maybe not the best way, but its working.

The biggest reason why I like it is because I find it much easier to handle the multiple lines of text on a image, than in XML or in the Arraylist with \n.

Carel Kat
  • 131
  • 1
  • 13