我遇到有关 RecyclerViewAdapter 的问题。
我使用fragments
,我有4 fragments
。 3 fragments
工作正常,没有滞后。但是在第4 fragment
次中,我使用了mapview
而我的recyclerView
减慢意味着当我滚动时,不能像以前的fragments
那样平滑滚动。
这是 recyclerViewAdapter
:
public class VenturesAdapter extends RecyclerView.Adapter<VenturesAdapter.MyViewHolder>{
Context mContext;
List<venturesPojo> venturesList=new ArrayList<>();
OnBottomReachedListener onBottomReachedListener;
GoogleMap googleMap1;
public class MyViewHolder extends RecyclerView.ViewHolder
{
TextView title,description,address;
ImageView logo;
Button viewWeb;
private MapView mapView;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
address = (TextView) itemView.findViewById(R.id.address);
logo = (ImageView) itemView.findViewById(R.id.logo);
viewWeb = (Button) itemView.findViewById(R.id.viewWebsites);
mapView = (MapView) itemView.findViewById(R.id.mapView);
description.setMovementMethod(new ScrollingMovementMethod());
description.setNestedScrollingEnabled(true);
description.setVerticalScrollBarEnabled(true);
description.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
}
}
public VenturesAdapter(Context mContext, List<venturesPojo> venturesList) {
this.mContext = mContext;
this.venturesList = venturesList;
}
@Override
public VenturesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_ventures_list, parent, false);
return new VenturesAdapter.MyViewHolder(itemView);
}
public void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener) {
this.onBottomReachedListener = onBottomReachedListener;
}
@Override
public void onBindViewHolder(VenturesAdapter.MyViewHolder holder, int position) {
venturesPojo venturesPojo=venturesList.get(position);
if (position ==venturesList.size() - 2 ){
onBottomReachedListener.onBottomReached(position);
}
holder.mapView.onCreate(null);
holder.mapView.onResume();
final String lat=venturesPojo.getLatitude();
final String lng=venturesPojo.getLongitude();
if(!TextUtils.isEmpty(lat) || !TextUtils.isEmpty(lng)) {
final LatLng target=new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
holder.mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap1=googleMap;
googleMap.addMarker(new MarkerOptions().position(target));
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(target,16));
googleMap.animateCamera(CameraUpdateFactory.zoomIn());
googleMap.animateCamera(CameraUpdateFactory.zoomTo(16), 1500, null);
}
});
}
holder.title.setText(venturesPojo.getAgency_name());
holder.description.setText(Html.escapeHtml(venturesPojo.getDescription()));
holder.address.setText(venturesPojo.getAddress());
if(!TextUtils.isEmpty(Config.agencyLogo+venturesPojo.getLogo().trim()))
{
String logoUrl=Config.agencyLogo+venturesPojo.getLogo().replace(" ","%20");
Log.d("images",logoUrl);
Glide
.with(mContext)
.load(logoUrl)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE))
.into(holder.logo);
}
else
{
holder.logo.setImageResource(R.drawable.thum);
}
}
@Override
public int getItemCount() {
return venturesList.size();
}
@Override
public void onViewRecycled(MyViewHolder holder) {
if(googleMap1!=null)
{
googleMap1.clear();
googleMap1.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}
}
我添加了 onViewRecycled
,因为当我搜索某些成员给出了这个解决方案但是对我不起作用时,仍然滞后。