【问题标题】:RecyclerView not calling getItemCountRecyclerView 没有调用 getItemCount
【发布时间】:2018-02-12 06:48:27
【问题描述】:

我已经调查了关于这个问题的几个 SO 答案(herehereand here),但提出的解决方案都没有奏效。我的问题是没有显示我的 RecyclerView 列表项。我在 MessengerRecyclerAdapter、onCreateViewHolder、onBindViewHolder 和 getItemCount 中设置了断点,并且只调用了第一个。在断点中,我输入了表达式求值器并执行了

MessengerRecyclerAdapter.getItemCount();

并收到了预期的答案 20。RecyclerView 本身占据了预期的内容区域,如下面的屏幕截图所示(我将 RecyclerView 变为洋红色以突出显示它占用的空间)。

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

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

我的 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;
}

我的适配器类:

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);
    }
}

}

如您所见,我已将 linearLayout 方向设置为垂直并设置布局管理器,这是两种最常见的解决方案。我真的不知道接下来要尝试什么,因此感谢您提供任何帮助。

【问题讨论】:

  • 在 onViewCreated() 方法而不是 onCreateView() 中编写您的代码,在 onCreateView 中您应该只返回您的膨胀视图,其余代码必须在 onViewCreated 中。
  • 显示您的fragment_messenger_cell.xml 并尝试mRecyclerView.setHasFixedSize(true);

标签: java android android-recyclerview adapter android-viewholder


【解决方案1】:

正如我在之前的答案编辑中所说。问题出在您的 xml 中。它未显示的主要原因是,您试图使用包含标记而不是片段标记添加片段,因此在添加到您的活动的片段布局上从未调用相应的片段类。 下面是正确添加 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" />

而你的片段布局应该是这样的

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

这是工作的截图

【讨论】:

  • @JamesJordanTaylor 我已经更新了我的答案并向您发送了电子邮件。请检查
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2021-11-20
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多