5

私はrecyclerViewを画面上に最大3個まで表示できますが、リスト内の45個のアイテムすべてに対してonCreateViewHolderとonBindViewHolderを一度に呼び出します以下)。RecyclerViewは一度にすべてのアイテムのonCreateViewHolderとonBindViewHolderを呼び出す

これらの項目を画面にスクロールするときだけ、これらのメソッドを呼び出すべきではありませんか?

*使用するレイアウトを定義するテストとして、0と1のリストを使用しています。それは一度にすべてのアイテム、表示されていないものも含めを呼んで示す

マイアダプターコード

public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

int[] post_list; 
private static final int TYPE_0 =0; 
private static final int TYPE_1=1; 
private LayoutInflater inflater; 
private Context context; 
Activity mActivity; 

public MyRecyclerAdapter(Context context, int[] list, Activity mActivity){ 
    this.context=context; 
    inflater=LayoutInflater.from(context); 
    this.list=list; 
    this.mActivity = mActivity; 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    Log.e("onCreateViewHolder", "CALLED"); 
    if(viewType== TYPE_0){ 
     View view=inflater.inflate(R.layout.layout_type0, parent,false); 
     Type0Holder holder=new Type0Holder(view); 
     return holder; 
    } 
    else{ 
     View view=inflater.inflate(R.layout.layout_type1, parent,false); 
     Type1Holder holder=new Type1Holder (view); 
     return holder; 
    } 


} 

@Override 
public int getItemViewType(int position) { 
    if(list[position]==0) { 
     return TYPE_0; 
    } 
    else { 
     return TYPE_1; 
    } 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    if(holder instanceof Type0Holder){ 
     Type0Holder type0Holder = (Type0Holder) holder; 
     type0Holder .mTextView.setText("Layout type 0"); 
     Glide.with(mActivity.getApplicationContext()).load(R.drawable.imageTest).into(type0Holder.mImageView); 
    } 
    else{ 
     Type1Holder type1Holder = (Type1Holder) holder; 
     type1Holder .mTextView.setText("Layout type 1"); 
     Glide.with(mActivity.getApplicationContext()).load(R.drawable.imageTest).into(type1Holder.mImageView); 
    } 
    Log.e("onBindViewHolder", String.valueOf(position)); 

} 

@Override 
public int getItemCount() { 
    return post_list.length; 
} 

class Type0Holder extends RecyclerView.ViewHolder { 
    TextView mTextView; 
    ImageView mImageView; 

    public Type0Holder (View view) { 
     super(view); 
     mTextView= (TextView) itemView.findViewById(R.id.text_view0); 
     mImageView= (ImageView) itemView.findViewById(R.id.image_view0); 

    } 
} 

class Type1Holder extends RecyclerView.ViewHolder { 
    TextView mTextView; 
    ImageView mImageView; 

    public ImagePostHolder(View itemView) { 
     super(itemView); 
     mTextView= (TextView) itemView.findViewById(R.id.text_view1); 
     mImageView= (ImageView) itemView.findViewById(R.id.image_view1); 
    } 
} 

マイリサイクラーのXML

<android.support.v4.widget.NestedScrollView 
    android:id="@+id/nested_scroll_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    android:fillViewport="true" 
    android:paddingBottom="3dp" 
    > 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:orientation="vertical" 
     android:clipToPadding="false"> 


     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      ></android.support.v7.widget.RecyclerView> 


    </RelativeLayout> 



</android.support.v4.widget.NestedScrollView> 

マイLOG。

10-23 15:15:46.093 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.113 28766-28766/com.example..E/HOLDER: 0 
10-23 15:15:46.113 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.113 28766-28766/com.example..E/HOLDER: 1 
10-23 15:15:46.113 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.123 28766-28766/com.example..E/HOLDER: 2 
10-23 15:15:46.123 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.123 28766-28766/com.example..E/HOLDER: 3 
10-23 15:15:46.123 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.133 28766-28766/com.example..E/HOLDER: 4 
10-23 15:15:46.133 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.133 28766-28766/com.example..E/HOLDER: 5 
10-23 15:15:46.143 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.143 28766-28766/com.example..E/HOLDER: 6 
10-23 15:15:46.143 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.143 28766-28766/com.example..E/HOLDER: 7 
10-23 15:15:46.153 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.153 28766-28766/com.example..E/HOLDER: 8 
10-23 15:15:46.153 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.153 28766-28766/com.example..E/HOLDER: 9 
10-23 15:15:46.163 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.163 28766-28766/com.example..E/HOLDER: 10 
10-23 15:15:46.163 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.163 28766-28766/com.example..E/HOLDER: 11 
10-23 15:15:46.173 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.173 28766-28766/com.example..E/HOLDER: 12 
10-23 15:15:46.173 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.183 28766-28766/com.example..E/HOLDER: 13 
10-23 15:15:46.183 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.183 28766-28766/com.example..E/HOLDER: 14 
10-23 15:15:46.183 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.183 28766-28766/com.example..E/HOLDER: 15 
10-23 15:15:46.193 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.193 28766-28766/com.example..E/HOLDER: 16 
10-23 15:15:46.193 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.193 28766-28766/com.example..E/HOLDER: 17 
    10-23 15:15:46.203 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.203 28766-28766/com.example..E/HOLDER: 18 
