我完全迷失了这个错误,我明白了,但我不知道出了什么问题.对于代码:
// 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);
Run Code Online (Sandbox Code Playgroud)
该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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个疯狂的部分,当我更新列表时setHistoryList,我知道nullRecycler视图有正确的适配器,它不是......但它有点失去它,当它试图更新时,在Recycler视图中没有更多的适配器notifyDatasetChanged().它只是消失了,并且,没有显示任何东西.
知道什么是错的吗?
Jai*_*Jai 23
您必须像定期使用为RecyclerView创建的任何适配器一样添加布局管理器.
historyRecyclerView.setLayoutManager(new LinearLayoutManager(context));
Run Code Online (Sandbox Code Playgroud)
只需在下面的代码下方添加以下代码setAdpater,您可以使用adpapter设置回收站视图.
Doo*_*sil 13
您也可以将此添加到xml(感谢@Indra Kumar S)
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
Run Code Online (Sandbox Code Playgroud)
小智 5
对于使用AndroidX的用户,@ Doongsil解决方案的略微修改:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14677 次 |
| 最近记录: |