当前位置: 首页 > 面试题库 >

Recyclerview不会调用任何Adapter方法:onCreateViewHolder,onBindViewHolder,

温星华
2023-03-14
问题内容

我的RecyclerView不会调用onCreateViewHolder,onBindViewHolder,因此,在recyclerview中不会显示任何内容。我放置了调试日志,但未显示日志。可以是什么?

我的适配器:

public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static final int EMPTY_VIEW = 10 ;
private ArrayList<comment> mItems;
Boolean firstTime = true;
private Typeface mTf = null;
Context mContext;
public CommentListAdapter(Context context,ArrayList<comment> items){
    Log.e("Adapter", "constructor Called");
    this.mItems = items;
    mContext = context;
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
    public EmptyViewHolder(View itemView) {
        super(itemView);
    }
}
public class ViewHolder extends RecyclerView.ViewHolder{
    TextView mAuthorName;
    TextView mMessage;
    NetworkImageView mThumbnail;
    public ViewHolder(View itemView) {
        super(itemView);
        mAuthorName = (TextView)itemView.findViewById(R.id.author_name);
        mMessage = (TextView)itemView.findViewById(R.id.message);
        mThumbnail = (NetworkImageView)itemView.findViewById(R.id.author_avatar);
    }

}

public void add(comment item, int position) {
    mItems.add(position, item);
    notifyItemInserted(position);
}

public void remove(comment item) {
    int position = mItems.indexOf(item);
    mItems.remove(position);
    notifyItemRemoved(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    Log.e("Adapter", "onCreateViewHolder Called");
    View v;
    if(firstTime){
        mTf = BBcTypeFace.getTypeFace(parent.getContext().getApplicationContext(),"font/bbc.ttf");
        firstTime = false;
    }
    if( viewType == EMPTY_VIEW){
        v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_empty_row,parent,false);
        EmptyViewHolder evh = new EmptyViewHolder(v);
        return evh;
    }else {
        v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_row, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    Log.e("Adapter", "onBindViewHolder Called");
    if(viewHolder instanceof ViewHolder) {
        ViewHolder holder = (ViewHolder)viewHolder;
        comment c = mItems.get(position);
        Log.e("Adapter", "Comment is\n: " + c.toString());
        final ViewHolder finalHolder = holder;
        ImageRequest request = new ImageRequest(c.author_img_link, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                if (bitmap != null) {
                    finalHolder.mThumbnail.setImageBitmap(bitmap);
                }
            }
        }, 0, 0, null,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        VolleyLog.e("ImageLoader", volleyError.getMessage());
                        VolleyLog.e("ImageLoader", volleyError.getStackTrace());
                    }
                });
        GetVideoInfo.getInstance(mContext.getApplicationContext()).addToRequestQueue(request);
        holder.mAuthorName.setText(c.author_name);
        holder.mMessage.setText(c.Message);
        holder.mMessage.setTypeface(mTf);
        holder.mAuthorName.setTypeface(mTf);
    }
}
@Override
public int getItemCount() {
    Log.e("Adapter", "getItemCount() Called");
    return (mItems.size() > 0 ? mItems.size() : 1);
}
@Override
public int getItemViewType(int position) {
    Log.e("Adapter", "getItemViewType() Called");
    if (mItems.size() == 0) {
        return EMPTY_VIEW;
    }
    return super.getItemViewType(position);
}}

我将用于public void add(comment item, int position){...}在RecyclerView中添加项目。

在片段中:

private RecyclerView mRecyclerView;
private CommentListAdapter mAdapter;
private LayoutManager mLayoutManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
....

    mLayoutManager = new LinearLayoutManager(getActivity());
    mAdapter = new CommentListAdapter(getActivity(),new ArrayList<comment>());
    mRecyclerView = (RecyclerView)rootView.findViewById(R.id.comment_list);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    makeJsonObjectRequest(mItem.url);
    return rootView;
}

XML档案:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:background="@android:color/white"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"

    android:layout_alignParentBottom="true">

    ....
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
.....

            <android.support.v7.widget.RecyclerView
                android:id="@+id/comment_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </android.support.v7.widget.RecyclerView>
....
        </RelativeLayout>
    ....
</ScrollView>

comment_row xml文件:

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

<LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:gravity="right">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/author_name"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/author_avatar"
            android:layout_toStartOf="@+id/author_avatar"
            android:gravity="right" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/message"
            android:layout_below="@+id/author_name"
            android:layout_toLeftOf="@+id/author_avatar"
            android:layout_toStartOf="@+id/author_avatar"
            android:gravity="right" />
        </LinearLayout>

