这就是我在搜索片段中设置回收器视图的方式。我不明白为什么我总是收到同样的错误以及我错过了什么。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
searchText = (EditText)view.findViewById(R.id.searchText);
searchButton = (Button)view.findViewById(R.id.searchButton);
nutritionRecyclerView = (RecyclerView)view.findViewById(R.id.nutritionRecyclerView);
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ApiClient.getInstance().getNutritionApiAdapter()
.getSearchResults(
AppDefines.APPLICATION_ID,
AppDefines.APPLICATION_KEY,
searchText.getText().toString())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<SearchResultsModel>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(SearchResultsModel searchResultsModel) {
// On handling the http response, instantiate a new adapter with the results
adapter = new NutritionAdapter(searchResultsModel.getSearchResults());
adapter.setOnItemSelected(new NutritionAdapter.OnItemSelected() {
@Override
public void onSelected(NutritionItemModel item) {
if (onFragmentEvent != null) {
onFragmentEvent.onEvent(item);
}
}
});
// Assigning the LayoutManager to the RecyclerView
nutritionRecyclerView.setLayoutManager(layoutManager);
// Assigning the Adapter to the RecyclerView. If this isn't done, the view will not populate
nutritionRecyclerView.setAdapter(adapter);
}
});
}
});
return view;
}
这是我的 API 客户端 公共类 API 客户端 { 私有静态 ApiClient 实例;
public static ApiClient getInstance() {
if (instance == null) {
instance = new ApiClient();
}
return instance;
}
public NutritionApiAdapter getNutritionApiAdapter() {
NutritionApiAdapter api = new Retrofit.Builder()
.baseUrl(AppDefines.BASE_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build()
.create(NutritionApiAdapter.class);
return api;
}
}
这是我的营养适配器
public interface NutritionApiAdapter {
@GET("item")
Observable<SearchResultsModel> getSearchResults(
@Query("_app_id") String appId,
@Query("_app_key") String appKey,
@Query("q") String searchString);
}
营养补充
public class NutritionAdapter extends RecyclerView.Adapter<NutritionItemViewHolder>{
private ArrayList<NutritionItemModel> nutritionItemCollection;
private OnItemSelected onItemSelected;
public NutritionAdapter(ArrayList<NutritionItemModel> nutritionItemCollection){
this.nutritionItemCollection = nutritionItemCollection;
}
@Override
public NutritionItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nutrition, parent, false);
return new NutritionItemViewHolder(view);
}
@Override
public void onBindViewHolder(NutritionItemViewHolder holder,int position) {
NutritionItemModel item = nutritionItemCollection.get(position);
holder.setOnRecipeItemClicked(new NutritionItemViewHolder.OnNutritionItemClicked(){
@Override
public void onClick(NutritionItemModel item) {
if(onItemSelected != null) {
onItemSelected.onSelected(item);
}
}
});
// Bind the RecipeItemModel data to the view managed by the ViewHolder
holder.bind(item);
}
@Override
public final void onViewRecycled(final NutritionItemViewHolder holder) {
super.onViewRecycled(holder);
holder.setOnRecipeItemClicked(null);
holder.unbind();
}
@Override
public int getItemCount() {return nutritionItemCollection.size();}
public void setOnItemSelected(OnItemSelected onItemSelected) {
this.onItemSelected = onItemSelected;
}
public interface OnItemSelected {
void onSelected(NutritionItemModel item);
}
}
和营养项目视图持有人
public class NutritionItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView foodName;
private ImageView foodThumbnail;
private NutritionItemModel item;
private TextView item_name;
private TextView brand_name;
private OnNutritionItemClicked onNutritionItemClicked;
//todo add other elements here
public NutritionItemViewHolder(final View itemView) {super(itemView);}
public final void bind(final NutritionItemModel item) {
item_name = (TextView)itemView.findViewById(R.id.item_name);
brand_name = (TextView)itemView.findViewById(R.id.brand_name);
//foodThumbnail = (ImageView)itemView.findViewById(R.id.foodThumbnail);
//todo add other element id things here
/*
//used for preloading images
Glide.with(itemView.getContext())
.load(item.getSmallImageUrls().get(0))
.into(foodThumbnail);
*/
item_name.setText(item.getFields().getItem_name());
brand_name.setText(item.getFields().getBrand_name());
}
public final void unbind() {
//todo ?
//For later with butterknife
}
@Override
public void onClick(View v) {
if(onNutritionItemClicked != null) {
onNutritionItemClicked.onClick(item);
}
}
// The setter that allows other classes to create a reference to the listener.
public void setOnRecipeItemClicked(OnNutritionItemClicked onNutritionItemClicked) {
this.onNutritionItemClicked = onNutritionItemClicked;
}
// An interface is added as an internal implementation in our ViewHolder. This will allow
// classes that instantiate a new instance of this ViewHolder to subscribe to this interface
// and listen for events.
public interface OnNutritionItemClicked {
void onClick(NutritionItemModel item);
}
}
设置布局管理器,
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter(adapter);
也许这可以帮助你。