Con*_*nti 57 java android android-fragments android-recyclerview
没有得到任何错误,所有数据似乎都有效.出于某种原因,正在调用与视图相关的方法.我确定了以下内容:
getItemCount()是唯一被调用的适配器方法,并返回一个正整数值,(我知道这将是你们要看的区域)
正在调用构造函数,成员变量是有效的.
父视图是一个垂直的LinearLayout; 没有滚动视图,或任何其他具有自己的滚动属性的视图.
包含片段视图已创建并显示在屏幕上.
这是片段中的声明,后跟适配器.任何帮助将不胜感激,因为这是完全困惑的.
SubMenuAdapter adapter = new SubMenuAdapter(getActivity(), mContentItems);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Aer*_*rim 88
这也可以帮助某人
首次使用
recyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
然后:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Run Code Online (Sandbox Code Playgroud)
所以它看起来像这样:
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Run Code Online (Sandbox Code Playgroud)
订单相反
ral*_*abb 38
被追了一个多小时的答案.
在设置适配器之前,不要忘记调用这个衬垫.
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Run Code Online (Sandbox Code Playgroud)
似乎recycleler视图没有内置的默认布局管理器选项,所以我们必须以编程方式添加它.
如果您觉得有用,请干杯.
rad*_*may 19
确保您从nestedscrollview回收视图不是子视图
例如,此代码不起作用.
<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>
Run Code Online (Sandbox Code Playgroud)
然后我用,
<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"/>
Run Code Online (Sandbox Code Playgroud)
Abd*_*mad 11
我不知道这对任何人都有帮助,但我几乎有同样的问题.我的问题不在于片段/类和回收器适配器.问题出现在父XML格式中,其中操作栏占用了替换片段match_parent的FrameLayout-in,但没有显示任何可用的位置.这就是为什么不调用这些方法的原因.
我想类似的情况可能适用于那些面临同样问题的人.仔细检查每个XML文件中的每个宽度和高度可能在同一个树中,因为问题似乎根本不在适配器中.
小智 8
我的两个便士在这里.只花了三个多小时调试这个.
确保您没有不小心使用以下行.
recyclerView.setHasFixedSize(true);
仅当您确定视图具有恒定大小时才设置固定大小.如果不是,则可能根本不会调用onCreateViewHolder和onBindView方法.
我也遇到过相同的问题,onCreateViewHolder即onBindView方法没有被调用,而只是getItemCount被调用。因此,从根本上来说,高度和重量RecyclerView设置为wrap_content或是一个问题0dp。将其更改为某个值后,可以解决此问题。
| 归档时间: |
|
| 查看次数: |
42788 次 |
| 最近记录: |