网格视图开始滞后

时间:2015-08-02 10:15:55

标签: android scroll android-gridview android-scrollview lag

我正在尝试制作一个显示图像网格的简单网格视图。然而,有些事情使得应用程序滞后非常糟糕。我得到了一些关于跳过框架的警告,而且我可能在主线程上做了太多工作,但老实说我不知道​​我做错了什么。

我的适配器:

public class HeaderListAdapter extends BaseAdapter {
    private List<HeaderInfo> headerList;
    private int HEADER_SIZE;
    private LayoutInflater layoutInflater;


    public HeaderListAdapter(Activity context, List<HeaderInfo> headerList) {
        this.headerList = headerList;
        HEADER_SIZE = (context.getResources().getDimensionPixelSize(R.dimen.storeItemSelect_HeaderHeight));
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HeaderInfo headerInfo = (HeaderInfo) this.getItem(position);

        ViewHolder holder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.store_item_selection_header_card, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.headerImage.setImageDrawable(headerInfo.headerImage);
        holder.headerSpace.setVisibility(position == 0 || position == 1 
            ? View.VISIBLE 
            : View.GONE);
        holder.headerName.setText(headerInfo.headerName);

        return convertView;
    }

public static class ViewHolder {
    public ImageView headerImage;
    public TextView headerName;
    public Space headerSpace;

    public ViewHolder(View v) {
        headerImage = (ImageView) v.findViewById(R.id.header_image);
        headerName = (TextView) v.findViewById(R.id.header_title);
        headerSpace = (Space) v.findViewById(R.id.headerSpace);
    }
}

public int Height() {
    return (HEADER_SIZE + 32) * Math.round(headerList.size() / 2);
}

我的onCreate活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.store_item_selection_main);

    Intent intent = getIntent();
    storeName = intent.getStringExtra(EXTRA_STORE_NAME);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    backdrop = (ImageView) findViewById(R.id.backdrop);
    collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    GridView testGridView = (GridView) findViewById(R.id.gridView);

    headerItems = _HelperFunctions.getTempHeaderDB(this);
    HeaderListAdapter expandableListAdapter = new HeaderListAdapter(this, headerItems);
    testGridView.getLayoutParams().height = getHeight(expandableListAdapter.Height());
    testGridView.setAdapter(expandableListAdapter);
    testGridView.setNumColumns(2);

    setupToolbar(toolbar, collapsingToolbar, storeName, backdrop);

    LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.recent_layout_controller);
    testGridView.setLayoutAnimation(controller);
}

注意:getHeight()方法只能获得设置列表视图的正确高度,而setupToolbar只是设置我的可折叠工具栏。

如果您需要更多信息,请告诉我,并感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以在我的情况下,问题是我的图像太大而导致压力,因为它们都被加载到UI线程上。 JohanShogun关于使用后台线程和缓存结果的建议是正确的方法。

This link是一个很好的开始,this link帮助我了解异步任务和后台加载。