Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...