Recycler ver mostrando un solo elemento


Me enfrento a un extraño error en el que recyclerview muestra solo un elemento. A continuación se muestra el código para mi adaptador recyclerview:

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


List<chat> chats;
String username;
final int My=0,Their=1;

public ChatAdapter(List<chat> chats) {
    this.chats = chats;
    this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case My:
            View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
            viewHolder = new MyChatHolder(v1);
            break;
        case Their:
            View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
            viewHolder = new TheirMessageHolder(v2);
            break;
        default:
            View v = inflater.inflate(R.layout.my_chat_child, parent, false);
            viewHolder = new MyChatHolder(v);
            break;
    }

    return viewHolder;

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case My:
            MyChatHolder myChatHolder = (MyChatHolder) holder;
            myChatHolder.setMyMessage(chats.get(position).getMessage());
            break;

        case Their:
            TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
            theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
            break;
    }
}

@Override
public int getItemViewType(int position) {
    if(username.equalsIgnoreCase(chats.get(position).getFrom()))
        return My;
    return Their;
}

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

Ya he utilizado este código en otra aplicación y funciona perfectamente. He comprobado los datos de chats que también es perfecto.

Aquí está el enlace a los archivos de diseño de repositorio de git: archivos de diseño

Author: Rujul1993, 2016-04-12

3 answers

No use match_parent para altura para la vista de elementos. Un elemento llena toda la pantalla verticalmente para que no veas otro.

 294
Author: Slavik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-12 15:49:48

Cuando está creando una fila.xml para recyclerview debe seguir estas cosas:

  1. Siempre use " Wrap_content "para la altura de la fila, de lo contrario en" match_parent " ocupará toda la pantalla para una sola fila.

  2. También puede tomar la altura en dp.

 9
Author: Abhishek Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-13 13:31:00

1) si su recyclerview es vertical, establezca la altura de recyclerview match_parent y row_item.xml también la altura match_parent

2) si su recyclerview es horizontal, establezca el ancho de recyclerview match_parent y row_item.xml El ancho también match_parent

Para ex:- Horizontal RecyclerView

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp" />

Row_item.xml

<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="25sp" />
 0
Author: Gautam Surani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-22 06:54:42