Search in sources :

Example 1 with MapMarkersActiveAdapterListener

use of net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter.MapMarkersActiveAdapterListener in project Osmand by osmandapp.

the class MapMarkersActiveFragment method onCreateView.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final MapActivity mapActivity = (MapActivity) getActivity();
    final View mainView = inflater.inflate(R.layout.fragment_map_markers_active, container, false);
    final EmptyStateRecyclerView recyclerView = (EmptyStateRecyclerView) mainView.findViewById(R.id.list);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    adapter = new MapMarkersActiveAdapter(mapActivity);
    final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(mapActivity, adapter));
    touchHelper.attachToRecyclerView(recyclerView);
    adapter.setAdapterListener(new MapMarkersActiveAdapterListener() {

        private int fromPosition;

        private int toPosition;

        @Override
        public void onItemClick(View view) {
            int pos = recyclerView.getChildAdapterPosition(view);
            if (pos == RecyclerView.NO_POSITION) {
                return;
            }
            MapMarker marker = adapter.getItem(pos);
            OsmandApplication app = mapActivity.getMyApplication();
            if (app.getSettings().SELECT_MARKER_ON_SINGLE_TAP.get()) {
                app.getMapMarkersHelper().moveMarkerToTop(marker);
                updateAdapter();
            } else {
                FavouritePoint fav = marker.favouritePoint == null ? app.getFavorites().getVisibleFavByLatLon(marker.point) : marker.favouritePoint;
                if (fav != null) {
                    showMap(marker.point, fav.getPointDescription(), fav);
                    return;
                }
                WptPt pt = marker.wptPt == null ? app.getSelectedGpxHelper().getVisibleWayPointByLatLon(marker.point) : marker.wptPt;
                if (pt != null) {
                    showMap(marker.point, pt.getPointDescription(mapActivity), pt);
                    return;
                }
                Amenity mapObj = mapActivity.getMapLayers().getMapMarkersLayer().getMapObjectByMarker(marker);
                PointDescription desc = mapObj == null ? marker.getPointDescription(mapActivity) : mapActivity.getMapLayers().getPoiMapLayer().getObjectName(mapObj);
                showMap(marker.point, desc, mapObj == null ? marker : mapObj);
            }
        }

        private void showMap(LatLon latLon, PointDescription desc, Object objToShow) {
            mapActivity.getMyApplication().getSettings().setMapLocationToShow(latLon.getLatitude(), latLon.getLongitude(), 15, desc, true, objToShow);
            MapActivity.launchMapActivityMoveToTop(mapActivity);
            ((DialogFragment) getParentFragment()).dismiss();
        }

        @Override
        public void onDragStarted(RecyclerView.ViewHolder holder) {
            compassUpdateAllowed = false;
            fromPosition = holder.getAdapterPosition();
            touchHelper.startDrag(holder);
        }

        @Override
        public void onDragOrSwipeEnded(RecyclerView.ViewHolder holder) {
            compassUpdateAllowed = true;
            toPosition = holder.getAdapterPosition();
            if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) {
                hideSnackbar();
                mapActivity.getMyApplication().getMapMarkersHelper().reorderActiveMarkersIfNeeded();
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onSwipeStarted() {
            compassUpdateAllowed = false;
        }
    });
    final View emptyView = mainView.findViewById(R.id.empty_view);
    ImageView emptyImageView = (ImageView) emptyView.findViewById(R.id.empty_state_image_view);
    if (Build.VERSION.SDK_INT >= 18) {
        emptyImageView.setImageResource(mapActivity.getMyApplication().getSettings().isLightContent() ? R.drawable.ic_empty_state_marker_list_day : R.drawable.ic_empty_state_marker_list_night);
    } else {
        emptyImageView.setVisibility(View.INVISIBLE);
    }
    recyclerView.setEmptyView(emptyView);
    recyclerView.setAdapter(adapter);
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            compassUpdateAllowed = newState == RecyclerView.SCROLL_STATE_IDLE;
        }
    });
    return mainView;
}
Also used : WptPt(net.osmand.plus.GPXUtilities.WptPt) Amenity(net.osmand.data.Amenity) MapMarker(net.osmand.plus.MapMarkersHelper.MapMarker) OsmandApplication(net.osmand.plus.OsmandApplication) FavouritePoint(net.osmand.data.FavouritePoint) EmptyStateRecyclerView(net.osmand.plus.widgets.EmptyStateRecyclerView) MapMarkersActiveAdapterListener(net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter.MapMarkersActiveAdapterListener) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) EmptyStateRecyclerView(net.osmand.plus.widgets.EmptyStateRecyclerView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) FavouritePoint(net.osmand.data.FavouritePoint) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) LatLon(net.osmand.data.LatLon) MapMarkersItemTouchHelperCallback(net.osmand.plus.mapmarkers.adapters.MapMarkersItemTouchHelperCallback) PointDescription(net.osmand.data.PointDescription) EmptyStateRecyclerView(net.osmand.plus.widgets.EmptyStateRecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) MapMarkersActiveAdapter(net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter) ImageView(android.widget.ImageView) MapActivity(net.osmand.plus.activities.MapActivity) Nullable(android.support.annotation.Nullable)

Aggregations

Nullable (android.support.annotation.Nullable)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 Amenity (net.osmand.data.Amenity)1 FavouritePoint (net.osmand.data.FavouritePoint)1 LatLon (net.osmand.data.LatLon)1 PointDescription (net.osmand.data.PointDescription)1 WptPt (net.osmand.plus.GPXUtilities.WptPt)1 MapMarker (net.osmand.plus.MapMarkersHelper.MapMarker)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 MapActivity (net.osmand.plus.activities.MapActivity)1 MapMarkersActiveAdapter (net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter)1 MapMarkersActiveAdapterListener (net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter.MapMarkersActiveAdapterListener)1 MapMarkersItemTouchHelperCallback (net.osmand.plus.mapmarkers.adapters.MapMarkersItemTouchHelperCallback)1 EmptyStateRecyclerView (net.osmand.plus.widgets.EmptyStateRecyclerView)1