Android 如何修复我的RecyclerView在SearchView筛选结果上的项目点击?

Android 如何修复我的RecyclerView在SearchView筛选结果上的项目点击?,android,mapbox,android-recyclerview,searchview,Android,Mapbox,Android Recyclerview,Searchview,您好,我正在从事一个使用GeoJson的项目,它被存储在一个列表中作为一个功能集合,然后使用它来创建一个RecyclerView,并在Mapbox上单击项目以进行相机定位和绘制路线,它在初始列表中运行良好,但当我尝试实施SearchView过滤时,我被绊倒了,它肯定会过滤列表,但似乎无法正确更新卡的位置,这会给我在过滤结果上的相机定位和路线绘制方法带来麻烦 任何帮助都将不胜感激 以下是在my主要活动上执行搜索和OnItemClick时使用的相关方法: List<Feature> fe

您好,我正在从事一个使用GeoJson的项目,它被存储在一个列表中作为一个功能集合,然后使用它来创建一个RecyclerView,并在Mapbox上单击项目以进行相机定位和绘制路线,它在初始列表中运行良好,但当我尝试实施SearchView过滤时,我被绊倒了,它肯定会过滤列表,但似乎无法正确更新卡的位置,这会给我在过滤结果上的相机定位和路线绘制方法带来麻烦

任何帮助都将不胜感激

以下是在my主要活动上执行搜索和OnItemClick时使用的相关方法:

List<Feature> featureList = featureCollection.features();

            // Retrieve and update the source designated for showing the store location icons
            GeoJsonSource source = mapboxMap.getStyle().getSourceAs("store-location-source-id");
            if (source != null) {
              source.setGeoJson(FeatureCollection.fromFeatures(featureList));
            }

            if (featureList != null) {

              for (int x = 0; x < featureList.size(); x++) {

                Feature singleLocation = featureList.get(x);

                // Get the single location's String properties to place in its map marker
                String singleLocationName = singleLocation.getStringProperty("name");
                String singleLocationHours = singleLocation.getStringProperty("hours");
                String singleLocationDescription = singleLocation.getStringProperty("description");
                String singleLocationPhoneNum = singleLocation.getStringProperty("phone");


                // Add a boolean property to use for adjusting the icon of the selected store location
                singleLocation.addBooleanProperty(PROPERTY_SELECTED, false);

                // Get the single location's LatLng coordinates
                Point singleLocationPosition = (Point) singleLocation.geometry();

                // Create a new LatLng object with the Position object created above
                LatLng singleLocationLatLng = new LatLng(singleLocationPosition.latitude(),
                  singleLocationPosition.longitude());

                // Add the location to the Arraylist of locations for later use in the recyclerview
                listOfIndividualLocations.add(new IndividualLocation(
                  singleLocationName,
                  singleLocationDescription,
                  singleLocationHours,
                  singleLocationPhoneNum,
                  singleLocationLatLng
                ));

                // Call getInformationFromDirectionsApi() to eventually display the location's
                // distance from mocked device location
                getInformationFromDirectionsApi(singleLocationPosition, false, x);
              }
              // Add the fake device location marker to the map. In a real use case scenario,
              // the Maps SDK's LocationComponent can be used to easily display and customize
              // the device location's puck
              addMockDeviceLocationMarkerToMap();

              setUpRecyclerViewOfLocationCards(chosenTheme);

              mapboxMap.addOnMapClickListener(MapActivity.this);

              Toast.makeText(MapActivity.this, "Click on a card", Toast.LENGTH_SHORT).show();

              // Show 3d buildings if the blue theme is being used
              if (customThemeManager.getNavigationLineColor() == R.color.navigationRouteLine_blue) {
                showBuildingExtrusions();
              }
            }
          }

        });

      }
    });


    @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu,menu);

    MenuItem searchitem = menu.findItem(R.id.searchview);
    SearchView searchView = (SearchView) searchitem.getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override
      public boolean onQueryTextSubmit(String query) {
        return false;
      }

      @Override
      public boolean onQueryTextChange(String newText) {
        styleRvAdapter.getFilter().filter(newText);
        return false;
      }
    });
    return true;
  }

    private boolean featureSelectStatus(int index) {
    if (featureCollection == null) {
      return false;
    }
    return featureCollection.features().get(index).getBooleanProperty(PROPERTY_SELECTED);
  }

