Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView adapter show only first item

I know that this question has been asked many times, but the answers doesn't work for me. I am trying to use Horizontal placement. Here is my code:

public class CalendarDaysAdapter extends RecyclerView.Adapter<CalendarDaysAdapter.MyViewHolder> {

    private ArrayList<String> list;

    public CalendarDaysAdapter(ArrayList<String> list)
    {
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_calendar_days_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        String day = list.get(position);
        holder.daystxt.setText(day);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView daystxt;

        public MyViewHolder(View view) {
            super(view);
            daystxt = (TextView) view.findViewById(R.id.daystxt);
        }
    }

}

fragment_calendar_days_item xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center">

        <TextView
            android:id="@+id/daystxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MON"
            android:textSize="20sp"/>

    </RelativeLayout>


</LinearLayout>

I tried every combination of wrap_content - match_parent, also on the RecyclerView xml in parent Activity, but only the first item is showing. I have used breakpoints to see if the data of the list are there, and they are.

So what I am missing ?

like image 841
Student Avatar asked Dec 02 '22 11:12

Student


2 Answers

Change your fragment_calendar_days_item height and width to android:layout_height="wrap_content" and android:layout_width="wrap_content"

it will solve your issue

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center">

        <TextView
            android:id="@+id/daystxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MON"
            android:textSize="20sp"/>

    </RelativeLayout>


</LinearLayout>
like image 144
Goku Avatar answered Dec 04 '22 12:12

Goku


Try this for your

fragment_calendar_days_item xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center">

        <TextView
            android:id="@+id/daystxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MON"
            android:textSize="20sp"/>
    </RelativeLayout>
</LinearLayout>
like image 27
Samir Bhatt Avatar answered Dec 04 '22 14:12

Samir Bhatt