Reputation: 155
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.
Upvotes: 1
Views: 980
Reputation: 1376
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 .()
}
}
Upvotes: 1