我想使用Fresco和recyclerview建立一个画廊。
但是当我notifyDataSetChange()
然后滚动到列表顶部时,我可以看到项目正在移动。与this一样。
我尝试了什么:
我尝试使用recyclerview.setItemAnimator(null)
和holder.pic.setLayoutParams()
解决此问题。他们都没有工作。
我发现an issue与我的相似,但我仍然不知道这个问题。
这是我的密码: MyAdapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
private List<ImageModel> mItems;
Context mContext;
private WindowManager windowManager;
private int itemWidth;
public MyRecyclerAdapter(Context context,List<ImageModel> objects,int gap) {
mContext = context;
mItems = objects;
windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
itemWidth=(windowManager.getDefaultDisplay().getWidth()-gaps*2)/2
}
static class ViewHolder extends RecyclerView.ViewHolder{
private ImageView picture;
private TextView mTextView;
private View rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView;
picture =(ImageView)itemView.findViewById(R.id.image);
mTextView =(TextView)itemView.findViewById(R.id.title);
}
}
@Override
public int getItemCount() {
return mItems.size();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position,Picture picture) {
holder.mTextView.setText(picture.getTitle());
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams)holder.picture.getLayoutParams();
float ratio = picture.getHeight()/picture.getWidth();
int width=itemWidth;
int height=(int)(width * ratio);
params.height = height;
holder.picture.setLayoutParams(params);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(picture.getPath()))
.setResizeOptions(new ResizeOptions(width, height))
.build();
PipelineDraweeController controller = Fresco.newDraweeControllerBuilder()
.setOldController(holder.picture.getController())
.setImageRequest(request)
.build();
holder.picture.setController(controller);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.item, parent, false);
return new ViewHolder(convertView);
}
}
我的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
fresco:actualImageScaleType="fitXY"
fresco:placeholderImage="@mipmap/default_image"
fresco:actualImageScaleType="fitXY"
fresco:roundAsCircle="false"
fresco:roundedCornerRadius="1dp"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundingBorderWidth="2dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="28dp"
android:textSize="14dp"
android:textColor="#ffffff"
android:layout_below="@+id/fresco_image"
android:gravity="center"
android:background="#ffffff"/>
</RelativeLayout>
</RelativeLayout>
以上内容夸大了以下内容:
<android.support.v7.widget.RecyclerView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
</RelativeLayout>
我想获得帮助:
当我滚动到顶部或加载更多时,为什么recyclerview的项目总是会移动?
如何解决这个问题?你能告诉我这里的密码或样品吗?