SHA*_*AHM 28 dependencies android android-recyclerview android-studio-2.0
经过几个小时的研究,我终于咨询了官方的帮助.
我有一个完美的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());
}
Run Code Online (Sandbox Code Playgroud)
根据Log消息(项目计数作为预期列表的大小),我假设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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在主代码中实现为
List<Attendee> attendees = AttendeeLab.get().getAttendeeList();
mAttendeeAdapter = new AttendeeAdapter(attendees);
mAmountRecyclerView.setAdapter(mAttendeeAdapter);
Run Code Online (Sandbox Code Playgroud)
我想代码可以工作(我想我没有做任何改动)但是gradle依赖关系可能没有正确设置.那是我尝试将recyclerview-v7:23.3.0修改为recyclerview-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'
}
Run Code Online (Sandbox Code Playgroud)
任何帮助或评论将不胜感激.我希望从现在开始几个小时之后,我会告诉你头痛的事.
Ash*_*wat 88
问题不在您的代码中.确保设置layout_height
为wrap_content
RecyclerView子项.
处理RecyclerView周期中的onBindViewHolder行为.RecyclerView.ViewHolder位置0 itemView
android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)
占用当前显示的屏幕.应该触发Scroll和onBindViewHolder,但要确保正确设置了getItemCount.
解:
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
使用ConstraintLayout作为膨胀itemView的解决方案:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"><!-- IMPORTANT -->
<!-- ... -->
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
对所有支持库使用相同的版本:
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.3.0' //<<< here
}
Run Code Online (Sandbox Code Playgroud)
另外,RecyclerView
应该与包一起添加'com.android.support:design:23.3.0'
- 覆盖类或Gradle
在构建过程中使用此“重复”包所做的任何魔法可能会导致您的问题。
归档时间: |
|
查看次数: |
9878 次 |
最近记录: |