RecyclerView OnClick传递意图长按添加标记到地图(RecyclerView OnClick pass through intent Long click adds marker to

编程入门 行业动态 更新时间:2024-10-27 11:20:56
RecyclerView OnClick传递意图长按添加标记到地图(RecyclerView OnClick pass through intent Long click adds marker to map)

最初,我有一个listview为我做的工作,我会在onCreate Main类中设置clicklistener和long licklistener如下:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(getApplicationContext(), UserFeed.class); i.putExtra("venueName", businessList.get(position).name); i.putExtra("companyId",businessList.get(position).id); i.putExtra("canWrite", checkedIn); i.putExtra("Lat",businessList.get(position).anchorpt.getLatitude()); i.putExtra("Lon",businessList.get(position).anchorpt.getLongitude()); i.putExtra("NotType",venueType); startActivity(i); } }); listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) { Double Lat = businessList.get(position).anchorpt.getLatitude(); Double Lon = businessList.get(position).anchorpt.getLongitude(); ArrayList<Marker> markers = new ArrayList<>(); LatLngBounds.Builder builder = new LatLngBounds.Builder(); mMap.clear(); markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(point.getLatitude(), point.getLongitude())) .icon(BitmapDescriptorFactory.defaultMarker()) .title("My Location") .visible(false))); markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(Lat, Lon)) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) .visible(true))); for (Marker marker : markers){ builder.include(marker.getPosition()); } LatLngBounds bounds = builder.build(); int padding = 100; CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding); mMap.animateCamera(cu); return true; } });

我正在尝试使用recyclerview采用相同的方法,但没有成功。 我可以在recylcerviewadapter中完成,但我不确定如何添加标记。 它看起来像这样:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{ public TextView tvName,tvDistance,tvAgeRange,tvUserCount,tvGenderRatio,tvTimeDetails; public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); //itemView.setOnLongClickListener(this); tvName = (TextView) itemView.findViewById(R.id.venueName); tvDistance = (TextView) itemView.findViewById(R.id.textDistance); tvAgeRange = (TextView) itemView.findViewById(R.id.textAgeRange); tvUserCount = (TextView) itemView.findViewById(R.id.textUserCount); tvGenderRatio = (TextView) itemView.findViewById(R.id.textGenderRatio); tvTimeDetails = (TextView) itemView.findViewById(R.id.whenLitOccurs); } @Override public void onClick(View view) { //context.startActivity(new Intent(context, UserFeed.class)); int row = getAdapterPosition(); Log.i("AppInfo","Item clicked"+ items.get(row)); Intent i = new Intent(context, UserFeed.class); i.putExtra("venueName", items.get(row).name); i.putExtra("companyId",items.get(row).id); //i.putExtra("canWrite", checkedIn); i.putExtra("Lat",items.get(row).anchorpt.getLatitude()); i.putExtra("Lon",items.get(row).anchorpt.getLongitude()); //i.putExtra("NotType",venueType); context.startActivity(i); } @Override public boolean onLongClick(View view) { //Add a marker to map return true; } }

我正在使用谷歌地图v2。

Initially, I had a listview do the job for me, I would set the clicklistener and long licklistener as follows in the onCreate Main class:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(getApplicationContext(), UserFeed.class); i.putExtra("venueName", businessList.get(position).name); i.putExtra("companyId",businessList.get(position).id); i.putExtra("canWrite", checkedIn); i.putExtra("Lat",businessList.get(position).anchorpt.getLatitude()); i.putExtra("Lon",businessList.get(position).anchorpt.getLongitude()); i.putExtra("NotType",venueType); startActivity(i); } }); listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) { Double Lat = businessList.get(position).anchorpt.getLatitude(); Double Lon = businessList.get(position).anchorpt.getLongitude(); ArrayList<Marker> markers = new ArrayList<>(); LatLngBounds.Builder builder = new LatLngBounds.Builder(); mMap.clear(); markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(point.getLatitude(), point.getLongitude())) .icon(BitmapDescriptorFactory.defaultMarker()) .title("My Location") .visible(false))); markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(Lat, Lon)) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)) .visible(true))); for (Marker marker : markers){ builder.include(marker.getPosition()); } LatLngBounds bounds = builder.build(); int padding = 100; CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding); mMap.animateCamera(cu); return true; } });

