我的设置涉及一个Googlemap视图,该视图的recyclerview叠加在 MapviewFragment.class 中。每当用户单击itemview(包含我从Firestore查询的数据)时,我都需要重新定位摄像头,以专注于该itemview的后续Google地图标记。
因此,要实现此目的,我要传递 RestaurantPickAdapter.class 所单击项目的位置,并通过索引对照所有标记进行检查,然后重新定位相机。但是,我不知道如何传递接口,甚至不知道如何实例化它。
这是我的 MapviewFragment.class (它有600行代码,这些代码与我已排除的标记和折线有关)。
public class MapviewFragment extends Fragment implements
OnMapReadyCallback,
GoogleMap.OnInfoWindowClickListener,
RestaurantPickupAdapter.RestaurantRecyclerListener,
GoogleMap.OnPolylineClickListener {
// This method below queries firestore and loads the recyclerview
private void listRestaurants(View pickupView) {
restaurantItemsArrayList = new ArrayList<>();
restaurantRecycler = pickupView.findViewById(R.id.restaurant_pickup_recycler);
restaurantRecycler.setHasFixedSize(true);
restaurantRecycler.setLayoutManager(new LinearLayoutManager(mapviewContext, LinearLayoutManager.HORIZONTAL, false));
Query restaurantQuery = FirebaseFirestore.getInstance().collection("Restaurant_Data").limit(LOAD_LIMIT);
restaurantQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
restaurantItemsArrayList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
if (task.isSuccessful()) {
RestaurantItems restaurantItems = new RestaurantItems(document.getString("restaurantName"),
document.getString("restaurantDescription"), document.getString("restaurantAddress"),
document.getString("restaurantLogo"), document.getId(), document.getGeoPoint("geoPoint"));
restaurantItemsArrayList.add(restaurantItems);
Log.d(TAG, "menu items:" + restaurantItemsArrayList);
}
}
RestaurantPickupAdapter.RestaurantRecyclerListener restaurantRecyclerListener =
restaurantPickupAdapter = new RestaurantPickupAdapter(mapviewContext, restaurantItemsArrayList, // this is where I need to pass the interface);
restaurantRecycler.setAdapter(restaurantPickupAdapter);
}
// In this method the the camera is focused on the marker (i.e. location of a restaurant) that corresponds to the itemview (i.e. restaurant description)
@Override
public void onRestaurantClicked(int position) {
String restaurantId = restaurantItemsArrayList.get(position).getRestaurantId();
for (String markerRestaurantId : markerRestaurantIdArrayList) {
if (markerRestaurantId.equals(restaurantId)) {
googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(restaurantItemsArrayList.get(position).getGeoPoint().getLatitude() , restaurantItemsArrayList.get(position).getGeoPoint().getLongitude())), 600, null);
break;
}
}
}
这是RestaurantPickupAdapter.class
public class RestaurantPickupAdapter extends RecyclerView.Adapter<RestaurantPickupAdapter.RestaurantViewHolder>{
private Context adapterContext;
private ArrayList<RestaurantItems> restaurantItemsArrayList;
private RestaurantRecyclerListener restaurantRecyclerListener;
public RestaurantPickupAdapter(Context adapterContext, ArrayList<RestaurantItems> restaurantItemsArrayList, RestaurantRecyclerListener restaurantRecyclerListener) {
this.adapterContext = adapterContext;
this.restaurantItemsArrayList = restaurantItemsArrayList;
this.restaurantRecyclerListener = restaurantRecyclerListener;
}
@NonNull
@Override
public RestaurantPickupAdapter.RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(adapterContext);
View view = layoutInflater.inflate(R.layout.item_restaurant_pickup, viewGroup,false);
return new RestaurantViewHolder(view, restaurantRecyclerListener);
}
@Override
public void onBindViewHolder(@NonNull final RestaurantPickupAdapter.RestaurantViewHolder viewHolder, final int i) {
viewHolder.tvRestaurantName.setText(restaurantItemsArrayList.get(i).getRestaurantName());
viewHolder.tvRestaurantAddress.setText(restaurantItemsArrayList.get(i).getRestaurantAddress());
viewHolder.tvRestaurantDescription.setText(restaurantItemsArrayList.get(i).getRestaurantDescription());
Glide.with(adapterContext).load(restaurantItemsArrayList.get(i).getRestaurantLogo()).into(viewHolder.tvRestaurantLogo);
}
@Override
public int getItemCount() {
return restaurantItemsArrayList.size();
}
class RestaurantViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView tvRestaurantName, tvRestaurantAddress, tvRestaurantDescription;
public ImageView tvRestaurantLogo;
RestaurantRecyclerListener restaurantClickListener;
public RestaurantViewHolder(@NonNull View itemView, RestaurantRecyclerListener restaurantRecyclerListener) {
super(itemView);
tvRestaurantName = itemView.findViewById(R.id.restaurant_name);
tvRestaurantAddress = itemView.findViewById(R.id.restaurant_address);
tvRestaurantDescription = itemView.findViewById(R.id.restaurant_description);
tvRestaurantLogo = itemView.findViewById(R.id.restaurant_logo);
restaurantClickListener = restaurantRecyclerListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
restaurantClickListener.onRestaurantClicked(getAdapterPosition());
}
}
public interface RestaurantRecyclerListener{
void onRestaurantClicked(int position);
}
}
我不能简单地传递 this ,因为在Firestore this 的onSuccess / onComplete侦听器中,它引用查询结果。
答案 0 :(得分:0)
这似乎是您想要的:
restaurantPickupAdapter = new RestaurantPickupAdapter(
mapviewContext, restaurantItemsArrayList, MapviewFragment.this);
匿名内部类引用了使用该语法创建的类的实例。