我有一个带适配器的recyclerView
。我尝试以编程方式设置adapter
,但它不起作用。
这是我的代码:
public class RecordAdapter extends RecyclerView.Adapter<RecordViewHolder>{
private List<RecordData> records;
public RecordAdapter(){
records = new ArrayList<>();
}
@Override
public RecordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
FLog.d("Records: creating new recordViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.record_item, parent, false);
return new RecordViewHolder(view);
}
@Override
public void onBindViewHolder(RecordViewHolder holder, int position) {
RecordData data = records.get(position);
FLog.d("Records: binding new recordViewHolder");
holder.mRecordText.setText("Record " + position);
}
@Override
public int getItemCount() {
FLog.d("Records: count is - " + records.size());
return records.size();
}
public void addRecord(RecordData newRecord){
records.add(newRecord);
this.notifyDataSetChanged();
}
}
我的RecordViewHolder是:
public class RecordViewHolder extends RecyclerView.ViewHolder{
@Bind(R.id.record_play_button)
public ImageButton mRecordPlay;
@Bind(R.id.record_message_text)
public TextView mRecordText;
@Bind(R.id.record_seek_bar)
public SeekBar mSeekBar;
public RecordViewHolder(View itemView){
super(itemView);
ButterKnife.bind(this, itemView);
}
我试图像这样设置适配器:
if (adapter == null){
adapter = new RecordAdapter();
}
adapter.addRecord(recordData);
holder.mIncomingRecordList.setAdapter(adapter);
但是我的adapter
没有调用methoda onCreateViewHolder
和onBindViewHolder
我的XML
文件recyclerView
:
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="wrap_content"
a:layout_height="wrap_content">
<LinearLayout
a:layout_width="wrap_content"
a:layout_height="wrap_content">
.......
<LinearLayout
a:layout_width="wrap_content"
a:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
a:layout_width="match_parent"
a:layout_height="match_parent"
a:id="@+id/record_list"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>