private void setSelected(int index) {
    Feature feature = featureCollection.features().get(index);
    setFeatureSelectState(feature, true);
    refreshSource();
  }
 private void setFeatureSelectState(Feature feature, boolean selectedState) {
    feature.properties().addProperty(PROPERTY_SELECTED, selectedState);
    refreshSource();
  }


private void refreshSource() {
    GeoJsonSource source = mapboxMap.getStyle().getSourceAs("store-location-source-id");
    if (source != null && featureCollection != null) {
      source.setGeoJson(featureCollection);
    }
  }

private void setUpRecyclerViewOfLocationCards(int chosenTheme) {
    // Initialize the recyclerview of location cards and a custom class for automatic card scrolling
    locationsRecyclerView = findViewById(R.id.map_layout_rv);
    locationsRecyclerView.setHasFixedSize(true);
    locationsRecyclerView.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(this));
    styleRvAdapter = new LocationRecyclerViewAdapter(listOfIndividualLocations,
      getApplicationContext(), this, chosenTheme);
    locationsRecyclerView.setAdapter(styleRvAdapter);
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(locationsRecyclerView);
  }



    @Override
  public void onItemClick(int position) {
    // Get the selected individual location via its card's position in the recyclerview of cards
    IndividualLocation selectedLocation = styleRvAdapter.getItem(position);
    // Evaluate each Feature's "select state" to appropriately style the location's icon
    List<Feature> featureList = featureCollection.features();
    assert featureCollection.features() != null;
    Point selectedLocationPoint = (Point) featureCollection.features().get(position).geometry();
    for (int i = 0; i < featureList.size(); i++) {
      if (featureList.get(i).getStringProperty("name").equals(selectedLocation.getName())) {
        if (featureSelectStatus(i)) {
          setFeatureSelectState(featureList.get(i), false);
        } else {
          setSelected(i);
        }
      } else {
        setFeatureSelectState(featureList.get(i), false);
      }
    }

    // Reposition the map camera target to the selected marker
    if (selectedLocation != null) {
      repositionMapCamera(selectedLocationPoint);
    }

    // Check for an internet connection before making the call to Mapbox Directions API
    if (deviceHasInternetConnection()) {
      // Start call to the Mapbox Directions API
      if (selectedLocation != null) {
        getInformationFromDirectionsApi(selectedLocationPoint, true, null);
      }
    } else {
      Toast.makeText(this, R.string.no_internet_message, Toast.LENGTH_LONG).show();
    }
  }
