android – RecyclerView.onBindViewHolder只调用一次

前端之家收集整理的这篇文章主要介绍了android – RecyclerView.onBindViewHolder只调用一次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
经过多少小时的研究,我终于咨询了官方的帮助.

我有一个RecyclerView.Adapter和RecyclerView.ViewHolders工作完美.但由于某些原因我不明白,RecyclerView.Adapter.onBindViewHolder没有正确调用.

private class AttendeeAdapter extends RecyclerView.Adapter<AttendeeHolder> {
    /*FIELDS*/
    private List<Attendee> mAttendeeList;

    /*CONSTRUCTORS*/
    public AttendeeAdapter(List<Attendee> attendees) {
        mAttendeeList = attendees;
        //Log.i(TAG,"AttendeeAdapter size: " + getItemCount());
    }

基于日志消息(项目计数作为预期的列表的大小),我假设AttendeeAdapter已被正确实例化.

所以我希望onBindViewHolder(VH,int)方法将被调用多次,而不是List的大小.该方法调用ONCE!

/*METHODS*/
    @Override
    public AttendeeHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View itemView = layoutInflater.inflate(R.layout.list_attendee,parent,false);
        return new AttendeeHolder(itemView);
    }

    @Override
    public void onBindViewHolder(AttendeeHolder holder,int position) {
        Attendee attendee = mAttendeeList.get(position);
        holder.bindAttendee(attendee,position);

        Log.i(TAG,"Binding ViewHolder #" + position);
        /* Binding ViewHolder #0 and that's it */
    }

    @Override
    public int getItemCount() {
        return mAttendeeList.size();
    }
}

我的AttendeeHolder(扩展RecyclerView.ViewHolder)如下:

private class AttendeeHolder extends RecyclerView.ViewHolder {
    /*FIELDS*/
    private EditText mAttendeeNameEditText;
    private Attendee mAttendee;

    /*CONSTRUCTOR*/
    public AttendeeHolder(View itemView) {
        super(itemView);
        mAttendeeNameEditText = (EditText) itemView.findViewById(R.id.edit_text_list_item);
        mAmountEditTextList = new ArrayList<>(eventMaxCount);
      }

    /*METHODS*/
    public void bindAttendee(Attendee attendee,final int position) {
        mAttendee = attendee;
        String attendeeName = mAttendee.getName();

        // Set the name to the EditText if a name has already been set
        if (attendeeName != null) {
            mAttendeeNameEditText.setText(attendeeName);
        }
    }
}

并在主要代码中实现

List<Attendee> attendees = AttendeeLab.get().getAttendeeList();
     mAttendeeAdapter = new AttendeeAdapter(attendees);
     mAmountRecyclerView.setAdapter(mAttendeeAdapter);

我猜这个代码可以工作(我想我没有做任何改变),但是毕业生的依赖可能没有正确设置.那就是我尝试修改recyclelerview-v7:23.3.0到recyclelerview-v7:23.1.0或其他(没有一个工作).

dependencies {
compile fileTree(dir: 'libs',include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:recyclerview-v7:23.1.2'
}

任何帮助或评论将不胜感激.我希望在现在几个小时后,我会再次对头痛说再见.

解决方法

问题不在你的代码中.确保将layout_height设置为RecyclerView子项的wrap_content.
原文链接:https://www.f2er.com/android/312737.html

猜你在找的Android相关文章