Java RecyclerView 不调用 onCreateViewHolder 或 onBindView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28992929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RecyclerView not calling onCreateViewHolder or onBindView
提问by Conti
Not getting any errors and all the data seems valid. For some reason, nether of the view related methods are being called. I have made sure of the following:
没有收到任何错误,所有数据似乎都有效。出于某种原因,正在调用与视图相关的方法。我已经确定了以下几点:
getItemCount() is the only adapter method being called and is returning a positive integer value, (I know this will be the area you guys will look at)
Constructor is being called, member variables are valid.
Parent View is a vertical LinearLayout; no scrollview, or any other view with their own scroll properties in sight.
containing fragment view is created and shown on screen.
getItemCount() 是唯一被调用的适配器方法,它返回一个正整数值,(我知道这将是你们要查看的区域)
正在调用构造函数,成员变量有效。
Parent View 是一个垂直的 LinearLayout;没有滚动视图,或任何其他具有自己滚动属性的视图。
包含片段视图被创建并显示在屏幕上。
Here is the declaration in the fragment followed by the adapter. Any help would be appreciated as this has be completely baffled.
这是片段中的声明,后跟适配器。任何帮助将不胜感激,因为这完全被困惑了。
SubMenuAdapter adapter = new SubMenuAdapter(getActivity(), mContentItems);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
public class SubMenuAdapter extends RecyclerView.Adapter<SubMenuAdapter.ViewHolder> {
private static final String TAG = String.format("==> %S", SubMenuAdapter.class.getSimpleName());
private final List<ContentItem> mContentItems;
private Context mContext;
public SubMenuAdapter(Context context, List<ContentItem> contenItems) {
Log.d(TAG, "Constructor called");
mContentItems = contenItems;
mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder called");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_resource_efficiency, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder called");
ContentItem item = mContentItems.get(position);
holder.textName.setText(item.getName());
FontSetter.setMyriadProRegular(mContext, holder.textName);
Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon);
}
@Override
public int getItemCount() {
Log.d(mContext, String.format("getItemCount: %d", mContentItems.size()));
return mContentItems.size();
}
// ViewHolder
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textName;
ImageView imageIcon;
public ViewHolder(View view) {
super(view);
textName = (TextView) view.findViewById(R.id.tv_resource_efficiency_option);
imageIcon = (ImageView) view.findViewById(R.id.iv_resource_efficiency_icon);
}
}
回答by raditya gumay
Make sure you recycle view is not child from nestedscrollview
确保回收视图不是来自nestedscrollview 的子视图
for example, this code will not work.
例如,此代码将不起作用。
<android.support.v4.widget.NestedScrollView
android:id="@+id/responder_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkGray"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:cardElevation="@dimen/spacing_medium"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/responder_testimonial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Testimonial"
android:textSize="@dimen/general_font_size" />
<TextView
android:id="@+id/responder_counter_testimonial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/responder_testimonial"
android:text="170"
android:textSize="@dimen/general_font_size_small" />
<Button
android:id="@+id/responder_btn_testimonial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Go" />
<view
android:id="@+id/responder_list_testimonial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/responder_testimonial"
class="android.support.v7.widget.RecyclerView"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
then i use,
然后我用,
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<view
android:id="@+id/responder_list_testimonial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/responder_testimonial"
class="android.support.v7.widget.RecyclerView"/>
回答by Abdul-Rahman Ahmad
I don't know if this will be helpful to anyone, but I almost had the same exact problem.
My problem was not in either the fragment/class nor the recycler adapter. The problem was in parent XML layout where the actionbar took match_parent
where the FrameLayout
-in which the fragment is replaced- didn't get any available place to be shown. That's why the methods weren't called.
我不知道这是否对任何人都有帮助,但我几乎遇到了同样的问题。我的问题不在于片段/类,也不在于回收器适配器。问题出在父 XML 布局中,其中 actionbarmatch_parent
所在的位置FrameLayout
- 在其中替换了片段 - 没有显示任何可用位置。这就是没有调用这些方法的原因。
I guess similar scenarios might be the case for those facing the same problem. Double check every width and height in every XML file might be in the same tree, as the problem doesn't seem to be in the adapter at all.
我想那些面临同样问题的人可能会遇到类似的情况。仔细检查每个 XML 文件中的每个宽度和高度可能在同一棵树中,因为问题似乎根本不在适配器中。
回答by Aerim
This may also help someone
这也可能对某人有所帮助
First Use
首次使用
recyclerView.setAdapter(adapter);
And then:
进而:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
So it will look like this:
所以它看起来像这样:
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
The order is reversed
顺序颠倒了
回答by dpspae03
I had this same issue where neither onCreateViewHolder or onBindViewHolder were being called. Changing your onCreateViewHolder method to use the Context you saved a reference to in the constructor instead of doing parent.getContext() should fix it:
我有同样的问题,既没有调用 onCreateViewHolder 也没有调用 onBindViewHolder 。更改您的 onCreateViewHolder 方法以使用您在构造函数中保存引用的 Context 而不是执行 parent.getContext() 应该修复它:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder called");
View view = LayoutInflater.from(mContext).inflate(R.layout.row_resource_efficiency, parent, false);
return new ViewHolder(view);
回答by Venkatesh Shukla
My two pennies here. Just spent more than three hours debugging this.
我的两便士在这里。只花了三个多小时调试这个。
Make sure that you have not used the following line carelessly.
确保您没有不小心使用以下行。
recyclerView.setHasFixedSize(true);
recyclerView.setHasFixedSize(true);
Set the fixed size only when you are sure that the view would have a constant size. In case it is not, the onCreateViewHolder and onBindView methods might not get called at all.
仅当您确定视图具有恒定大小时才设置固定大小。如果不是,则可能根本不会调用 onCreateViewHolder 和 onBindView 方法。
回答by Mateusz Wlodarczyk
In my case I set the ID of my RecyclerView to "recyclerView". It seems that this ID was already defined somewhere, because when I changed that to different ID, the methods from the adapter were called properly.
就我而言,我将 RecyclerView 的 ID 设置为“recyclerView”。似乎这个 ID 已经在某处定义了,因为当我将它更改为不同的 ID 时,来自适配器的方法被正确调用。
回答by Vijay Pal
I had also face the same issue where onCreateViewHolder
or onBindView
method was not getting invoke, only getItemCount
was getting invoked. So it was basically issue with the height and weight of RecyclerView
which was set to wrap_content
or 0dp
. After changing it to some value fixed the problem.
我也遇到了同样的问题,其中onCreateViewHolder
或onBindView
方法没有被调用,只是getItemCount
被调用。所以基本上是高度和重量RecyclerView
设置为wrap_content
或 的问题0dp
。将其更改为某个值后解决了问题。
回答by bem22
I know this topic is old, but if you did the same learner mistake, as I did, you might find this helpful.
我知道这个话题很老,但如果你和我一样犯了同样的学习错误,你可能会发现这很有帮助。
As I was studying on some website about RecyclerView and copying some code and adapting to my context, I have created different variables (eg users and data) which were meant to do the same thing, except some methods were calling data and some were calling users. This is an easy mistake to do as you try to understand other's code and java functionality (it happens to me all the time).
当我在一些关于 RecyclerView 的网站上学习并复制一些代码并适应我的上下文时,我创建了不同的变量(例如用户和数据),它们旨在做同样的事情,除了一些方法调用数据,一些方法调用用户. 当您试图理解其他人的代码和 Java 功能时,这是一个很容易犯的错误(它一直发生在我身上)。
It's good to know that you can println in your console anywhere in your java code, so you can debug like that pretty easily.
很高兴知道您可以在 Java 代码的任何位置在控制台中 println,因此您可以非常轻松地进行调试。
Good luck!
祝你好运!
回答by Jonathan Kibet
I recently encountered this problem and apparently there are many possible causes. Try one or combination of these options, see which one(s) work(s) for you :)
我最近遇到了这个问题,显然有很多可能的原因。尝试这些选项中的一个或组合,看看哪一个适合你:)
1. Layout manager
Ensure that you set a layout manager to your recycler view as below
1.布局管理器
确保您将布局管理器设置为您的回收站视图,如下所示
// create a layout manager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(my_context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// get recycler view
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_id);
// set
recyclerView.setLayoutManager(linearLayoutManager);
2. Fixed Size
Ensure that you state whether your recycler view has a fixed size or not
2. 固定大小
确保你声明你的回收者视图是否有固定大小
// set fixed size
recyclerView.setHasFixedSize(true); // or false depending on implementation
3. XML
Ensure that your recycler is not nested in too many layouts which may cause confusion such as combination of ScrollView and RelativeLayout. Make it simple, see if it fixes your issue
3. XML
确保您的回收器没有嵌套在太多布局中,例如ScrollView 和RelativeLayout 的组合可能导致混淆。简单点,看看能不能解决你的问题
<?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">
<android.support.v7.widget.RecyclerView
android:id="@+id/thread_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
回答by ralphgabb
Been chasing answer for over an hour.
追了一个多小时的答案。
Dont forget to call this one liner before setting your adapter.
在设置适配器之前,不要忘记调用这个衬垫。
recyclerView.setLayoutManager(new LinearLayoutManager(this));
It seems that recycler view do not have default layout manager option built in so we have to programatically add it.
似乎回收站视图没有内置默认布局管理器选项,因此我们必须以编程方式添加它。
Cheers if you found it helpful.
如果您觉得有帮助,请干杯。