У меня есть некоторые фрагменты в RecyclerView, который добавляется в контейнер тела MainActivity. У меня есть фрагмент с панелью инструментов и RecyclerView, который добавлен в главный контейнер MainActivity. Я хочу сделать поиск по всем элементам всего RecyclerView и сделать setVisibility(View.GONE)
для контейнера тела и setVisibility(View.VISIBLE)
для головы RecyclerView. Я не понимаю, как сделать поиск.
фрагмента_станций. xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
</RelativeLayout>
Activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container_from_toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
фрагмент_поиск. xml
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/toolbar_main"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewSearch"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
</RelativeLayout>
</LinearLayout>
RecyclerStationAdapter. java (для контейнера тела)
public class RecyclerStationAdapter extends RecyclerView.Adapter<RecyclerStationAdapter.ViewHolder> {
private LayoutInflater mLayoutInflater;
private List<Station> mStations;
private Context mContext;
private static AnimatorHelper animatorHelper;
public RecyclerStationAdapter(Context context, List<Station> stations) {
this.mStations = stations;
this.mLayoutInflater = LayoutInflater.from(context);
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.example_list_item_station, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
// viewHolder.setIsRecyclable(false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Station station = mStations.get(position);
holder.textView.setText(station.getName());
// holder.setIsRecyclable(false);
Glide.with(mContext)
.load(mStations.get(position).getImage())
.error(R.drawable.ic_launcher_foreground)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createFirebaseReport(position);
Log.d("debug", mStations.get(position).getName());
Player player = new Player(mStations.get(position).getStream());
player.start(mContext);
startPlayerService();
if (animatorHelper != null)
animatorHelper.stopAnimation();
animatorHelper = new AnimatorHelper(holder.playViewAnimation);
animatorHelper.startAnimation();
Log.d("anm", String.valueOf(holder.getItemId()));
}
private void startPlayerService() {
Intent serviceIntent = new Intent(mContext, NotificationService.class);
serviceIntent.setAction(Const.ACTION.STARTFOREGROUND_ACTION);
mContext.startService(serviceIntent);
}
});
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private void createFirebaseReport(int position) {
FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(mContext);
Bundle eventDetails = new Bundle();
StringBuilder sb = new StringBuilder();
sb.append(mStations.get(position).getName() + " : " + mStations.get(position).getStream());
eventDetails.putString("station", sb.toString());
firebaseAnalytics.logEvent("select_station", eventDetails);
}
@Override
public int getItemCount() {
return mStations.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
final TextView textView;
final AVLoadingIndicatorView playViewAnimation;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.ivStation);
textView = itemView.findViewById(R.id.tvStation);
playViewAnimation = itemView.findViewById(R.id.playing_anim);
}
}
}