我想使用SearchView过滤我的RecylerView(RecyclerView),它位于片段内,位于ViewPager中,ViewPager位于自定义布局中,并且此自定义布局包含在主活动布局中。
我已经在RecyclerView适配器中实现了过滤逻辑。
我想知道的是在哪里调用filter方法,以及如何获得对RecyclerView的引用。
这是RecyclerView的适配器类:
public class cardAdapter extends RecyclerView.Adapter<cardAdapter.noteHolder> implements Filterable {
private List<singleNoteDetail> myNotesList;
private List<singleNoteDetail> myNotesListFull;
private OnItemClickListenerMyNotes listener;
public static class noteHolder extends RecyclerView.ViewHolder {
protected static TextView titleField;
protected static TextView dateField;
public noteHolder(View v) {
super(v);
titleField = v.findViewById(R.id.singleNoteTitle);
dateField = v.findViewById(R.id.singleNoteDate);
}
public void bind(final singleNoteDetail item, final OnItemClickListenerMyNotes listener) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
}
public cardAdapter(List<singleNoteDetail> myNotesList,OnItemClickListenerMyNotes listener) {
this.myNotesList = myNotesList;
this.myNotesListFull = new ArrayList<>(myNotesList);
this.listener = listener;
}
@NonNull
@Override
public noteHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.single_note_card, viewGroup, false);
return new noteHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull noteHolder viewHolder, int i) {
singleNoteDetail singleNoteDetail = myNotesList.get(i);
noteHolder.titleField.setText(singleNoteDetail.title);
noteHolder.dateField.setText(singleNoteDetail.date);
viewHolder.bind(myNotesList.get(i), listener);
}
@Override
public int getItemCount() {
return myNotesList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public Filter getFilter() {
return filter;
}
private Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<singleNoteDetail> filteredList = new ArrayList<>();
if(constraint == null || constraint.length() == 0){
filteredList.addAll(myNotesListFull);
}else{
String pattern = constraint.toString().toLowerCase().trim();
for(singleNoteDetail item : myNotesListFull){
if(item.getTitle().toLowerCase().contains(pattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
myNotesList.clear();
myNotesList.addAll((List)results.values);
notifyDataSetChanged();
}
};
}
这是片段类:
public class MyNotes extends Fragment {
SharedPreferences prefs;
RecyclerView recyclerView;
SearchView searchView;
public RecyclerView.LayoutManager layoutManager;
public List<singleNoteDetail> cardList;
public RecyclerView.Adapter adapter;
ProgressBar progressBar;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public MyNotes() {
// Required empty public constructor
}
public void syncMyNotes(){
//
}
public static MyNotes newInstance(String param1, String param2) {
MyNotes fragment = new MyNotes();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
prefs = getActivity().getSharedPreferences("Username",Context.MODE_PRIVATE);
View view = inflater.inflate(R.layout.fragment_my_notes, container, false);
recyclerView = view.findViewById(R.id.myNotesList);
progressBar = view.findViewById(R.id.progress);
layoutManager = new LinearLayoutManager( getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
syncMyNotes();
adapter = new cardAdapter(cardList, new OnItemClickListenerMyNotes() {
@Override
public void onItemClick(singleNoteDetail item) {
Intent i = new Intent(getActivity(),NoteEditingActivity.class);
i.putExtra("noteTitle",item.title);
startActivity(i);
}
});
recyclerView.setAdapter(adapter);
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onAttachFragment(Fragment fragment) {
// TODO Auto-generated method stub
super.onAttachFragment(fragment);
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(fragment.getId()), Toast.LENGTH_SHORT).show();
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这是包含SearchView和ViewPager的布局
existing_notes_layout.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/customborder"
android:orientation="vertical"
android:gravity="center_horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.design.widget.TabLayout>
<SearchView
android:id="@+id/searchNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</SearchView>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
答案 0 :(得分:0)
因为您已经在片段中包含了搜索视图。然后您就可以在查询文本更改时提交过滤器并提交。这样做的示例方法如下:
public class MyNotes extends Fragment implements SearchView.OnQueryTextListener {
.........
..........
........
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// If you have not instantiated your adapter then you must instantiate it
CardAdapter adapter = new cardAdapter(cardList, new OnItemClickListenerMyNotes() {
@Override
public void onItemClick(singleNoteDetail item) {
Intent i = new Intent(getActivity(),NoteEditingActivity.class);
i.putExtra("noteTitle",item.title);
startActivity(i);
}
});
// If you have already instantiated it you can start with this
if (adapter != null && newText != null) {
adapter.getFilter().filter(newText);
return true;
}
return false;
}
}
答案 1 :(得分:0)
以后无论谁来,这里都是这种场景的解决方案。
更好的方法是使用 EventBus。这在我们必须将某些数据从 Activity 传递到 Fragment 的场景中非常有用。
为了使用EventBus,添加这个库实现'org.greenrobot:eventbus:3.0.0',然后你需要定义一个POJO类,有点像这样:
class SearchQueryEvent {
String query;
public SearchQueryEvent(String query) {
this.query=query;
}
public String getQuery() {
return query;
}
}
现在,您的代码应该像这样修改:
MainActivity.java
SearchView searchView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
EventBus.getDefault().post(new SearchQueryEvent(s));
return false;
}
});
return true;
}
YourFragment.java
@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onSearchQuery(SearchQueryEvent event) {
String query=event.getQuery();
adapter.getFilter().filter(query);
}
答案 2 :(得分:0)
好吧,我遇到了完全类似的问题。我所做的是: 首先为您的自定义布局提供 id,即 existing_notes_layout。然后在 onCreateView 中的片段中编写以下代码:
View view1 = getActivity().findViewById(R.id.activity); where "activity" is the id given to the customlayout i.e. existing_notes_layout.Then, find the id for the searchview you defined in your customlayout. like,
SearchView searchview = view1.findViewById(R.id.searchNotes);
那么,最后就可以实现searchview的以下方法了。
searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
cardAdapter.getFilter().filter(newText);
return false;
}
});