【问题标题】:FirebaseRecyclerAdapter, PopulateViewHolder called many times when scrollingFirebaseRecyclerAdapter、PopulateViewHolder 滚动时多次调用
【发布时间】:2017-07-01 02:53:24
【问题描述】:

我正在使用 FirebaseRecyclerAdapter 从 Firebase 获取数据并填充回收器视图。代码工作正常,但是当我开始滚动时,回收器视图会在每次滚动时调用 PopulateViewHolder 方法,这会导致滚动延迟很多,有时我会在回收器视图的开头丢失一些数据。

我的代码是:

    final FirebaseRecyclerAdapter<Post, PostsViewHolder> adapt = new FirebaseRecyclerAdapter<Post, PostsViewHolder>(Post.class, R.layout.layout_cardview, PostsViewHolder.class, mRef) {

        @Override
        protected void populateViewHolder(PostsViewHolder viewHolder, Post model, int position) {
            Log.i("CHECK", "POPULATEVIEWHOLDER");
            test.setLongitude(model.getLongitude());
            Log.d("LONGITUDE", model.getLongitude() + "");
            test.setLatitude(model.getLatitude());
            Log.d("LATITUDE", test.getLatitude() + "");
            try {
                if ((location.distanceTo(test) / 1000.0) < 10) {
                    viewHolder.Content.setText(model.getContent());
                    viewHolder.Time.setText(model.getTime());
                } else {
                    viewHolder.delete();
                }
            }catch(Exception e){
                Toast.makeText(getApplicationContext(), "Please Make sure Location is enabled", Toast.LENGTH_LONG).show();
            }
        }
    };
    adapt.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            rv.getLayoutManager().smoothScrollToPosition(rv,null,positionStart);
        }
    });

PostsViewHolder 是:

    public static class PostsViewHolder extends RecyclerView.ViewHolder{

    TextView Content;
    TextView Time;
    CardView cv;

    public PostsViewHolder(View v) {
        super(v);
        Content = (TextView) v.findViewById(R.id.textview_descriptionBrief);
        cv = (CardView) v.findViewById(R.id.post_cardview);
        Time = (TextView) v.findViewById(R.id.textView_time);
    }

    public void delete(){
        cv.setLayoutParams(new RecyclerView.LayoutParams(0,0));
    }
}

Post 类模型包含每个帖子的 getter 和 setter 以及字段。

列表的 XML 是:

    <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_lookup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
tools:context="com.app.postit.postit.LookupActivity">

<!--<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:id="@+id/toolbar_lookupActivity">

</android.support.v7.widget.Toolbar>-->

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:id="@+id/recyclerview_lookupActivity"
    />

<android.support.design.widget.FloatingActionButton
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/button_floatingButton"
    android:scaleType="center"
    app:backgroundTint="@color/colorWhite"
    app:fabSize="mini"
    android:src="@drawable/ic_add_circle_blue_48dp"
    app:elevation="5dp"
    />

布局管理器是一个线性布局管理器,包含适配器的方法在onCreate方法中只调用一次。

【问题讨论】:

    标签: android firebase firebase-realtime-database android-recyclerview firebaseui


    【解决方案1】:

    没错。
    populateViewHolder方法被onBindViewHolder方法调用。

    您可以查看official javadoc
    该方法被RecyclerView调用在指定位置显示数据。

    当您滚动时,您将开始获得用于离开屏幕的行的视图持有者,您必须用新数据替换他们持有的旧数据。
    为此,将调用此方法。

    检查您的代码。
    如果这一步有一些耗时的方法(例如,如果你加载图像),你应该使用异步任务。

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 2017-01-09
      • 2016-11-10
      • 2018-06-18
      • 1970-01-01
      • 2018-04-02
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多