RecyclerView 未填充,可能是一个愚蠢的适配器问题

安德鲁加

我使用一个工作RecyclerView作为另一个的模板,但我在它的实现中犯了一些错误,我找不到它。这是初始化视图的代码:

 mMessageThreadList = new ArrayList<>();       
 mRecyclerView = findViewById(R.id.chat_list_recycler_view);


 private void initRecyclerView() {
        Log.d(TAG, "initRecyclerView Called");

        //Initializes and sets up adapter

        mAdapter = new ChatListRecyclerViewAdapter(mThreadList);
        mRecyclerView.setAdapter(mAdapter);


        Query query = mThreadCollection;
        query.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
                    switch (documentChange.getType()) {
                        case ADDED:
                            Thread thread = documentChange.getDocument().toObject(Thread.class);
                            Log.d(TAG, "Last Message: " + thread.getLastMessage().getMessage());

                            mThreadList.add(thread);
                            mAdapter.notifyDataSetChanged();

                    }
                }
            }
        });
    }

请注意,打印时的打印日志语句SnapshotListener确实包含一个值,因此信息从 Firebase 返回,只是不显示。这让我觉得我的问题出在我的适配器/查看器上:

public class ChatListRecyclerViewAdapter extends RecyclerView.Adapter<ChatListRecyclerViewAdapter.ChatListViewHolder> {

    private List<Thread> threadList;

    ChatListRecyclerViewAdapter(List<Thread> threadList) {
        this.threadList = threadList;
    }


    @NonNull
    @Override
    public ChatListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list_row, parent, false);
        return new ChatListViewHolder(view);

    }

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

        // This method fills fields with data for each list item
        Log.d(TAG, "onBindViewholder called");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM hh:mm a");
        final Thread thread = threadList.get(position);
        char initial = thread.getPartnerName().charAt(0);
        char uppercaseInitial = Character.toUpperCase(initial);

        Log.d(TAG, thread.getLastMessage() + " " + thread.getPartnerID());


        holder.partnerName.setText(thread.getPartnerName());
        holder.authorInitial.setText(uppercaseInitial);
        holder.lastMessageText.setText(thread.getLastMessage().getMessage());
        holder.lastMessageTime.setText(simpleDateFormat.format(thread.getLastMessage().getTimestamp()));

    }

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

    //Viewholder stores the information about the layout and content of each list item, and serves as a template for each item of a RecyclerView


    public class ChatListViewHolder extends RecyclerView.ViewHolder {

        TextView partnerName;
        TextView authorInitial;
        TextView lastMessageText;
        TextView lastMessageTime;
        LinearLayout listTextHolder;

        public ChatListViewHolder(View itemView) {
            super(itemView);

            partnerName = itemView.findViewById(R.id.partner_name);
            authorInitial = itemView.findViewById(R.id.partner_initial);
            lastMessageText = itemView.findViewById(R.id.last_message);
            lastMessageTime = itemView.findViewById(R.id.last_message_time);
            listTextHolder = itemView.findViewById(R.id.list_text_holder);

        }
    }
}

chat_list_row.xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/single_thread_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:background="@color/colorPrimaryDark">

    <TextView
        android:id="@+id/partner_initial"
        android:background="@drawable/chat_list_circle"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentStart="true"
        android:gravity="center"
        android:text="A"
        android:textSize="36sp"
        tools:ignore="HardcodedText" />


    <TextView
        android:id="@+id/last_message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:padding="5dp"
        android:fontFamily="@font/nunito_extralight"
        android:text="@string/sample_time"
        android:textColor="@android:color/darker_gray"
        android:textSize="12sp"
        android:typeface="normal" />

    <LinearLayout
        android:id="@+id/list_text_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/partner_initial"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/partner_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginStart="5dp"
            android:fontFamily="@font/nunito"
            android:text="@string/sample_name"
            android:textColor="@color/white"
            android:textSize="14sp"
            android:textStyle="bold"
            android:typeface="sans" />

        <TextView
            android:id="@+id/last_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:fontFamily="@font/nunito_extralight"
            android:gravity="start"
            android:text="@string/sample_message"
            android:textColor="@android:color/darker_gray"
            android:textSize="12sp"
            android:typeface="normal" />

    </LinearLayout>

</RelativeLayout>

编辑:RecyclerView出现在其中的活动的布局。我还编辑了上面的代码以显示变量附加到视图的位置:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorPrimaryDark"
    android:paddingLeft="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="0dp"
    android:paddingBottom="0dp"
    tools:context="org.andrewedgar.theo.ChatListActivity">

    <include
        android:id="@+id/chat_list_toolbar"
        layout="@layout/toolbar" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/chat_list_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chat_list_toolbar" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/new_check_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_person_pin_circle_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />


</android.support.constraint.ConstraintLayout>
费萨尔·艾哈迈德

似乎您错过了添加 RecyclerView 的布局管理器。你能添加这个然后再试一次吗

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用多个RecyclerView一个适配器类

从另一个适配器刷新recyclerview适配器的值

在recyclerview中用两个阵列列表填充一个自定义适配器

我面临 RecyclerView 没有连接适配器的问题,即使在将适配器设置为布局管理器之后也是如此。我一个

如何在 Android 中将 RecyclerView 用于另一个 RecyclerView 的适配器?

RecyclerView() 适配器

两个ArrayList一个RecyclerView适配器

如何制作一个RecyclerView适配器以在每行中获取3个arraylist项

RecyclerView-上下滚动一个屏幕(不基于适配器位置)

从另一个片段的适配器刷新RecyclerView片段

从另一个片段重置RecyclerView适配器

使用RecyclerView适配器-错误E / RecyclerView:未连接适配器;跳过布局

从适配器更新recyclerview

我可以有两个 recyclerView 一个 viewHolder 和两个适配器吗?

如何用Firebase填充RecyclerView适配器

如何使用泛型将一个RecyclerView适配器用于不同类型的对象?

Firebase-在数组具有任何元素之前调用适配器,并返回一个空的RecyclerView

是否可以创建一个动态的RecyclerView适配器来接受任何对象列表<Object>的列表?

RecyclerView:未连接适配器;跳过片段中的布局

“ E / RecyclerView:未连接适配器;正在跳过布局”

recyclerview未连接适配器;跳过布局

Recycleview RecyclerView:未连接适配器;跳过布局

RecyclerView没有显示项目。适配器未调用

如何修复RecyclerView:未连接适配器;跳过布局?

RecyclerView 适配器 onItemClick 事件未触发

E / RecyclerView:未连接适配器;跳过布局

RecyclerView 错误:未连接适配器;跳过布局

问题在 Recyclerview 上使用 LiveData、Retrofit、Coroutine 实现更新 UI:适配器 Recyclerview 未更新

适配器中的Android RecyclerView显示问题