2014-12-10 1 views
4

목록보기가 있습니다. 각 목록 항목 내에지도를 추가하고 싶습니다. 목록 항목을 클릭하면지도가 표시되거나 숨겨집니다. 지도가 표시되면 확대/축소하고 위치 세부 정보를 볼 수 있습니다. 하지만 어댑터에서 MapFragment를 설정할 수는 없습니다. 그래서, 해결책을 좀주세요. 고맙습니다.어댑터 내의 목록보기 항목 내에 mapfragment를 추가하는 방법은 무엇입니까?

gMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
googleMap = gMap.getMap(); 

getView에서 할 수 없습니다.

답변

6

나는 비슷한 문제를 겪었으며 다음 해결책을 생각해 냈습니다. 그건 그렇고, 지금 재생 서비스는 구글 맵 라이트 모드가 있습니다. 이제 당신이 BaseAdapter를 사용하여 ListView에 있다고 가정하자 https://github.com/vinirll/MapListView

, 그래서 당신은 당신의 getView 메소드를 오버라이드 (override) :

당신은 전체 예를 볼 수 있습니다. 이것이 내 getView의 모습입니다.

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) 
     convertView = new CustomItem(mContext,myLocations.get(position)); 

    return convertView; 
} 

여기서 class CustomItem은 내 행을 나타내는 FrameLayout입니다.

public class CustomItem extends FrameLayout { 

public int myGeneratedFrameLayoutId; 

public CustomItem(Context context,Location location) { 
    super(context); 
    myGeneratedFrameLayoutId = 10101010 + location.id; // choose any way you want to generate your view id 

    LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 

    FrameLayout view = (FrameLayout) inflater.inflate(R.layout.my_custom_item,null); 
    FrameLayout frame = new FrameLayout(context); 
    frame.setId(myGeneratedFrameLayoutId); 

    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics()); 
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,height); 
    frame.setLayoutParams(layoutParams); 

    view.addView(frame); 

    GoogleMapOptions options = new GoogleMapOptions(); 
    options.liteMode(true); 
    MapFragment mapFrag = MapFragment.newInstance(options); 

    //Create the the class that implements OnMapReadyCallback and set up your map 
    mapFrag.getMapAsync(new MyMapCallback(location.lat,location.lng)); 

    FragmentManager fm = ((Activity) context).getFragmentManager(); 
    fm.beginTransaction().add(frame.getId(),mapFrag).commit(); 

    addView(view); 
} 
+0

감사합니다. –

+0

부풀린보기가 FrameLayout인데 왜 새 FrameLayout을 만드나요? 조금 두 배가 ... – Glenn85

+0

public static int myGeneratedFrameLayoutId = 10101010; 이것은 매우 못생긴다 –

관련 문제