使用 SearchView 过滤 RecyclerView
Filter RecyclerView with SearchView
我使用 SearchView 过滤我的应用程序的 RecyclerView,与 post 中描述的完全一样:
一开始一切看起来都很好,我可以根据需要过滤 RecyclerView,但经过一番尝试后,出现了两个问题:
- 如果我在 SearchView 中输入文本的速度太快,应用程序会崩溃并显示以下日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.scherrer.robin.chvote, PID: 28406
java.lang.NullPointerException: Attempt to invoke virtual method
'android.view.ViewGroup$LayoutParams
android.view.View.getLayoutParams()' on a null object reference
at
com.tonicartos.superslim.LayoutManager.getAnchorAtEnd(LayoutManager.java:1038)
at
com.tonicartos.superslim.LayoutManager.fillNextSectionToEnd(LayoutManager.java:725)
at
com.tonicartos.superslim.LayoutManager.layoutChildren(LayoutManager.java:1369)
at
com.tonicartos.superslim.LayoutManager.onLayoutChildren(LayoutManager.java:276)
at
android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at
android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at
android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at
android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122)
at
android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at
android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1192)
at
android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at
com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.jav
一开始我以为是filtermethod耗时太长,还没执行完就又执行了,但是写日志发现不是这个问题。
- 当我过滤某些内容并向下滚动时,应用程序崩溃并显示以下日志:
FATAL EXCEPTION: main
Process: com.scherrer.robin.chvote, PID: 473
java.lang.IndexOutOfBoundsException: Invalid item position 145(145). Item count:20
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4622)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at com.tonicartos.superslim.LayoutState.getView(LayoutState.java:48)
at com.tonicartos.superslim.LayoutManager.getHeaderOrFirstViewForSection(LayoutManager.java:1242)
at com.tonicartos.superslim.LayoutManager.fillToEnd(LayoutManager.java:840)
at com.tonicartos.superslim.LayoutManager.fillUntil(LayoutManager.java:908)
at com.tonicartos.superslim.LayoutManager.scrollVerticallyBy(LayoutManager.java:361)
at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1529)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2486)
at android.view.View.dispatchTouchEvent(View.java:9294)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9514)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.View
为了使 RecyclerView 中的 header 项具有粘性,我使用了一个名为 Super SLiM
的库
适配器:
public class RfrdAdapter extends RecyclerView.Adapter<RfrdViewHolder> {
private final LayoutInflater mInflater;
private final List<AdapterBaseRow> mModels;
private static final int VIEW_TYPE_HEADER = 0x01;
private static final int VIEW_TYPE_RFRD_OPEN = 0x02;
private static final int VIEW_TYPE_RFRD_PAST = 0x03;
public RfrdAdapter(Context context, List<AdapterBaseRow> models) {
this.mInflater = LayoutInflater.from(context);
this.mModels = new ArrayList<>(models);
}
@Override
public RfrdViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = null;
switch (viewType) {
case VIEW_TYPE_HEADER:
itemView = mInflater.inflate(R.layout.rfrd_list_title, parent, false);
break;
case VIEW_TYPE_RFRD_OPEN:
itemView = mInflater.inflate(R.layout.open_referendum, parent, false);
break;
case VIEW_TYPE_RFRD_PAST:
itemView = mInflater.inflate(R.layout.past_referendum, parent, false);
break;
default:
Log.e("CHVote", "viewType not specified");
break;
}
return new RfrdViewHolder(itemView);
}
@Override
public int getItemViewType(int position) {
int itemViewType = 0;
if (mModels.get(position) instanceof AdapterRowHeader) {
itemViewType = VIEW_TYPE_HEADER;
} else if (mModels.get(position) instanceof AdapterRowRfrdPast) {
itemViewType = VIEW_TYPE_RFRD_PAST;
} else if (mModels.get(position) instanceof AdapterRowRfrdOpen) {
itemViewType = VIEW_TYPE_RFRD_OPEN;
}
return itemViewType;
}
@Override
public void onBindViewHolder(RfrdViewHolder holder, int position) {
final AdapterBaseRow model = mModels.get(position);
final View itemView = holder.itemView;
final LayoutManager.LayoutParams params;
holder.bind(model);
params = (LayoutManager.LayoutParams) itemView.getLayoutParams();
params.setSlm(LinearSLM.ID);
params.setFirstPosition(model.sectionFirstPosition);
itemView.setLayoutParams(params);
}
@Override
public int getItemCount() {
return mModels.size();
}
public void animateTo(List<AdapterBaseRow> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
Log.d("tag", "Finshed filtering");
}
private void applyAndAnimateRemovals(List<AdapterBaseRow> newModels) {
for (int i = mModels.size() - 1; i >= 0; i--) {
final AdapterBaseRow model = mModels.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<AdapterBaseRow> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final AdapterBaseRow model = newModels.get(i);
if (!mModels.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<AdapterBaseRow> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final AdapterBaseRow model = newModels.get(toPosition);
final int fromPosition = mModels.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public AdapterBaseRow removeItem(int position) {
final AdapterBaseRow model = mModels.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, AdapterBaseRow model) {
mModels.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final AdapterBaseRow model = mModels.remove(fromPosition);
mModels.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
}
带有 RecyclerView 的片段:
public class MainFragment extends Fragment implements SearchView.OnQueryTextListener {
public static MainFragment newInstance() {
return new MainFragment();
}
private RecyclerView mRecyclerView;
private RfrdAdapter mAdapter;
private List<AdapterBaseRow> mModels;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.lvRef);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
this.mRecyclerView.setLayoutManager(new LayoutManager(getActivity()));
AdapterData adapterData = new AdapterData();
this.mModels = adapterData.getRfrdData();
mAdapter = new RfrdAdapter(getActivity(), mModels);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextChange(String query) {
final List<AdapterBaseRow> filteredModelList = filter(mModels, query);
Log.d("tag","Started filtering");
mAdapter.animateTo(filteredModelList);
mRecyclerView.scrollToPosition(0);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<AdapterBaseRow> filter(List<AdapterBaseRow> models, String query) {
final List<AdapterBaseRow> filteredModelList = new ArrayList<>();
final ArrayList<String> searchableTxt = new ArrayList<String>();
query = query.toLowerCase();
for (AdapterBaseRow model : models) {
if (model instanceof AdapterRowHeader) {
final String searchTxtVotingDateHeader = ((AdapterRowHeader) model).getVotingDateTxt().toLowerCase();
searchableTxt.add(searchTxtVotingDateHeader);
} else if (model instanceof AdapterRowRfrdOpen) {
final String searchTxtTitleRfrdOpen = ((AdapterRowRfrdOpen) model).getOpenRfrd().getTitle().toLowerCase();
searchableTxt.add(searchTxtTitleRfrdOpen);
final String searchTxtVotingDateRfrdOpen = ((AdapterRowRfrdOpen) model).getOpenRfrd().getVotingDateTxt().toLowerCase();
searchableTxt.add(searchTxtVotingDateRfrdOpen);
} else if (model instanceof AdapterRowRfrdPast) {
final String searchTxtTitleRfrdPast = ((AdapterRowRfrdPast) model).getPastRfrd().getTitle().toLowerCase();
searchableTxt.add(searchTxtTitleRfrdPast);
final String searchTxtVotingDateRfrdPast = ((AdapterRowRfrdPast) model).getPastRfrd().getVotingDateTxt().toLowerCase();
searchableTxt.add(searchTxtVotingDateRfrdPast);
}
for (String sTxt : searchableTxt) {
if (sTxt.contains(query)) {
filteredModelList.add(model);
break;
}
}
searchableTxt.clear();
}
return filteredModelList;
}
}
我在这里查看了很多线程并尝试了很多不同的组合,但似乎无法解决这个问题。
非常感谢任何帮助。
我有一个带有 superslim 的 recyclerview 并尝试放置一个搜索视图(我在片段中使用了一个 edittext up recyclerview)
我的过滤结果有很多问题,headers 中有错误,等等。最后,我创建了一个 non-elegant 解决方案,但工作正常且易于实施:
首先,你必须让你的 recyclerview 使用你的适配器和 superslim 工作(你可以看到所有项目都与你的 headers sticky)
其次,我的解决方案是使用过滤结果重新创建数据(数组)并使用一些逻辑重建适配器:
//event on write in filter field
buscador.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
//get the word...
String text = buscador.getText().toString().toLowerCase(Locale.getDefault());
if(text.isEmpty()){
//if text is empty we load all results (no filter results)
actividades.clear();
actividades.addAll(actividadescopia); //actividadescopia is a clone of actividades(data for populate recyclerview, and all results without filtered)
} else{
//if no empty (searching a word)...
//we create a array for results finds...
ArrayList<ActividadBean> result = new ArrayList<>();
text = text.toLowerCase();
for(ActividadBean item: actividadescopia){
if(item.getTitulo()!=null) {
if (item.getTitulo().toLowerCase().contains(text)) {
//...add to results if find coincidence
result.add(item);
}
}
}
//populate array with the results
actividades.clear();
actividades.addAll(result);
}
if(actividades.size()>0) { //if we have results...
//...inicialize adapter and set in recyclerview
mAdapter = new ListaBusquedaAdapter(getActivity(), mHeaderDisplay, actividades);
mRecyclerView.setAdapter(mAdapter);
//...and set recyclerview visible for see results (see below)
mRecyclerView.setVisibility(View.VISIBLE);
}else{
//..if we haven't results we hide the recyclerview for show that nothing is finding with filtered word
mRecyclerView.setVisibility(View.GONE);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
希望对你有帮助
(抱歉我不是英语母语)
经过一些测试,我发现问题是 superSLiM 库需要自定义 LayoutManager,但过滤仅适用于 LinearLayoutManager。
有人知道如何转换 LayoutManager 吗?
我使用 SearchView 过滤我的应用程序的 RecyclerView,与 post 中描述的完全一样:
一开始一切看起来都很好,我可以根据需要过滤 RecyclerView,但经过一番尝试后,出现了两个问题:
- 如果我在 SearchView 中输入文本的速度太快,应用程序会崩溃并显示以下日志:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.scherrer.robin.chvote, PID: 28406 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference at com.tonicartos.superslim.LayoutManager.getAnchorAtEnd(LayoutManager.java:1038) at com.tonicartos.superslim.LayoutManager.fillNextSectionToEnd(LayoutManager.java:725) at com.tonicartos.superslim.LayoutManager.layoutChildren(LayoutManager.java:1369) at com.tonicartos.superslim.LayoutManager.onLayoutChildren(LayoutManager.java:276) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1192) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.jav
一开始我以为是filtermethod耗时太长,还没执行完就又执行了,但是写日志发现不是这个问题。
- 当我过滤某些内容并向下滚动时,应用程序崩溃并显示以下日志:
FATAL EXCEPTION: main Process: com.scherrer.robin.chvote, PID: 473 java.lang.IndexOutOfBoundsException: Invalid item position 145(145). Item count:20 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4622) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at com.tonicartos.superslim.LayoutState.getView(LayoutState.java:48) at com.tonicartos.superslim.LayoutManager.getHeaderOrFirstViewForSection(LayoutManager.java:1242) at com.tonicartos.superslim.LayoutManager.fillToEnd(LayoutManager.java:840) at com.tonicartos.superslim.LayoutManager.fillUntil(LayoutManager.java:908) at com.tonicartos.superslim.LayoutManager.scrollVerticallyBy(LayoutManager.java:361) at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1529) at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2486) at android.view.View.dispatchTouchEvent(View.java:9294) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737) at android.app.Activity.dispatchTouchEvent(Activity.java:2765) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364) at android.view.View.dispatchPointerEvent(View.java:9514) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896) at android.view.View
为了使 RecyclerView 中的 header 项具有粘性,我使用了一个名为 Super SLiM
的库适配器:
public class RfrdAdapter extends RecyclerView.Adapter<RfrdViewHolder> {
private final LayoutInflater mInflater;
private final List<AdapterBaseRow> mModels;
private static final int VIEW_TYPE_HEADER = 0x01;
private static final int VIEW_TYPE_RFRD_OPEN = 0x02;
private static final int VIEW_TYPE_RFRD_PAST = 0x03;
public RfrdAdapter(Context context, List<AdapterBaseRow> models) {
this.mInflater = LayoutInflater.from(context);
this.mModels = new ArrayList<>(models);
}
@Override
public RfrdViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = null;
switch (viewType) {
case VIEW_TYPE_HEADER:
itemView = mInflater.inflate(R.layout.rfrd_list_title, parent, false);
break;
case VIEW_TYPE_RFRD_OPEN:
itemView = mInflater.inflate(R.layout.open_referendum, parent, false);
break;
case VIEW_TYPE_RFRD_PAST:
itemView = mInflater.inflate(R.layout.past_referendum, parent, false);
break;
default:
Log.e("CHVote", "viewType not specified");
break;
}
return new RfrdViewHolder(itemView);
}
@Override
public int getItemViewType(int position) {
int itemViewType = 0;
if (mModels.get(position) instanceof AdapterRowHeader) {
itemViewType = VIEW_TYPE_HEADER;
} else if (mModels.get(position) instanceof AdapterRowRfrdPast) {
itemViewType = VIEW_TYPE_RFRD_PAST;
} else if (mModels.get(position) instanceof AdapterRowRfrdOpen) {
itemViewType = VIEW_TYPE_RFRD_OPEN;
}
return itemViewType;
}
@Override
public void onBindViewHolder(RfrdViewHolder holder, int position) {
final AdapterBaseRow model = mModels.get(position);
final View itemView = holder.itemView;
final LayoutManager.LayoutParams params;
holder.bind(model);
params = (LayoutManager.LayoutParams) itemView.getLayoutParams();
params.setSlm(LinearSLM.ID);
params.setFirstPosition(model.sectionFirstPosition);
itemView.setLayoutParams(params);
}
@Override
public int getItemCount() {
return mModels.size();
}
public void animateTo(List<AdapterBaseRow> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
Log.d("tag", "Finshed filtering");
}
private void applyAndAnimateRemovals(List<AdapterBaseRow> newModels) {
for (int i = mModels.size() - 1; i >= 0; i--) {
final AdapterBaseRow model = mModels.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<AdapterBaseRow> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final AdapterBaseRow model = newModels.get(i);
if (!mModels.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<AdapterBaseRow> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final AdapterBaseRow model = newModels.get(toPosition);
final int fromPosition = mModels.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public AdapterBaseRow removeItem(int position) {
final AdapterBaseRow model = mModels.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, AdapterBaseRow model) {
mModels.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final AdapterBaseRow model = mModels.remove(fromPosition);
mModels.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
}
带有 RecyclerView 的片段:
public class MainFragment extends Fragment implements SearchView.OnQueryTextListener {
public static MainFragment newInstance() {
return new MainFragment();
}
private RecyclerView mRecyclerView;
private RfrdAdapter mAdapter;
private List<AdapterBaseRow> mModels;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.lvRef);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
this.mRecyclerView.setLayoutManager(new LayoutManager(getActivity()));
AdapterData adapterData = new AdapterData();
this.mModels = adapterData.getRfrdData();
mAdapter = new RfrdAdapter(getActivity(), mModels);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextChange(String query) {
final List<AdapterBaseRow> filteredModelList = filter(mModels, query);
Log.d("tag","Started filtering");
mAdapter.animateTo(filteredModelList);
mRecyclerView.scrollToPosition(0);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<AdapterBaseRow> filter(List<AdapterBaseRow> models, String query) {
final List<AdapterBaseRow> filteredModelList = new ArrayList<>();
final ArrayList<String> searchableTxt = new ArrayList<String>();
query = query.toLowerCase();
for (AdapterBaseRow model : models) {
if (model instanceof AdapterRowHeader) {
final String searchTxtVotingDateHeader = ((AdapterRowHeader) model).getVotingDateTxt().toLowerCase();
searchableTxt.add(searchTxtVotingDateHeader);
} else if (model instanceof AdapterRowRfrdOpen) {
final String searchTxtTitleRfrdOpen = ((AdapterRowRfrdOpen) model).getOpenRfrd().getTitle().toLowerCase();
searchableTxt.add(searchTxtTitleRfrdOpen);
final String searchTxtVotingDateRfrdOpen = ((AdapterRowRfrdOpen) model).getOpenRfrd().getVotingDateTxt().toLowerCase();
searchableTxt.add(searchTxtVotingDateRfrdOpen);
} else if (model instanceof AdapterRowRfrdPast) {
final String searchTxtTitleRfrdPast = ((AdapterRowRfrdPast) model).getPastRfrd().getTitle().toLowerCase();
searchableTxt.add(searchTxtTitleRfrdPast);
final String searchTxtVotingDateRfrdPast = ((AdapterRowRfrdPast) model).getPastRfrd().getVotingDateTxt().toLowerCase();
searchableTxt.add(searchTxtVotingDateRfrdPast);
}
for (String sTxt : searchableTxt) {
if (sTxt.contains(query)) {
filteredModelList.add(model);
break;
}
}
searchableTxt.clear();
}
return filteredModelList;
}
}
我在这里查看了很多线程并尝试了很多不同的组合,但似乎无法解决这个问题。
非常感谢任何帮助。
我有一个带有 superslim 的 recyclerview 并尝试放置一个搜索视图(我在片段中使用了一个 edittext up recyclerview)
我的过滤结果有很多问题,headers 中有错误,等等。最后,我创建了一个 non-elegant 解决方案,但工作正常且易于实施:
首先,你必须让你的 recyclerview 使用你的适配器和 superslim 工作(你可以看到所有项目都与你的 headers sticky)
其次,我的解决方案是使用过滤结果重新创建数据(数组)并使用一些逻辑重建适配器:
//event on write in filter field
buscador.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
//get the word...
String text = buscador.getText().toString().toLowerCase(Locale.getDefault());
if(text.isEmpty()){
//if text is empty we load all results (no filter results)
actividades.clear();
actividades.addAll(actividadescopia); //actividadescopia is a clone of actividades(data for populate recyclerview, and all results without filtered)
} else{
//if no empty (searching a word)...
//we create a array for results finds...
ArrayList<ActividadBean> result = new ArrayList<>();
text = text.toLowerCase();
for(ActividadBean item: actividadescopia){
if(item.getTitulo()!=null) {
if (item.getTitulo().toLowerCase().contains(text)) {
//...add to results if find coincidence
result.add(item);
}
}
}
//populate array with the results
actividades.clear();
actividades.addAll(result);
}
if(actividades.size()>0) { //if we have results...
//...inicialize adapter and set in recyclerview
mAdapter = new ListaBusquedaAdapter(getActivity(), mHeaderDisplay, actividades);
mRecyclerView.setAdapter(mAdapter);
//...and set recyclerview visible for see results (see below)
mRecyclerView.setVisibility(View.VISIBLE);
}else{
//..if we haven't results we hide the recyclerview for show that nothing is finding with filtered word
mRecyclerView.setVisibility(View.GONE);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
希望对你有帮助 (抱歉我不是英语母语)
经过一些测试,我发现问题是 superSLiM 库需要自定义 LayoutManager,但过滤仅适用于 LinearLayoutManager。
有人知道如何转换 LayoutManager 吗?