【问题标题】:GridView Performance lag with Custom CursorAdapters自定义 CursorAdapters 的 GridView 性能滞后
【发布时间】:2014-01-04 13:56:23
【问题描述】:

我目前正在尝试掌握 android developmentemnt 的窍门,到目前为止它进展顺利(直到现在)。我目前正在为我的网格视图使用自定义光标适配器。我读到使用光标适配器承担了听起来很有效的主 UI 线程的负载,所以我尝试按如下方式实现我自己的光标适配器

public class VideoListAdapter extends SimpleCursorAdapter{

private Context ctx;
private int parent_layout_id;

ArrayList<VideoModel> videoList = new ArrayList<VideoModel>();

public VideoListAdapter(
        Context ctx,
        int parent_layout_id,
        Cursor c,
        String[] fromColumns,
        int[] toView)
{
    super(ctx,parent_layout_id,c,fromColumns,toView);
    this.ctx = ctx;
    this.parent_layout_id = parent_layout_id;
}

@Override
public void bindView(View v, Context context, Cursor c) {

    //gets title column
    int titleColumn = c.getColumnIndex(MediaStore.Video.Media.TITLE);
    String title = c.getString(titleColumn);
    TextView titleView =(TextView) v.findViewById(R.id.video_title);
    titleView.setText(title);

    //gets duration
    int durationColumn = c.getColumnIndex(MediaStore.Video.Media.DURATION);
    String duration = c.getString(durationColumn);
    TextView durationView = (TextView) v.findViewById(R.id.video_date);
    durationView.setText(duration);

    //gets Thumb
    int videoDataColumn = c.getColumnIndex(MediaStore.Video.Media.DATA);
    String videodataUrl = c.getString(videoDataColumn);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(videodataUrl);
    Bitmap bm = mmr.getFrameAtTime(1000);
    ImageView imgv = (ImageView) v.findViewById(R.id.video_thumb);
    imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imgv.setImageBitmap(bm);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Cursor c = getCursor();

    //get layout inflater
    final LayoutInflater li=LayoutInflater.from(context);
    View v = li.inflate(parent_layout_id,parent,false);

    //gets title column
    int titleColumn = c.getColumnIndex(MediaStore.Video.Media.TITLE);
    String title = c.getString(titleColumn);
    TextView titleView =(TextView) v.findViewById(R.id.video_title);
    titleView.setText(title);

    //gets duration
    int durationColumn = c.getColumnIndex(MediaStore.Video.Media.DURATION);
    String duration = c.getString(durationColumn);
    TextView durationView = (TextView) v.findViewById(R.id.video_date);
    durationView.setText(duration);

    //gets Thumb
    int videoDataColumn = c.getColumnIndex(MediaStore.Video.Media.DATA);
    String videodataUrl = c.getString(videoDataColumn);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(videodataUrl);
    Bitmap bm = mmr.getFrameAtTime(1000);
    ImageView imgv = (ImageView) v.findViewById(R.id.video_thumb);
    imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imgv.setImageBitmap(bm);
    return v;
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    return super.runQueryOnBackgroundThread(constraint);
}

}

我想要实现的是从外部媒体存储中获取视频文件列表并将它们显示在网格视图中。标准缩略图对于我想到的布局风格来说太小了,所以我使用 MediaMetadataRetriever 从每个视频文件中获取帧并将其用作缩略图。喜欢这样

当我运行应用程序时,我的 gridView 的滚动性能变差了。我错过了什么。我知道 SimpleCursorAdapter 类有一个可覆盖的方法 runQueryOnBackgroundThread 但我什至不知道放在那里或如何使用它。任何指点都将不胜感激并原谅我的污迹;)

【问题讨论】:

    标签: java android android-gridview simplecursoradapter android-adapter


    【解决方案1】:

    首先使用ViewHolder 绑定您的Gridview,第二个是save your thumb(bitmap) in existing arrayList,因此您在滚动时不必执行相同的过程。

    【讨论】:

    • 谢谢 Sanket,我修好了。原来我在 bindview 中的代码是问题所在,newView 被第一次调用,而且只有一次,所以我找到了一种使用列表项的 setTag() 方法保存模型的方法,因此处理只发生一次,我可以轻松取回对象在 bindView 方法上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多