編集:わかりました、私は答えで与えられたアドバイスを取りました、そして違いは巨大です!投稿のSeriesAdapterを新しいものに置き換えました。1つは、SQLクエリで計算を行うことです(エピソードの総数と視聴されたエピソードの総数を知るため)。また、ビットマップをロードしたらハッシュマップに保存して、2回ロードする必要がないようにします。OutOfMemoryExceptionが怖いので、他のソリューションを検討しています。
私はAndroidを初めて使用し、外部ストレージに保存した画像を含むリストビューを表示したいと考えています。
画像は以前にダウンロードされ、今は外部ストレージに保存されています。これは画像の例ですhttp://thetvdb.com/banners/graphical/80348-g32.jpg画像を80%に圧縮するとスペースを節約するためにそれらを保存します。
リストビューのスクロールをスムーズにするためにいくつかの方法を試しましたが、ここでは明らかに頭を悩ませています。ここで何かおかしなことをした場合に備えて、リストアイテムとアダプターのレイアウトを提供しました。
リストビューを改善するためのヒントやコツをいただければ幸いです。
SeriesAdapter:
public static class SeriesAdapter extends ArrayAdapter<Series> {
static class viewHolder
{
ImageView image;
TextView information;
String seriesId;
String season;
ProgressBar progress;
TextView txtSmallView;
}
private final Context context;
private final ArrayList<Series> series;
private DateHelper dateHelper;
private final DatabaseHandler db;
Object mActionMode;
int resource;
public SeriesAdapter(Context context, int resource, ListView lv, ArrayList<ExtendedSeries> objects)
{
super(context, resource, objects);
this.context = context;
this.series = objects;
this.resource = resource;
db = new DatabaseHandler(context);
dateHelper = new DateHelper();
cache = new HashMap<String, Bitmap>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
viewHolder holder;
ExtendedSeries s = series.get(position);
if(convertView == null)
{
convertView = View.inflate(context, resource, null);
holder = new viewHolder();
holder.image = (ImageView)convertView.findViewById(R.id.imgSeriesImage);
holder.information = (TextView)convertView.findViewById(R.id.txtUpcomingEpisode);
holder.progress = (ProgressBar)convertView.findViewById(R.id.pgrWatched);
convertView.setTag(holder);
}
else
{
holder = (viewHolder)convertView.getTag();
}
if(s != null)
{
holder.seriesId = s.getImage();
convertView.setTag(R.string.homeactivity_tag_id,s.getID());
convertView.setTag(R.string.homeactivity_tag_seriesid,s.getSeriesId());
holder.progress.setMax(s.getTotalEpisodes());
holder.progress.setProgress(s.getWatchedEpisodes());
holder.image.setImageBitmap(getBitmapFromCache(s.getImage()));
holder.information.setText(s.getNextEpisodeInformation().equals("") ? context.getText(R.string.message_show_ended) : s.getNextEpisodeInformation());
}
return convertView;
}
リストアイテムのレイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgSeriesImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:focusable="false"
android:scaleType="centerCrop"
android:src="@drawable/noimage" />
<RelativeLayout
android:id="@+id/relProgressView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/pgrWatched"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="21dp"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/progressbar" />
<TextView
android:id="@+id/txtUpcomingEpisode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:scrollHorizontally="true"
android:scrollbars="none"
android:shadowColor="@android:color/black"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:textAllCaps="true"
android:textColor="#ffffffff"
android:textSize="11sp"
android:textStyle="normal|bold"
android:typeface="normal" />
</RelativeLayout>
アクティビティレイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lstMySeries"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:longClickable="true"
android:divider="#000000"
/>