<com.android.volley.toolbox.NetworkImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/author_avatar"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:minHeight="150dp"
    android:minWidth="150dp" /></LinearLayout>

我将使用以下代码将项目添加到RecyclerView:

JsonObjectRequest jsonObjReqComment = new
             JsonObjectRequest(Request.Method.GET,urlJsonObj+"#comment", null,
                     new Response.Listener<JSONObject>() {
                         @Override
                         public void onResponse(JSONObject response) {
                             try {
                                 JSONArray res = response.getJSONArray("response");
                                 //Log.e("Comment","Count:"+response.toString());
                                 //Log.e("Comment","Count:"+res.length());
                                 for (int i = 0; i < res.length(); i++) {
                                     JSONObject thread = res.getJSONObject(i);
                                     JSONObject author_json = thread.getJSONObject("author");
                                     int dislikes = thread.getInt("dislikes");
                                     int likes = thread.getInt("likes");
                                     String Message = thread.getString("message");
                                     //get Author info
                                     String author_img_link = author_json.getJSONObject("avatar").getString("permalink");
                                     String author_name = author_json.getString("name");
                                     comment c = new comment(dislikes,likes,Message,author_img_link,author_name);
                                     //Log.e("Comment",c.toString());
                                     //commentsList.add(c);
                                     mAdapter.add(c,0);
                                 }

                             } catch (JSONException e) {
                                 Log.e("OnResponse","Error JSON");
                                 e.printStackTrace();
                             } catch (Exception e){
                                 Log.e("OnResponse","Error Exception");
                                 e.printStackTrace();
                             }


                         }
                     }
                     , new Response.ErrorListener() {
                 @Override
                 public void onErrorResponse(VolleyError error) {
                     VolleyLog.d("vOLLEY", "Error: " + error.getMessage());
                     // hide the progress dialog

                 }
             }
             ){
                 @Override
                 public Map<String, String> getHeaders(){
                     Map<String, String> headers = new HashMap<String, String>();
                     headers.put("User-agent", "Comment");
                     return headers;
                 }
             };

问题答案:

当@yigit猜测的组合时ScrollViewRelativeLayout会导致此问题,请为腾出更多空间RecyclerView



 类似资料:
  • 没有任何错误,所有数据似乎都有效。出于某种原因,正在调用与视图相关的其他方法。我已经确保了以下几点: > > 正在调用构造函数,成员变量有效。 父视图是垂直线性布局;没有scrollview,或任何其他具有自己滚动属性的视图。 包含片段的视图被创建并显示在屏幕上。 下面是片段中的声明,后跟适配器。任何帮助都将不胜感激,因为这完全令人困惑。

  • 我想单击按钮将添加到中,但未调用适配器的实现方法。 这是我的代码: CourseDetailAdapter。班 我想为输入数据添加layout,所以我不确定是否正确

  • 我试图使用Reform2显示来自虚拟服务器(JSON服务器)的JSON数据,并将其显示在片段中的RecyclerView中。到目前为止,正在发生以下事件(使用日志信息发现): 主片段<代码>字符片段(CharactersListFragment)。kt创建已启动 只调用了。它正在返回0 未调用OnCreateViewHolder()和onBindViewHolder() 成功地从服务器获取数据并将

  • 我们从REST API获取到数据后,我们需要把它绑定View上,并用一个适配器填充列表。我们的RecyclerView适配器是标准的。它继承于RecyclerView.Adapter并指定它自己的ViewHolder: public static class ViewHolder extends RecyclerView.ViewHolder { @InjectView(R.id.name

  • 请注意:我已经检查了SO上的其他问题,虽然我可以消除错误,但它还有其他问题。 下面是我现有的代码: 我的适配器类: 修改代码查看各种答案: 现在,我没有得到错误,但是每次都被调用两次,并且recyclerview中的视图每次都在刷新。我做错了什么?请参阅logcat输出:

  • 我在将E3 RCP应用程序迁移到E4时遇到了问题。我有三个部分。2.样品含量为Lars Vogel(http://www.vogella.com/tutorials/EclipseRCP/article.html#plugin_creatinge4“使用SWT电源小部件”)进行测试,并将其与我的部件进行比较。 长话短说(er):应该创建视图内容的方法(标记为@PostConstruct)不会被调用