この投稿で説明されているように、アプリの RecyclerView を SearchView でフィルター処理します。
SearchView で RecyclerView をフィルタリングする方法
最初はすべて問題ないように見え、必要に応じて RecyclerView をフィルター処理できましたが、いくつか試してみると、次の 2 つの問題が発生しました。
- SearchView へのテキストの入力が速すぎると、アプリがクラッシュし、次のログが出力されます。
E/AndroidRuntime: FATAL EXCEPTION: メイン プロセス: com.scherrer.robin.chvote、PID: 28406 java.lang.NullPointerException: 仮想メソッド 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' を呼び出そうとしていますcom.tonicartos.superslim.LayoutManager.getAnchorAtEnd(LayoutManager.java:1038) の null オブジェクト参照 com.tonicartos.superslim.LayoutManager.fillNextSectionToEnd(LayoutManager.java:725) com.tonicartos.superslim.LayoutManager.com.tonicartos.superslim.LayoutManager.onLayoutChildren(LayoutManager.java:276) の layoutChildren(LayoutManager.java:1369) android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) の android.support.v7 android.view.View.layout(View.java:16630) で android.view.ViewGroup.layout(ViewGroup.java:5437) で android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) で android.widget.FrameLayout.onLayout(FrameLayout.java:273) で android. android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) android.widget.FrameLayout.onLayout(FrameLayout.java:273) で android.view.View.layout(View.java:16630) で android.view.ViewGroup.layout(ViewGroup.java:5437) で android. support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1192) で android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) で android.view.View.layout(View.java:16630) android.view.ViewGroup.layout(ViewGroup.java:5437) で android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) で android.widget.FrameLayout.onLayout(FrameLayout.java:273) android.view.View.layout(View.java:16630) で android.view.ViewGroup.layout(ViewGroup.java:5437) で android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) で android. android.view.View.layout(View.java:16630) で android.view.ViewGroup.layout(ViewGroup.java:5437) で android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) で android.widget.FrameLayout.onLayout(FrameLayout.java:273) で android. android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) で android.widget.LinearLayout.onLayout(LinearLayout.java:1495) で android.view.View.layout(View.java:16630) で android. android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) の view.ViewGroup.layout(ViewGroup.java:5437) android.widget.FrameLayout.onLayout(FrameLayout.java:273) com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) で android.view.View.layout(View.java:16630) で android.view.ViewGroup.layout(ViewGroup.java) :5437) android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) で android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) で android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) で android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) で android.view.Choreographer.doCallbacks(Choreographer.java:670) ) android.view.Choreographer.doFrame(Choreographer.java:606) で android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) で android.os.Handler.android.os.Handler.dispatchMessage(Handler.java:95) の handleCallback(Handler.java:739) android.os.Looper.loop(Looper.java:148) の android.app.ActivityThread.main(ActivityThread.jav)
最初は、filtermethod に時間がかかりすぎて終了前に再実行されるためにエラーが発生するのではないかと思っていましたが、ログに書き込むと、それが問題の原因ではないことがわかりました。
- 何かをフィルタリングして下にスクロールするとすぐに、次のログでアプリがクラッシュします。
致命的な例外: メイン プロセス: com.scherrer.robin.chvote、PID: 473 java.lang.IndexOutOfBoundsException: アイテムの位置 145(145) が無効です。項目数: android.support.v7.widget.RecyclerView$RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4622) で 20 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) で com.tonicartos .superslim.LayoutState.getView(LayoutState.java:
RecyclerView のヘッダー アイテムをスティッキーにするために、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;
}
}
ここで多くのスレッドを見て、さまざまな組み合わせを試しましたが、この問題を乗り越えることができないようです.
どんな助けでも大歓迎です。