I'm trying to do the same approach with the recyclerview, with no success. I can do it in the recylcerviewadapter but I'm not sure how I would add a marker. It would look like this:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{ public TextView tvName,tvDistance,tvAgeRange,tvUserCount,tvGenderRatio,tvTimeDetails; public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); //itemView.setOnLongClickListener(this); tvName = (TextView) itemView.findViewById(R.id.venueName); tvDistance = (TextView) itemView.findViewById(R.id.textDistance); tvAgeRange = (TextView) itemView.findViewById(R.id.textAgeRange); tvUserCount = (TextView) itemView.findViewById(R.id.textUserCount); tvGenderRatio = (TextView) itemView.findViewById(R.id.textGenderRatio); tvTimeDetails = (TextView) itemView.findViewById(R.id.whenLitOccurs); } @Override public void onClick(View view) { //context.startActivity(new Intent(context, UserFeed.class)); int row = getAdapterPosition(); Log.i("AppInfo","Item clicked"+ items.get(row)); Intent i = new Intent(context, UserFeed.class); i.putExtra("venueName", items.get(row).name); i.putExtra("companyId",items.get(row).id); //i.putExtra("canWrite", checkedIn); i.putExtra("Lat",items.get(row).anchorpt.getLatitude()); i.putExtra("Lon",items.get(row).anchorpt.getLongitude()); //i.putExtra("NotType",venueType); context.startActivity(i); } @Override public boolean onLongClick(View view) { //Add a marker to map return true; } }

I'm using google maps v2.

最满意答案

不要在适配器内执行任何激烈的操作,而是使用回调方法(例如:interface)将click和longclick事件传递给mainactivity

第1步:为clickListener创建一个接口。

public interface CustomAdapterClickListener { public void onItemClick(View v, int position);}

第2步:在recylcerview的构造函数的帮助下,将MainActivity reffrence分配给接口。

public class RecylcerAdapter extends extends RecyclerView.Adapter<>{ public RecylcerAdapter(Context context, ArrayList<> list, final CustomAdapterClickListener clickListener) { this.context = context; inflator = LayoutInflater.from(context); this.list = list; this.clickListener = clickListener; }

第3步:从适配器的onClicks方法调用此接口方法

@Override public void onClick(View v) { clickListener.onItemClick(v,(int)v.getTag()); }

第4步:在MainActivity中实现接口

public class MaiActivity extends AppCompatActivity implements CustomAdapterClickListener{ @Override public void onItemClick(View v, int position) { // now you have the list item position as well as view , get the adapter data based on the postion // add marker with the help of googlemap instance .() }

}

dont perform any intense operation inside adapter , instead use callback methods (ex : interface) to transfer the click and longclick events to mainactivity

Step 1 : create an interface for clickListener.

public interface CustomAdapterClickListener { public void onItemClick(View v, int position);}

Step 2 : Assign MainActivity reffrence to interface with the help of constructor of recylcerview.

public class RecylcerAdapter extends extends RecyclerView.Adapter<>{ public RecylcerAdapter(Context context, ArrayList<> list, final CustomAdapterClickListener clickListener) { this.context = context; inflator = LayoutInflater.from(context); this.list = list; this.clickListener = clickListener; }

}Step 3 : Call this interface method from onClicks method of adapter

@Override public void onClick(View v) { clickListener.onItemClick(v,(int)v.getTag()); }

Step 4 : Implement the interface in MainActivity

public class MaiActivity extends AppCompatActivity implements CustomAdapterClickListener{ @Override public void onItemClick(View v, int position) { // now you have the list item position as well as view , get the adapter data based on the postion // add marker with the help of googlemap instance .() }

}

更多推荐

本文发布于:2023-07-05 06:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意图   标记   地图   pass   OnClick

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!