RecyclerView不调用getItemCount [英] RecyclerView not calling getItemCount

查看:435
本文介绍了RecyclerView不调用getItemCount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经调查了有关此问题的几种答案(



我的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
工具:context = com.jypsee.jypseeconnect.orgPicker.MessengerListFragment
工具:listitem = @ layout / fragment_messenger_cell />

< / LinearLayout>

我的RecyclerView Cell 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 = horizo​​ntal>

< 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 saveInstanceState){

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();
返回视图;
}

我的适配器类:

 公共类MessengerRecyclerAdapter 
扩展了RecyclerView.Adapter< MessengerRecyclerAdapter.MessageThreadHolder> {

私人最终列表< DummyItem> mValues;
私人最终RecyclerViewClickListener mListener;

public MessengerRecyclerAdapter(List< DummyItem>项目,RecyclerViewClickListener侦听器){
mValues = items;
mListener =监听器;
}

@Override
public MessageThreadHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_messenger_cell,父级,false);
返回新的MessageThreadHolder(view);
}

@Override
公共无效onBindViewHolder(最终MessageThreadHolder持有人,最终int位置){
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();
}

公共类MessageThreadHolder扩展了RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
个公共最终TextView mContentView;
public DummyItem mItem;

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

}



如您所见,我已经将linearLayout方向设置为vertical,并设置了布局管理器,这是两种最常见的解决方案。我真的对下一步的尝试感到茫然,因此不胜感激。

解决方案

回答编辑。问题出在您的xml中。它未显示的主要原因是,您试图使用include标记而不是fragment tag来添加片段,因此从未在要添加到您的活动的片段布局上调用各个片段类。
下面是正确添加片段所需的代码。

 <片段
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
工具: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
工具:context =。orgPicker.MessengerThreadListFragment>
< android.support.v7.widget.RecyclerView
android:id = @ + id / thread_list
android:layout_width = match_parent
android:layout_height = match_parent />
< / LinearLayout>

以下是工作


I've investigated several SO answers on this question (here, here, and here) and none of the proposed solutions have worked. My problem is that my RecyclerView list items aren't being displayed. I've set breakpoints in MessengerRecyclerAdapter, onCreateViewHolder, onBindViewHolder, and getItemCount and only the first one is ever called. 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).

My RecyclerView XML code is below:

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

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

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

这篇关于RecyclerView不调用getItemCount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