最初,我有一个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。
答案 0 :(得分:1)
不要在适配器内执行任何激烈的操作,而是使用回调方法(例如: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 .()
}
}