简体   繁体   English

RecyclerView 滚动到顶部本身

[英]RecyclerView scrolls to the top itself

Hi I have a RecyclerView which I use to display images in two columns.嗨,我有一个 RecyclerView,用于在两列中显示图像。 ArrayList of movies holds 60 String of URL's for images.So the images are loaded fine into the ImageViews.电影的 ArrayList 包含 60 个图像 URL 字符串。因此图像被很好地加载到 ImageViews 中。 When I scroll down everything works fine and smooth, then I reach bottom and scroll up, for a while it scrolls ok, but then it scrolls to the very top and I actually see all the photos between trying to load.当我向下滚动时,一切正常,然后我到达底部并向上滚动,有一段时间它滚动正常,但随后滚动到最顶部,我实际上看到了尝试加载之间的所有照片。 I looked for this issue, but haven't found a cause of this scrolling issue.我寻找了这个问题,但没有找到这个滚动问题的原因。 But if I have just 20 items in the list everything works great但是如果我在列表中只有 20 个项目,一切都很好

Here's the Adapter's code:这是适配器的代码:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private ArrayList<Movie> movies;
private Context context;
private int newWidth;
private int newHeight;

public CustomAdapter(ArrayList<Movie> movies, Context context) {
    this.movies = movies;
    this.context = context;
    int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    int orientation = context.getResources().getConfiguration().orientation;
    newWidth = screenWidth / (orientation == Configuration.ORIENTATION_LANDSCAPE ? 3 : 2);
    newHeight = (int) (((double) newWidth / 185) * 278);

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_movie, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Picasso.with(context)
            .load(movies.get(position).url)
            .resize(newWidth, newHeight)
            .into(holder.imageView);
}

@Override
public int getItemCount() {
    return movies.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    final ImageView imageView;

    public ViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.pic);
    }
}
}

Thank you谢谢

For me, I've noticed that if you use "match_parent" for some Views (ViewPager with RecyclerViews in my case) instead of constraints for ConstraintsLayout, this issue can occur.对我来说,我注意到如果你对某些视图使用“match_parent”(在我的例子中是带有 RecyclerViews 的 ViewPager)而不是 ConstraintsLayout 的约束,这个问题可能会发生。

I've reported it here .我已经在这里报告

For now, my workaround/fix was to set constraints.现在,我的解决方法/修复是设置约束。

I've found the solution.The problem was that my ImageView didn't have a fixed size, it got it when picture is being downloaded, that's why when I scrolled to top - there happened to be all of the previous ImageViews in one screen with 0 height and without picture, but when the picture is being dowloaded the ImageView size extended and looks like the RecyclerView scrolls to the first of the visible ImageView.我找到了解决方案。问题是我的 ImageView 没有固定大小,它在下载图片时得到了它,这就是为什么当我滚动到顶部时 - 一个屏幕中恰好有所有以前的 ImageView高度为 0 且没有图片,但是当图片被下载时,ImageView 大小会扩展,看起来 RecyclerView 滚动到可见 ImageView 的第一个。

So I added this code to my CustomAdapter所以我将此代码添加到我的 CustomAdapter

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_movie, parent, false);
    ImageView viewById = (ImageView) view.findViewById(R.id.poster);
    viewById.setMinimumWidth(newWidth);
    viewById.setMinimumHeight(newHeight);
    return new ViewHolder(view);
} 

this is how I change my ImageView size to the one I want before the picture actually being downloaded.这就是我如何在实际下载图片之前将我的 ImageView 大小更改为我想要的大小。

Also added a ProgressBar to look better.还添加了一个 ProgressBar 看起来更好。

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.progressBar.setVisibility(View.VISIBLE);

    Picasso.with(context)
            .load(movies.get(position).postersUrl)
            .resize(0, newHeight)
            .noFade()
            .into(holder.imageView, new Callback() {
                @Override
                public void onSuccess() {
                    holder.progressBar.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onError() {

                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套的 Recyclerview 自行滚动 - Nested Recyclerview scrolls by itself NestedScrollView 在 Recyclerview 调整大小时滚动到顶部 - NestedScrollView scrolls to top on Recyclerview resized RecyclerView notifyDataSetChanged 滚动到顶部 position - RecyclerView notifyDataSetChanged scrolls to top position 在nestedscrollview的recyclerview内部的recyclerview的notifyDataChanged上,外部recyclerview滚动到顶部 - On notifyDataChanged of recyclerview inside a recyclerview in a nestedscrollview the outer recyclerview scrolls to top 使用 ItemTouchHelper 拖动项目时 RecyclerView 滚动到顶部 - RecyclerView scrolls to top when dragging item with ItemTouchHelper 隐藏键盘后,RecyclerView滚动到顶部 - RecyclerView scrolls to top after keyboard hides 添加项目后 RecyclerView 滚动到顶部 - RecyclerView scrolls to top after adding items 在顶部插入时,RecyclerView GridLayoutManager向下滚动 - RecyclerView GridLayoutManager scrolls down when inserting at top RecyclerView 在聊天屏幕中的 notifyDataSetChanged 上滚动到顶部 - RecyclerView scrolls to top on notifyDataSetChanged in chat screen 更改列表项布局时 RecyclerView 滚动到顶部 - When changing list item layout RecyclerView scrolls to top
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM