4

GoogleマップのフラグメントをAndroidのRecyclerViewに追加しようとしています。Android GoogleマップRecyclerViewホルダーの動的フラグメント

私はそれについて読んでいました。重複したIDエラーを避けるために、XMLで行うのではなく、フラグメントを動的に作成する必要があることがわかりました。

public class MapViewHolder extends RecyclerView.ViewHolder { 
    protected FrameLayout mapContainer; 

    public MapViewHolder(View v){ 
     super(v); 
     view = v; 
     mapContainer = (FrameLayout) v.findViewById(R.id.mapContainer); 
    } 
} 

private void bindMapRow(final MapViewHolder holder, int position) { 
     MessageData message = (MessageData) messagesList.get(position); 
     LocationData location = message.getLocation(); 

     FrameLayout view = holder.mapContainer; 
     FrameLayout frame = new FrameLayout(context); 
     if (message.getIdMap() != 0) { 
      frame.setId(message.getIdMap()); 
     } 
     else { 
      message.setIdMap(generateViewId()); 
      frame.setId(message.getIdMap()); 
      messagesList.set(position, message); 
     } 

     int size = context.getResources().getDimensionPixelSize(R.dimen.row_chat_map_size); 
     FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(size, size); 
     frame.setLayoutParams(layoutParams); 

     view.addView(frame); 

     GoogleMapOptions options = new GoogleMapOptions(); 
     options.liteMode(true); 

     SupportMapFragment mapFrag = SupportMapFragment.newInstance(options); 
     mapFrag.getMapAsync(new MapReadyCallback(location)); 

     FragmentManager fm = activity.getSupportFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.replace(frame.getId(), mapFrag); 
     ft.commit(); 
} 

この私はマップが私のRecyclerViewに表示され、すべてがうまく動作しているようだ見ることができるの実行:

は、ここに私のアダプターコードです。 RecyclerViewをしばらくスクロールしてから地図に戻ったときに問題が発生します。

私はアプリがクラッシュすると、このエラーを示していることを実行します。

java.lang.IllegalArgumentException: No view found for id 0x1 (unknown) for fragment SupportMapFragment{606864b #0 id=0x1}

多くの感謝と種類について、 マルセルを。

+0

誰も私を助けることはできませんか? – MarcelPG

答えて

0

bindMapRowonBindViewHolderで呼び出されます。

エラーIllegalArgumentException: No view found for id理由は(したがってビューは、エラーが見つかりません)holder.mapContainerframeonBindViewHolder中まだRecyclerView /アクティビティーに接続されていないことです。

FragmentTransaction.replaceを呼び出す前に、ビューが既にRecyclerView/Activityに添付されていることを保証するには、代わりにonViewAttachedToWindowを聞きます。

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
    ... 
    @Override 
    public void onViewAttachedToWindow(ViewHolder holder) { 
     super.onViewAttachedToWindow(holder); 

     // If you have multiple View Type, check for the correct viewType 
     SupportMapFragment mapFragment = holder.mapFragment; 
     if (mapFragment == null) { 
      mapFragment = SupportMapFragment.newInstance(); 
      mapFragment.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        ... 
       } 
      }); 
     } 

     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit(); 
    } 
} 

https://code.luasoftware.com/tutorials/android/supportmapfragment-in-recyclerview/

関連する問題