我完全被这个虫子迷住了,我明白,但我不知道怎么了。
对于代码:

// In the OnCreate of my activity
  historyRecyclerView = (RecyclerView)findViewById(R.id.recycler_suggestions);
  SearchBarHistoryAdapter searchBarHistoryAdapter = new SearchBarHistoryAdapter();
  searchBarHistoryAdapter.setActivity(this);
  historyRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
  historyRecyclerView.setAdapter(searchBarHistoryAdapter);
  searchBarDisplayManager.setTypeAdapter(SearchBarDisplayManager.SEARCH_TYPE.HISTORY, searchBarHistoryAdapter);

SearchDisplayManager只包含适配器列表。
适配器:
public class SearchBarHistoryAdapter extends SearchBarAdapter {
private ArrayList<String> historyList;
private HistoryTask historyTask;

private void setHistoryList(ArrayList<String> history) {
historyList = history;
notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_list_item, parent, false);
final HistoryViewHolder historyViewHolder = new HistoryViewHolder(v);
return historyViewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  ((HistoryViewHolder) holder).bind(historyList.get(position));
}

@Override
public int getItemCount() {
return historyList == null ? 0 : historyList.size();
}

@Override
public void startSearch(String searchString) {
if(TextUtils.isEmpty(searchString)) {
  if (historyTask != null) {
    historyTask.cancel(true);
  }
  historyTask = new HistoryTask();
  historyTask.execute();
}else{
  setHistoryList(null);
}
}

private class HistoryTask extends AsyncTask<Void, Void, ArrayList<String>> {

@Override
protected ArrayList<String> doInBackground(Void... params) {
  String history = HelperUI.getSearchbarHistory(activity);
  if (!TextUtils.isEmpty(history)) {
    return new ArrayList<>(Arrays.asList(history.split("--")));
  }

  return new ArrayList<>(0);
}

@Override
protected void onPostExecute(ArrayList<String> results) {
  super.onPostExecute(results);
  if (!results.isEmpty()) {
    setHistoryList(results);
  }
  historyTask = null;
}
}

private class HistoryViewHolder extends RecyclerView.ViewHolder {
TextView hint, name;
ImageView img;

public HistoryViewHolder(View v) {
  super(v);
  name = (TextView) v.findViewById(R.id.app_label);
  hint = ((TextView) v.findViewById(R.id.type_label));
  img = ((ImageView) v.findViewById(R.id.item_icon));
}

public void bind(String suggestion) {
  name.setText(Html.fromHtml(suggestion));
  img.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_public_grey600_48dp));
  img.setTag(suggestion);
}
}
}

这里是疯狂的部分,当我更新setHistoryList中的列表时,我知道recycler视图有正确的适配器,它不是null。但是当它试图通过notifyDatasetChanged()进行更新时,它有点失去了它,在recycler视图中没有更多的适配器。它只是消失了,当然,什么也没显示。
知道怎么了吗?

最佳答案

您必须添加一个layoutmanager,就像您经常对为recyclerview创建的任何适配器所做的那样。

 historyRecyclerView.setLayoutManager(new LinearLayoutManager(context));

只需在setAdpater下面的代码下面添加这一行,您就可以使用adapter设置回收器视图。

07-28 03:53