MapView inside ScrollView (activity)
참고Url
리스트뷰안에 맵을 넣는 것을 검색하다가 적용 및 테스트를 해봤습니다.
위의 주소에 있는 코드에서는 ScrollView와 MapView 의 터치 이벤트를 동시에 적용되는 기능도 처리가 되어 있어서
여러모로 쓸모가 많은 코드네요.
fragment_maps.xml 맵뷰의 레이아웃을 정의 합니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.gms.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
WorkaroundMapFragment.java의 클레스를 정의합니다.
import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import com.google.android.gms.maps.SupportMapFragment; public class WorkaroundMapFragment extends SupportMapFragment { private OnTouchListener mListener; @Override public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) { View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance); TouchableWrapper frameLayout = new TouchableWrapper(getActivity()); frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return layout; } public void setListener(OnTouchListener listener) { mListener = listener; } public interface OnTouchListener { public abstract void onTouch(); } public class TouchableWrapper extends FrameLayout { public TouchableWrapper(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if(mListener == null) return false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mListener.onTouch(); break; case MotionEvent.ACTION_UP: mListener.onTouch(); break; } return super.dispatchTouchEvent(event); } } }
코드 적용
Activity layout 에서 ScrollView 안에 정의합니다.
<ScrollView ... <fragment android:tag="fragment_map" android:id="@+id/fragment_map" android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginTop="20dp" class="your package .WorkaroundMapFragment"/> ... </ScrollView>
Activity 내에서의 처리
public class MainActivity extends AppCompatActivity { private GoogleMap mMap; private ScrollView mScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_back); WorkaroundMapFragment mapFrag = (WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map); mapFrag.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { boolean permission = hasAllPermissionsGranted(); if (permission) { Log.e("test","permission "+permission); } return; } googleMap.setMyLocationEnabled(true); googleMap.getUiSettings().setZoomControlsEnabled(true); } }); mScrollView = (ScrollView) findViewById(R.id.sv_container); mapFrag.setListener(new WorkaroundMapFragment.OnTouchListener() { @Override public void onTouch() { mScrollView.requestDisallowInterceptTouchEvent(true); } }); } //권한 추가 private static final int REQUEST_PERMISSIONS = 1; private static final String[] MY_PERMISSIONS = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }; private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private boolean hasAllPermissionsGranted() { for (String permission : MY_PERMISSIONS) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, MY_PERMISSIONS, REQUEST_PERMISSIONS); return false; } } return true; } }
결과 화면
'Android' 카테고리의 다른 글
[안드로이드] ImageView 안에 있는 resource 컬러값 변경 (0) | 2017.04.12 |
---|---|
[안드로이드] ScrollView 안에 지도 달기 (fragment) (0) | 2017.04.11 |
[안드로이드] fragment inside fragment (0) | 2017.04.10 |
[안드로이드] DrawerLayout 열리고 닫힐때 이벤트 (0) | 2017.04.10 |
[안드로이드] gradle - minifyEnabled 와 shrinkResources 에 대해 (0) | 2017.04.08 |