RecyclerView在滚动之前不会显示项目 [英] RecyclerView doesn't show items until scrolling

查看:343
本文介绍了RecyclerView在滚动之前不会显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经问过几次这个问题,但是这些答案对我而言并不适用.我想对导致此问题的一般原因有一个更笼统的答案.

This question has been asked a few times but those answers doesn't apply to me. I would like a more general answer about what causes this issue generally.

我的活动布局中有一个recyclerview. recyclerview的行是具有一个imageview和textview的约束布局:

I have a recyclerview in my activity layout. Rows of the recyclerview is a constraint layout with one imageview and textview:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:clickable="true">

    <ImageView
        android:id="@+id/file_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:text="File"
        android:id="@+id/file_name"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toRightOf="@+id/file_icon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

在此行结构中,我要显示几个项目.我正确设置了适配器.但是,当我运行我的应用程序时,直到我再次向下滚动并向上滚动时,屏幕上才会显示文本视图.基本上,为了显示这些文本视图,需要将它们从显示中删除并再次输入.这是我的适配器代码:

I have several items to show in this row structure. I set up the adapter properly. However, when I run my application, textviews that are on the screen doesn't get shown, until I scroll-down and scroll-up again. Basically, in order for those textviews to be shown, they need to be discarded from display and enter again. Here is my adapter code:

public class FileViewAdapter extends RecyclerView.Adapter<FileViewAdapter.Viewholder> {

    private Context context;
    private List<File> files;

    public FileViewAdapter(Context context, List<File> files) {
        this.context = context;
        this.files = files;
    }

    public FileViewAdapter(Context context){

        this.context = context;
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.file_list_item, viewGroup, false);
        return new Viewholder(layout);
    }

    @Override
    public void onBindViewHolder(Viewholder viewholder, int i) {
        File file = files.get(i);
        if (file.isDirectory()) {
            viewholder.fileIcon.setImageDrawable(
                    context.getResources().getDrawable(R.drawable.ic_folder_black_24dp));
            viewholder.wholeThing.setOnClickListener(null);
        } else {
            viewholder.fileIcon.setImageDrawable(
                    context.getResources().getDrawable(R.drawable.ic_insert_drive_file_black_24dp));
            viewholder.wholeThing.setOnClickListener(null);
        }
        viewholder.fileName.setText(file.getName());
    }


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

    public void clear() {
        files.clear();
        notifyDataSetChanged();
    }

    public void addAll(List<File> newFiles) {
        files.addAll(newFiles);
        notifyDataSetChanged();
    }

    class Viewholder extends RecyclerView.ViewHolder {
        View wholeThing;
        ImageView fileIcon;
        TextView fileName;

        public Viewholder(View itemView) {
            super(itemView);
            wholeThing = itemView;
            fileIcon = (ImageView) itemView.findViewById(R.id.file_icon);
            fileName = (TextView) itemView.findViewById(R.id.file_name);
        }
    }

}

我如何在活动时调用适配器的构造函数.

How I am calling the constructor of adapter on activity.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_viewer);    
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}

@Override
public void onResume() {
     adapter = new Adapter(this, /*A list of File items*/);
     recyclerView.setAdapter(adapter);
     recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

推荐答案

听起来很愚蠢,在为recyclerView设置数据后调用此代码行,对我解决了这个问题:

As stupid as it might sound, calling this line of code after setting data for recyclerView, helped me for this issue:

recyclerView.smoothScrollToPosition(0)

PS:我使用的可能与此有关的技术是:RJava,Retrofit2,NavigationUI,Fragments,LiveData和Databinding.

PS: technologies that I was using that may have something to do with this were: RJava, Retrofit2, NavigationUI, Fragments, LiveData, and Databinding.

这篇关于RecyclerView在滚动之前不会显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