如何使gridview更有效?

时间:2014-10-13 10:10:01

标签: android android-listview

在我的布局中,我有一个用于手机的项目和最多10列用于平板电脑的项目。当我在手机上测试它时,我不会遇到任何滞后现象,但平板电脑上的滞后是明显的 我想知道以下措施是否会使listview消耗更少的资源 通常我的适配器中总是有以下行:

holder.tvTitle.setText("some text") 

如果我将用

替换它
if(!holder.title.equals(data.get(position).getTitle())){
            holder.title = data.get(position).getTitle();
            holder.tvTitle.setText(holder.title);
        }
我会得到任何好处吗? 要加载图像我正在使用UniversalImageLoader
还有我的适配器

public class FragmentArtistAdapter extends BaseAdapter {
    private List<ArtistsEntity> data;
    private Context context;
    private DisplayImageOptions options;

    public FragmentArtistAdapter(Context activity, List<ArtistsEntity> data) {
        this.data = data;
        this.context = activity;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.artist_stub)
                .showImageForEmptyUri(R.drawable.artist_stub)

                .showImageOnFail(R.drawable.artist_stub).cacheInMemory(true)
                .cacheOnDisk(true).considerExifParams(true)

                .bitmapConfig(Bitmap.Config.RGB_565).build();
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        final ViewHolder holder;
        // reuse views
        if (rowView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            rowView = inflater.inflate(R.layout.fragment_artist_list_item,
                    parent, false);
            // configure view holder
            holder = new ViewHolder(
                    (TextView) rowView.findViewById(R.id.tv_name),
                    (TextView) rowView.findViewById(R.id.tv_view_count),
                    (TextView) rowView.findViewById(R.id.tv_like_count),
                    (ImageView) rowView.findViewById(R.id.im_photo), position,
                    (ProgressBar) rowView.findViewById(R.id.progressbar1),
                    (LinearLayout) rowView.findViewById(R.id.ll_favorite_box),
                    (ImageView) rowView
                            .findViewById(R.id.im_favorite_indicator));
            Typeface fontTitle = Typeface.createFromAsset(inflater.getContext()
                    .getAssets(), "fonts/ubuntu_r.ttf");
            holder.title.setTypeface(fontTitle);
            Typeface fontArtist = Typeface.createFromAsset(inflater
                    .getContext().getAssets(), "fonts/ubuntu_l.ttf");
            holder.title.setShadowLayer(2f, -1, 1, Color.LTGRAY);
            holder.viewCount.setTypeface(fontArtist);
            holder.likeCount.setTypeface(fontArtist);
            holder.likeCount.setShadowLayer(2f, -1, 1, Color.LTGRAY);
            holder.viewCount.setShadowLayer(2f, -1, 1, Color.LTGRAY);
            rowView.setTag(holder);
        } else
            holder = (ViewHolder) rowView.getTag();

        if(!holder.sTitle.equals(data.get(position).getName())){
            holder.sTitle = data.get(position).getName();
            holder.title.setText(holder.sTitle);
        }
        holder.title.setText(data.get(position).getName());
        holder.viewCount.setText("" + data.get(position).getViewCount());
        holder.likeCount.setText("" + data.get(position).getFavoriteCount());
        String value = data.get(position).getImageUrl();
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(value, holder.photo, options,
                new MySimpleImageLoaderListener(holder, value));

        boolean fav = data.get(position).isFavorite();

        if (fav != holder.favorite) {
            if (fav) {
                holder.llFavBox.setBackgroundColor(context.getResources()
                        .getColor(R.color.blue_text_chart_position));
                holder.favIndicator
                        .setImageResource(R.drawable.star_unselected_icon);
                holder.likeCount.setTextColor(context.getResources().getColor(
                        R.color.white_text_60));
            } else {
                holder.llFavBox.setBackgroundColor(context.getResources()
                        .getColor(R.color.white_with_blue));
                holder.favIndicator
                        .setImageResource(R.drawable.star_selected_icon);
                holder.likeCount.setTextColor(context.getResources().getColor(
                        R.color.black_text_60));
            }
            holder.favorite = fav;
        }

        return rowView;
    }

    public class MySimpleImageLoaderListener extends SimpleImageLoadingListener {
        private final ViewHolder holder;
        private final String value;

        public MySimpleImageLoaderListener(ViewHolder holder, String value) {
            this.holder = holder;
            this.value = value;
        }

        @Override
        public void onLoadingStarted(String imageUri, View view) {
            holder.progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view,
                FailReason failReason) {
            holder.progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingComplete(String imageUri, View view,
                Bitmap loadedImage) {
            holder.progressBar.setVisibility(View.GONE);
        }

    }


    private static class ViewHolder {
        final TextView title;
        final TextView viewCount;
        final TextView likeCount;
        final ImageView photo;
        final ProgressBar progressBar;
        final int position;
        final LinearLayout llFavBox;
        final ImageView favIndicator;
        boolean favorite = false;
        String sTitle= "";

        private ViewHolder(TextView title, TextView viewCount,
                TextView likeCount, ImageView photo, int position,
                ProgressBar progressBar, LinearLayout llFavBox,
                ImageView favIndicator) {
            this.title = title;
            this.viewCount = viewCount;
            this.likeCount = likeCount;
            this.photo = photo;
            this.position = position;
            this.progressBar = progressBar;
            this.llFavBox = llFavBox;
            this.favIndicator = favIndicator;
        }
    }

可以做些什么来改进?

2 个答案:

答案 0 :(得分:0)

我将大多数对象初始化从getView转移到适配器构造函数或外部适配器

答案 1 :(得分:0)

设置文本不应该有任何问题,因此为了更清晰的代码,我放弃等于()检查。

您的代码中实际可能耗费时间的内容:

  • 一次又一次地创建字体 - 将其移出某处并重用字体。
  • getViewCount()和getFavoriteCount() - 我们不知道那些可能是复杂的数据库查询,可能需要花费时间。
  • 图像加载绝对是耗时的,但希望是异步完成的。