经过多少小时的研究,我终于咨询了官方的帮助.
我有一个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.