我已经阅读了其他类似主题,并且我使用了所有提示,我猜,但是滚动时我看到了滞后。我不明白我需要做些什么来防止它。我想我错过了一些东西,因为ListView不能很好地滚动。
这是我的代码。我正在使用Picasso来加载图片。我在这做错了什么?
public class CustomAdapter extends ArrayAdapter<CustomObjects>
{
Context context;
int layoutResourceId;
List<CustomObjects> data = null;
public CustomAdapter(Context context, int layoutResourceId, List<Objects> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(layoutResourceId, parent, false);
holder.img = (ImageView) convertView.findViewById(R.id.objectImage);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.field2 = (TextView) convertView.findViewById(R.id.field2);
holder.field3 = (TextView) convertView.findViewById(R.id.field3);
holder.field4 = (TextView) convertView.findViewById(R.id.field4);
holder.field5 = (TextView) convertView.findViewById(R.id.field5);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
CustomObjects customObject = getItem(position);
Picasso.with(getActivity())
.load(customObject.getImgUrl())
.tag("image")
.into(holder.img);
holder.title.setText(customObject.getName());
holder.field2.setText(customObject.getField2());
holder.field3.setText(customObject.getField3());
holder.field4.setText(customObject.getField4());
holder.field5.setText(customObject.getField5());
return convertView;
}
}
ViewHolder:
private static class ViewHolder {
TextView title;
ImageView img;
TextView field1;
TextView field2;
TextView field3;
TextView field4;
}
我也在滚动时停止上传图片:
//Its my ListView (objectsList)
objectsList.setOnScrollListener(new SampleScrollListener(getActivity()));
public class SampleScrollListener implements AbsListView.OnScrollListener {
private final Context context;
public SampleScrollListener(Context context) {
this.context = context;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
final Picasso picasso = Picasso.with(context);
if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_TOUCH_SCROLL) {
picasso.resumeTag("image");
} else {
picasso.pauseTag("image");
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {int lastInScreen = firstVisibleItem + visibleItemCount;
if (lastInScreen == totalItemCount) {
if(isUploaded == false && isLoading==false) {
isLoading = true;
// Async loading of objects
}
}
}
}
我的ListView:
<ListView
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:paddingRight="0dp"
android:paddingLeft="0dp"
android:fastScrollEnabled="false"
android:layout_marginTop="0dp"
android:id="@+id/objectsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:footerDividersEnabled="false"
android:scrollingCache="false"
android:animationCache="false"
android:smoothScrollbar="true"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:cacheColorHint="#00000000"
android:clipToPadding="false"
android:overScrollFooter="@android:color/transparent"/>
答案 0 :(得分:0)
我建议您使用Glide:从Picasso
切换是微不足道的,Glide
以更好的方式管理内存。滚动没有更多的滞后。
答案 1 :(得分:0)
问题解决了。我刚刚将手机与USB断开连接。我不知道问题的原因。