10-23 15:15:46.203 28766-28766/com.example..E/CREATE HOLDER: CALLED 
10-23 15:15:46.213 28766-28766/com.example.. E/HOLDER: 19 
    10-23 15:15:46.213 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
    10-23 15:15:46.213 28766-28766/com.example.. E/HOLDER: 20 
    10-23 15:15:46.213 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
    10-23 15:15:46.213 28766-28766/com.example.. E/HOLDER: 21 
10-23 15:15:46.223 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.223 28766-28766/com.example.. E/HOLDER: 22 
10-23 15:15:46.223 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.233 28766-28766/com.example.. E/HOLDER: 23 
10-23 15:15:46.233 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.233 28766-28766/com.example.. E/HOLDER: 24 
10-23 15:15:46.233 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.233 28766-28766/com.example.. E/HOLDER: 25 
10-23 15:15:46.243 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.243 28766-28766/com.example.. E/HOLDER: 26 
10-23 15:15:46.243 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.243 28766-28766/com.example.. E/HOLDER: 27 
10-23 15:15:46.253 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.253 28766-28766/com.example.. E/HOLDER: 28 
10-23 15:15:46.253 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.253 28766-28766/com.example.. E/HOLDER: 29 
10-23 15:15:46.263 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.263 28766-28766/com.example.. E/HOLDER: 30 
10-23 15:15:46.263 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.263 28766-28766/com.example.. E/HOLDER: 31 
10-23 15:15:46.273 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.283 28766-28766/com.example.. E/HOLDER: 32 
10-23 15:15:46.283 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.283 28766-28766/com.example.. E/HOLDER: 33 
10-23 15:15:46.293 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.313 28766-28766/com.example.. E/HOLDER: 34 
10-23 15:15:46.313 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.323 28766-28766/com.example.. E/HOLDER: 35 
10-23 15:15:46.323 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.323 28766-28766/com.example.. E/HOLDER: 36 
10-23 15:15:46.333 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.333 28766-28766/com.example.. E/HOLDER: 37 
10-23 15:15:46.333 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.343 28766-28766/com.example.. E/HOLDER: 38 
10-23 15:15:46.343 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.343 28766-28766/com.example.. E/HOLDER: 39 
10-23 15:15:46.343 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.353 28766-28766/com.example.. E/HOLDER: 40 
10-23 15:15:46.353 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.353 28766-28766/com.example.. E/HOLDER: 41 
10-23 15:15:46.353 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.363 28766-28766/com.example.. E/HOLDER: 42 
10-23 15:15:46.363 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.363 28766-28766/com.example.. E/HOLDER: 43 
10-23 15:15:46.373 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.373 28766-28766/com.example.. E/HOLDER: 44 
10-23 15:15:46.373 28766-28766/com.example.. E/CREATE HOLDER: CALLED 
10-23 15:15:46.383 28766-28766/com.example.. E/HOLDER: 45 

UPDATE 1 - 私はそれがNestedScrowViewの内側にあるので、私のrecyclerViewはリサイクルではない理由があることが分かったので、私もその場合、それはNestedScrowView内のビューをリサイクルする方法を作るのですか?

+0

あなたはこの解決策を見つけますか? –

答えて

0

私は同じ問題がありました。私の場合、NestedScrollViewはBottom Sheetのルートで、FrameLayoutに置き換えられました。

することができます場合は、あなたがRecyclerViewにFrameLayout

+0

がmycaseの場合は、NestedScrollを使用する必要があります。これを取り除く方法は? – GvSharma

0

無効にnestedScrollingを使用してみてください。

mRecyclerView.setNestedScrollingEnabled(false); 
関連する問題