我正在构建一个原生的android应用程序。我有一个服务,从服务器返回一个项目列表。我想收集收到的数据并将其添加到回收站视图(目前只有TextViews)。在初始加载活动和数据时,RecyclerView会正确显示数据。但是,当我开始滚动时,似乎停止将数据绑定到视图,列表看起来是空白的。我在这里做错了什么想法?
这是我的适配器
public class SearchItemAdapter extends RecyclerView.Adapter<SearchItemAdapter.SearchItemViewHolder> {
@Override
public SearchItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View itemView = LayoutInflater.from(context)
.inflate(R.layout.search_item_result_cell, parent, false);
return new SearchItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(SearchItemViewHolder holder, int position) {
ItemSearchResult searchResult = searchResults.get(position);
holder.bindSearchData(searchResult);
}
@Override
public int getItemCount() {
return searchResults.size();
}
public SearchItemAdapter(List<ItemSearchResult> searchResults) {
this.searchResults = searchResults;
}
public static class SearchItemViewHolder extends RecyclerView.ViewHolder {
private TextView description;
private TextView regularPrice;
private TextView salePrice;
private TextView aisle;
public SearchItemViewHolder(View itemView) {
super(itemView);
description = (TextView) itemView.findViewById(R.id.description);
regularPrice = (TextView) itemView.findViewById(R.id.regular_price);
salePrice = (TextView) itemView.findViewById(R.id.sale_price);
aisle = (TextView) itemView.findViewById(R.id.aisle);
}
public void bindSearchData(ItemSearchResult searchResult) {
aisle.setText(searchResult.getAisle());
salePrice.setText(searchResult.getAd());
regularPrice.setText(searchResult.getRegular());
description.setText(searchResult.getDescription());
}
}
}
以下是我的活动初始化数据和视图的一些代码
private RecyclerView mSearchResultRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_search);
mSearchResultRecyclerView = (RecyclerView) findViewById(R.id.item_search_recycler_view);
searchTerms = (EditText) findViewById(R.id.search_terms);
}
@Subscribe
public void displaySearchResults(SearchItemResultEvent event) {
mAdapter = new SearchItemAdapter(event.getSearchResults());
mSearchResultRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
mSearchResultRecyclerView.setLayoutManager(mLayoutManager);
}