2

我正在使用FirebaseRecyclerAdapter從Firebase獲取數據並填充回收站視圖。代碼工作正常,但是當我開始滾動時,回收站視圖使用每次滾動都會調用PopulateViewHolder方法,這使得滾動滯後很多,有時在回收站視圖的開始時會丟失一些數據。FirebaseRecyclerAdapter,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)); 
    } 
} 

及郵政類模型包含了每個職位的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" 
    /> 

佈局管理器是一個線性佈局管理器和包含該適配器的方法僅在onCreatemethod調用一次。

回答

2

這是正確的。
populateViewHolder方法由onBindViewHolder方法調用。您可以查看official javadoc
通過RecyclerView調用此方法以在指定位置顯示數據。

當您滾動時,您將開始獲取用於排除屏幕的行的視圖持有者,並且您必須用新數據替換舊數據。
爲此,調用此方法。

檢查您的代碼。
如果在此步驟中存在一些耗時的方法(例如,如果您加載圖像),則應使用異步任務。

相關問題