我完全迷失了这个错误,我明白了,但我不知道出了什么问题。 对于代码:
// 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
中的列表时,我知道回收器视图有正确的适配器,它不是null
...但是它有点失去它,没有当它试图按notifyDatasetChanged()
进行更新时,在Recycler视图中有更多适配器...它只是消失了,并且没有显示任何内容。
知道什么是错的吗?
答案 0 :(得分:19)
您必须像定期使用为RecyclerView创建的任何适配器一样添加布局管理器。
historyRecyclerView.setLayoutManager(new LinearLayoutManager(context));
只需在setAdpater
下方的代码下方添加此行,您可以使用adpapter设置回收站视图。
答案 1 :(得分:10)
您也可以将此添加到xml(感谢@Indra Kumar S)
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
答案 2 :(得分:0)
我的私人变量在这里
private RecyclerView recycleViewtask;
private RecyclerView.LayoutManager mLayoutManager;
然后我分配以下内容
recycleViewtask = findViewById(R.id.recycleViewtask);
recycleViewtask.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
recycleViewtask.setLayoutManager(mLayoutManager);
答案 3 :(得分:0)
对于使用AndroidX的用户,对@Doongsil的解决方案稍作修改:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"