gpt4 book ai didi

android - 如何在 ViewPager 中的 fragment 中过滤 RecyclerView

转载 作者:行者123 更新时间:2023-11-29 02:21:31 25 4
gpt4 key购买 nike

我想使用 SearchView 来过滤我的 RecylerView(RecyclerView),它位于 fragment 内,位于 ViewPager 内,ViewPager 位于自定义布局内,并且此自定义布局包含在主 Activity 布局中。

我已经在 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();
}
};


}

这是 fragment 类:

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>

最佳答案

好吧,我遇到了这个完全相似的问题。我所做的是:首先为您的自定义布局提供 id,即 existing_notes_layout。然后在 onCreateView 内的 fragment 中编写以下代码:

   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;
}
});

关于android - 如何在 ViewPager 中的 fragment 中过滤 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55322501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com