以下是我用来绘制路线的代码。每当用户暂停锻炼时,我想将此路径显示为粉红色折线。因此,我创建了自己的名为MapHelper
的接口,并将一个布尔值设为isPause
和lastPause
。当用户保存他/她的锻炼时,我将所有路线点保存为阵列,在典型列表中,我将这些地图显示为饲料,为此我使用的是精简模式版Googlemap。
下面是我的适配器类
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 在绘制折线时,它仅在我测试的设备中的lenovo k5中抛出 arrayindexoutofbound 异常。我已经在ArrayIndexOutOfBounds on Android polyline draw
上提出了这个问题