如果长时间快速滚动,GridView滚动滞后并崩溃。

时间:2014-10-08 08:41:40

标签: android gridview android-adapter

我已使用GridView在我的应用中实施了BaseAdapter。遵循任何ListView的推荐做法(使用ViewHolder,回收View和通用图像加载器以异步加载图像),我仍然不满意性能;网格的滚动仍然不是非常平滑,如果我连续上下滚动约5-6秒(没有抛出任何异常),活动就会崩溃。

在滚动操作期间,我经常在我的logcat中获得以下内容 -

I/Choreographer﹕ Skipped 32 frames! The application may be doing too much work on its main thread.其中32也可以拍摄最多54帧。

这是我的游标适配器代码 -

public class ContestantListCustomGridAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private Cursor allContestantShortInfo;

    GlobalMethods gm = new GlobalMethods();

    public ContestantListCustomGridAdapter(Context c) {

        mContext = c;

        String[] columns = new String[] {Database.ColumnOne,
                Database.ColumnTwo,
                Database.ColumnThree,
                Database.ColumnFour,
                Database.ColumnFive,
                Database.ColumnSix,
                };

        //Get data from the DB
        Database db = new Database(mContext);
        db.open();
        this.allContestantShortInfo = db.getValuesFromTable(Database.CONTESTANT_SHORT_INFO_TABLE,
                columns, Database.CONTESTANT_SHORT_INFO_EVENT_ID + "=" + GlobalConstants.eventId,
                null, null, null, null);
        db.close();
        allContestantShortInfo.moveToFirst();

        mLayoutInflater = LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        return allContestantShortInfo.getCount();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder {
        TextView participantName;
        TextView participantCountry;
        TextView participantLikes;
        RelativeLayout participantEliminated;
        ImageView participantImage;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {

            convertView = mLayoutInflater.inflate(R.layout.grid_view_card_layout, parent, false);
            holder = new ViewHolder();
            holder.participantEliminated = (RelativeLayout) convertView.findViewById(R.id.Card_isEliminated);
            holder.participantName = (TextView) convertView.findViewById(R.id.grid_model_name);
            holder.participantCountry = (TextView) convertView.findViewById(R.id.grid_country_name);
            holder.participantLikes = (TextView) convertView.findViewById(R.id.participant_total_likes_number);
            holder.participantImage = (ImageView) convertView.findViewById(R.id.grid_model_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        applyTypeFace(convertView);

        //Set the values into placeholders
        allContestantShortInfo.moveToPosition(position);
        holder.participantName.setText(allContestantShortInfo
                .getString(GlobalConstants.CONTESTANT_SHORT_INFO_NAME_INDEX));
        holder.participantCountry.setText(allContestantShortInfo
                .getString(GlobalConstants.CONTESTANT_SHORT_INFO_COUNTRY_NAME_INDEX));
        holder.participantLikes.setText(allContestantShortInfo
                .getString(GlobalConstants.CONTESTANT_SHORT_INFO_LIKES_INDEX));

        if(allContestantShortInfo.getString(GlobalConstants.CONTESTANT_SHORT_INFO_ELIMINATION_STATUS_INDEX)
                .equalsIgnoreCase("true")) {
            holder.participantEliminated.setVisibility(View.VISIBLE);
            holder.participantImage.setColorFilter(Color.parseColor("#90000000"));
        } else {
            holder.participantEliminated.setVisibility(View.GONE);
            holder.participantImage.setColorFilter(0);
        }

        ImageLoader.getInstance()
                .displayImage(allContestantShortInfo
                                .getString(GlobalConstants.CONTESTANT_SHORT_INFO_WEB_APP_PATH_INDEX) +
                              allContestantShortInfo
                                .getString(GlobalConstants.CONTESTANT_SHORT_INFO_THUMBNAIL_IMAGE_URL_INDEX),
                        holder.participantImage);

        return convertView;
    }
}

有什么可以改进,以便我可以让GridView像任何其他ListView一样顺畅,并且它也不会崩溃。

2 个答案:

答案 0 :(得分:0)

对于使用数据库和使用游标的每个getView,我认为要求光标移动或读取会导致滞后。

首先在适配器的constructor读取数据库内容并创建参数的ArrayList,然后使用ArrayList填充数据。

如果您的数据非常大,您可以按块读取块,例如读取0-100个项目,当用户滚动并触及项目90时读取例如100-110并删除0-10以防止获取{{1} }。

答案 1 :(得分:0)

滞后是由我的方法applyTypeFace(convertView)引起的。删除它解决了这个问题。