List featureList=featureCollection.features();
//检索并更新指定用于显示门店位置图标的源
GeoJsonSource=mapboxMap.getStyle().getSourceAs(“存储位置源id”);
如果(源!=null){
setGeoJson(FeatureCollection.fromFeatures(featureList));
}
如果(featureList!=null){
对于(int x=0;xpublic class LocationRecyclerViewAdapter extends
  RecyclerView.Adapter<LocationRecyclerViewAdapter.ViewHolder> implements Filterable {

  private ArrayList<IndividualLocation> listOfLocations;
  private ArrayList<IndividualLocation> listOfLocationsFull;

  private Context context;
  private int selectedTheme;
  private static ClickListener clickListener;
  private int upperCardSectionColor = 0;
  private LocationRecyclerViewAdapter styleRv;
  private int locationNameColor = 0;
  private int locationAddressColor = 0;
  private int locationPhoneNumColor = 0;
  private int locationPhoneHeaderColor = 0;
  private int locationHoursColor = 0;
  private int locationHoursHeaderColor = 0;
  private int locationDistanceNumColor = 0;
  private int milesAbbreviationColor = 0;

  public LocationRecyclerViewAdapter(List<IndividualLocation> styles,
                                     Context context, ClickListener cardClickListener, int selectedTheme) {
    this.context = context;
    this.listOfLocations = (ArrayList<IndividualLocation>) styles;
    this.selectedTheme = selectedTheme;
    this.clickListener = cardClickListener;
    listOfLocationsFull = new ArrayList<>(listOfLocations);
  }

  public IndividualLocation getItem(int position){
    return listOfLocationsFull.get(position);
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    int singleRvCardToUse = R.layout.single_location_map_view_rv_card;
    View itemView = LayoutInflater.from(parent.getContext()).inflate(singleRvCardToUse, parent, false);
    return new ViewHolder(itemView);
  }

  public interface ClickListener {
    void onItemClick(int position);
  }

  @Override
  public int getItemCount() {
    return listOfLocations.size();
  }

  @Override
  public Filter getFilter(){
    return listOfLocationsFilter;
  }
  private Filter listOfLocationsFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
      List<IndividualLocation> filteredList = new ArrayList<>();
      if(constraint == null || constraint.length() == 0 ){
        filteredList.addAll(listOfLocationsFull);

      }
      else{
        String filterpattern = constraint.toString().toLowerCase().trim();
        for(IndividualLocation item : listOfLocationsFull){
            if(item.getName().toLowerCase().contains(filterpattern)){
              filteredList.add(item);

            }
        }
      }
      FilterResults results = new FilterResults();
      results.values = filteredList;
      return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
      listOfLocations.clear();
      listOfLocations.addAll((List)results.values);
      notifyDataSetChanged();
    }
  };

  @Override
  public void onBindViewHolder(ViewHolder card, int position) {

    IndividualLocation locationCard = listOfLocations.get(position);

    card.nameTextView.setText(locationCard.getName());
    card.addressTextView.setText(locationCard.getAddress());
    card.phoneNumTextView.setText(locationCard.getPhoneNum());
    card.hoursTextView.setText(locationCard.getHours());
    card.distanceNumberTextView.setText(locationCard.getDistance());

    card.constraintUpperColorSection.setBackgroundColor(upperCardSectionColor);
    card.nameTextView.setTextColor(locationNameColor);
    card.phoneNumTextView.setTextColor(locationPhoneNumColor);
    card.hoursTextView.setTextColor(locationHoursColor);
    card.hoursHeaderTextView.setTextColor(locationHoursHeaderColor);
    card.distanceNumberTextView.setTextColor(locationDistanceNumColor);
    card.milesAbbreviationTextView.setTextColor(milesAbbreviationColor);
    card.addressTextView.setTextColor(locationAddressColor);
    card.phoneHeaderTextView.setTextColor(locationPhoneHeaderColor);
  }




  static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView nameTextView;
    TextView addressTextView;
    TextView phoneNumTextView;
    TextView hoursTextView;
    TextView distanceNumberTextView;
    TextView hoursHeaderTextView;
    TextView milesAbbreviationTextView;
    TextView phoneHeaderTextView;
    ConstraintLayout constraintUpperColorSection;
    CardView cardView;
    ImageView backgroundCircleImageView;
    ImageView emojiImageView;
    ViewHolder(final View itemView) {
      super(itemView);
      nameTextView = itemView.findViewById(R.id.location_name_tv);
      addressTextView = itemView.findViewById(R.id.location_description_tv);
      phoneNumTextView = itemView.findViewById(R.id.location_phone_num_tv);
      phoneHeaderTextView = itemView.findViewById(R.id.phone_header_tv);
      hoursTextView = itemView.findViewById(R.id.location_hours_tv);
      backgroundCircleImageView = itemView.findViewById(R.id.background_circle);
      emojiImageView = itemView.findViewById(R.id.emoji);
      constraintUpperColorSection = itemView.findViewById(R.id.constraint_upper_color);
      distanceNumberTextView = itemView.findViewById(R.id.distance_num_tv);
      hoursHeaderTextView = itemView.findViewById(R.id.hours_header_tv);
      milesAbbreviationTextView = itemView.findViewById(R.id.miles_mi_tv);
      cardView = itemView.findViewById(R.id.map_view_location_card);
      cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          clickListener.onItemClick(getAdapterPosition());
        }
      });
    }
      @Override
    public void onClick(View view) {
    }

  }

}