次のシナリオがあります。LinearLayoutを拡張するカスタムクラスである「カード」を追加するLinearLayoutがあります。
問題は、各カードに画像が含まれていることです。カードが多すぎて表示できない場合、画像のサイズが原因でメモリ不足エラーが発生します。
現在画面に表示されているカードを動的に確認し、それらのカードの画像のみをロードして、残りをnullのままにするにはどうすればよいですか?
現在画面に表示されているカードと表示されていないカードを見つけるのに苦労しています。また、ユーザーがリストをスクロールするときに、イベントを読み込んで画像をクリアすることもできます。
あなたはあなたのために仕事をするRecyclerViewを実装しなければなりません。
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
final Adapter adapter = new Adapter();
recyclerView.setAdapter(adapter);
アダプター:
private class Adapter extends RecyclerView.Adapter<MyViewHolder> {
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_main, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, int i)
// set the content of the card
}
@Override
public int getItemCount() {
return // number of cards
}
}
ViewHolder
private class MyViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public TextView text2;
public ImageView imageView;
public MyViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(/* your textView */);
text2 = (TextView) itemView.findViewById(/* another textView */);
imageView = (ImageView) itemView.findViewById(/* an image */);
}
}
レイアウト:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityFragment">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"
/>
</RelativeLayout>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加