简体   繁体   English

RecyclerView 不调用 getItemCount

[英]RecyclerView not calling getItemCount

I've investigated several SO answers on this question ( here , here , and here ) and none of the proposed solutions have worked.我已经调查了关于这个问题的几个 SO 答案( herehere 和 here ),但没有一个提议的解决方案奏效。 My problem is that my RecyclerView list items aren't being displayed.我的问题是没有显示我的 RecyclerView 列表项。 I've set breakpoints in MessengerRecyclerAdapter, onCreateViewHolder, onBindViewHolder, and getItemCount and only the first one is ever called.我在 MessengerRecyclerAdapter、onCreateViewHolder、onBindViewHolder 和 getItemCount 中设置了断点,并且只调用了第一个。 While in a breakpoint I've entered the expression evaluator and executed在断点时,我进入了表达式求值器并执行了

MessengerRecyclerAdapter.getItemCount();

And received the expected answer of 20. The RecyclerView itself takes up the intended content area as demonstrated by the screenshot below (I turned the RecyclerView magenta to highlight the space it occupies).并收到了预期的答案 20。如下面的屏幕截图所示,RecyclerView 本身占据了预期的内容区域(我将 RecyclerView 变为洋红色以突出显示它占据的空间)。

回收站查看位置

My RecyclerView XML code is below:我的 RecyclerView XML 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/thread_list"
    android:background="@color/colorAccent"
    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:name="com.jypsee.jypseeconnect.orgPicker.MessengerListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    tools:context="com.jypsee.jypseeconnect.orgPicker.MessengerListFragment"
    tools:listitem="@layout/fragment_messenger_cell"/>

</LinearLayout>

My RecyclerView Cell XML:我的 RecyclerView 单元格 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal">

<TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textColor="@color/blueText"/>

<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textColor="@color/darkText"/>
</LinearLayout>

My ListFragment class:我的 ListFragment 类:

    @Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {

    List<DummyContent.DummyItem> items = new ArrayList<>();
    for (Integer i = 0; i<20; i++){
        DummyContent.DummyItem item = new DummyContent.DummyItem(i.toString(),"Content","Details");
        items.add(item);
    }

    View view = inflater.inflate(R.layout.fragment_messenger_list, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.thread_list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerAdapter = new MessengerThreadRecyclerAdapter(items, mListener);
    mRecyclerView.setAdapter(mRecyclerAdapter);
    mRecyclerAdapter.notifyDataSetChanged();
    return view;
}

My Adapter class:我的适配器类:

public class MessengerRecyclerAdapter
    extends RecyclerView.Adapter<MessengerRecyclerAdapter.MessageThreadHolder>{

private final List<DummyItem> mValues;
private final RecyclerViewClickListener mListener;

public MessengerRecyclerAdapter(List<DummyItem> items, RecyclerViewClickListener listener) {
    mValues = items;
    mListener = listener;
}

@Override
public MessageThreadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_messenger_cell, parent, false);
    return new MessageThreadHolder(view);
}

@Override
public void onBindViewHolder(final MessageThreadHolder holder, final int position) {
    holder.mItem = mValues.get(position);
    holder.mIdView.setText(mValues.get(position).id);
    holder.mContentView.setText(mValues.get(position).content);
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.recyclerViewListClicked(v, position);
            }
        }
    });
}

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

public class MessageThreadHolder extends RecyclerView.ViewHolder {
    public final View mView;
    public final TextView mIdView;
    public final TextView mContentView;
    public DummyItem mItem;

    public MessageThreadHolder(View view) {
        super(view);
        mView = view;
        mIdView = (TextView) view.findViewById(R.id.id);
        mContentView = (TextView) view.findViewById(R.id.content);
    }
}

} }

As you can see I've set the linearLayout orientation to vertical and set the layout manager, which were the 2 most common solutions.如您所见,我已将 linearLayout 方向设置为垂直并设置布局管理器,这是两种最常见的解决方案。 I'm really at a loss as to what to try next, so any help is appreciated.我真的不知道接下来要尝试什么,因此感谢您的帮助。

As I said in previous Answer edit.正如我在之前的答案编辑中所说。 The issues was in your xml.问题出在您的 xml 中。 The main reason it was not showing was because, You were trying to add the fragment using include tag instead of fragment tag thus respective fragment class was never getting called on the fragment layout being added to your activity.它没有显示的主要原因是,您试图使用包含标签而不是片段标签添加片段,因此从未在添加到您的活动的片段布局上调用相应的片段类。 Below is the code you needed to add the Fragment correctly.下面是正确添加 Fragment 所需的代码。

<fragment
        android:id="@+id/message_thread"
        android:name="com.jypsee.jypseeconnect.orgPicker.MessengerThreadListFragment"
        layout="@layout/fragment_messengerthread_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_messengerthread_list" />

And your fragment layout should be like this你的片段布局应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".orgPicker.MessengerThreadListFragment">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/thread_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Here is the screenshot of working这是工作的截图在此处输入图片说明

就我而言,在适配器中设置列表后,我没有调用 notifyDataSetChanged()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView 里面的 RecyclerView 弄错了 getItemCount() - RecyclerView inside RecyclerView made wrong getItemCount() RecyclerView getChildCount() 和 getItemCount() 返回相同的值 - RecyclerView getChildCount() and getItemCount() returns same value RecyclerView 适配器的 getItemCount 抛出 NullPointerException - RecyclerView adapter's getItemCount throws NullPointerException RecyclerView未被填充(getItemCount()返回零) - RecyclerView not being populated (getItemCount() is returning zero) 未调用RecyclerView重写方法,并且getItemCount始终返回0 - RecyclerView Overriden methods not called and getItemCount always return 0 我们可以从RecyclerView的getItemCount()中获得两种返回类型吗? - Can we get two return types from getItemCount() in RecyclerView? 如何将 ArrayList 大小从 onBindViewHolder 传递给 RecyclerView 中的 getItemCount() 方法? - How to pass ArrayList size from onBindViewHolder to getItemCount() method in RecyclerView? 我的 RecyclerView 适配器的 getItemCount() 返回 0,即使里面有项目 - getItemCount() of my RecyclerView adapter returns 0 even though I have items in it RecyclerView的onBindview持有人无法为第二名工作,但我在getItemCount中数了三 - onBindview Holder of RecyclerView is not working for second position but i have count three in getItemCount FirebaseRecyclerAdapter getItemCount = 0 - FirebaseRecyclerAdapter getItemCount = 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM