Below is the code that I am using to draw routes. Whenever the user pauses the workout I want to show this path as pink polyline. I therefore made my own interface named MapHelper
and taken one boolean as isPause
and lastPause
. When the user saves his/her workout I save all route points as array and on typical list I am displaying this maps as feeds and for that I am using lite mode version of Googlemap.
Below is my adapter class
Adapter.java
public class FeedsAdpter extends RecyclerView.Adapter<FeedsAdpter.MyViewHolder> {
private LayoutInflater inflater;
private Context context;
private FeedsFragment feedsFragment;
RoundedTransFormation transFormation;
private ArrayList<FeedsDto> allFeedsList = new ArrayList<FeedsDto>();
public FeedsAdpter(Context context, FeedsFragment feedsFragment) {
this.context = context;
this.feedsFragment = feedsFragment;
transFormation=new RoundedTransFormation(3, 15,Color.WHITE);
inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_feeds, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onViewRecycled(MyViewHolder holder) {
super.onViewRecycled(holder);
if(Validator.isNotNull(holder.mapView.getMap())) {
GoogleMap googleMap = holder.mapView.getMap();
googleMap.clear();
googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
}
Picasso.with(context).cancelRequest(holder.userImg);
holder.userImg.setImageDrawable(null);
holder.likeBtn.setOnClickListener(null);
holder.commentBtn.setOnClickListener(null);
holder.tvPost.setOnClickListener(null);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final FeedsDto feeds = allFeedsList.get(position);
if (Validator.isNotNull(feeds)) {
if (Validator.isNotNull(feeds.getFeeds().getHistoryMap()) && feeds.getFeeds().getHistoryMap().length > 0) {
if (holder.mapView != null) {
holder.mapView.onCreate(null);
holder.mapView.setVisibility(View.VISIBLE);
holder.mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
if (Validator.isNotNull(googleMap)) {
googleMap.clear();
holder.mapView.setClickable(false);
googleMap.getUiSettings().setMapToolbarEnabled(false);
List<HistoryMap> historyMaps = new ArrayList<HistoryMap>();
historyMaps.addAll(Arrays.asList(feeds.getFeeds().getHistoryMap()));
Util.drawRouteIntoMap(historyMaps, googleMap);
}
}
});
}
} else {
holder.mapView.setVisibility(View.GONE);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
feedsFragment=null;
if(Validator.isNotNull(allFeedsList)){
allFeedsList.clear();
}
InputMethodManager methodManager = (InputMethodManager)context. getSystemService(Context.INPUT_METHOD_SERVICE);
try {
Field mNextServedView = methodManager.getClass().getDeclaredField("mNextServedView");
mNextServedView.setAccessible(true);
mNextServedView.set(methodManager, null);
Field mServedView = methodManager.getClass().getDeclaredField("mServedView");
mServedView.setAccessible(true);
mServedView.set(methodManager, null);
Method method = methodManager.getClass().getDeclaredMethod("finishInputLocked");
method.setAccessible(true);
method.invoke(methodManager);
} catch (Throwable e) {
e.printStackTrace();
}
super.onDetachedFromRecyclerView(recyclerView);
}
public static void hideKeyboard(Context context) {
try {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = ((Activity) context).getCurrentFocus();
if (view != null) {
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void addAll(List<FeedsDto> list) {
allFeedsList.addAll(list);
notifyDataSetChanged();
}
public void clear() {
allFeedsList = new ArrayList<FeedsDto>();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return allFeedsList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.img_user_pic)
ImageView userImg;
@Bind(R.id.tv_user_name)
TextView tvUserName;
@Bind(R.id.tv_duration)
TextView tvDuration;
@Bind(R.id.tv_msg)
TextView tvMsg;
@Bind(R.id.workout_map_summary)
MapView mapView;
@Bind(R.id.et_feeds_comment)
EditText etComment;
@Bind(R.id.tv_post)
ImageView tvPost;
@Bind(R.id.img_like_btn)
ImageButton likeBtn;
@Bind(R.id.img_comment_btn)
ImageButton commentBtn;
@Bind(R.id.tv_like_count)
TextView tvLikecount;
@Bind(R.id.tv_comment_count)
TextView tvCommentcount;
@Bind(R.id.feeds_parent)
LinearLayout feedsParent;
public MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
Here is the link to json file that I'm receving Here is the link to model class that is required While drawing polylines it is throwing arrayindexoutofbound exception only in lenovo k5 among the devices I did testing on. I already asked this question on ArrayIndexOutOfBounds on Android polyline draw