content: package com.example.vibemusicplayer.ui.adapter;
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.navigation.NavController;
  import androidx.navigation.Navigation;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.example.vibemusicplayer.R;
  import com.example.vibemusicplayer.ui.model.Song;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
  
      private final List<Song> data;
  
      public SearchAdapter(List<Song> data) {
          this.data = data; // 传入数据源
      }
  
      public static class ViewHolder extends RecyclerView.ViewHolder {
          public TextView name;
  
          public ViewHolder(View itemView) {
              super(itemView);
              name = itemView.findViewById(R.id.item_result);
          }
      }
  
      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search, parent, false);
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
          Song song = data.get(position);
  
          if (song != null) {
              holder.name.setText(song.getName());
  
              // 设置子项的点击事件
              holder.itemView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Context context = v.getContext(); // 获取点击 View 的上下文
  
                      // 收起键盘
                      InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                      inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
  
                      NavController navController = Navigation.findNavController((Activity) context, R.id.nav_host_fragment_content_main);
                      Bundle bundle = new Bundle();
                      bundle.putSerializable("data", new ArrayList<>(data));
                      bundle.putInt("position", position);
                      navController.navigate(R.id.nav_song, bundle);
                  }
              });
          }
      }
  
      @Override
      public int getItemCount() {
          return data != null ? data.size() : 0;
      }
  }
  