RecyclerView适配器仅显示第一项

RecyclerView适配器仅显示第一项

本文介绍了RecyclerView适配器仅显示第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多遍了,但是答案对我来说不起作用.我正在尝试使用水平放置.这是我的代码:

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 :

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>

我也在父Activity的RecyclerView xml上尝试了wrap_content-match_parent的每种组合,但只显示了第一项.我已经使用断点来查看列表中的数据是否存在.

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.

那我想念的是什么?

推荐答案

将您的 fragment_calendar_days_item 高度和宽度更改为 android:layout_height="wrap_content" android:layout_width="wrap_content"

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

它将解决您的问题

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

这篇关于RecyclerView适配器仅显示第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:01