TonicArtos / SuperSLiM

A layout manager for the RecyclerView with interchangeable linear, grid, and staggered displays of views, all with configurable section headers including the sticky variety as specified in the material design docs.
http://tonicartos.nz
2.12k stars 297 forks source link

Null value passed in call to LayoutManager.LayoutParams.from() #150

Closed maoruibin closed 7 years ago

maoruibin commented 8 years ago

I use this lib,and refer to example, but when get itemView's LayoutParam ,it values is null ,how to fix it, code like this, only for important code

/**
 * ViewHolderItem is a parent class for different item 
 */
 abstract static class ViewHolderItem extends RecyclerView.ViewHolder {

    public ViewHolderItem(View itemView) {
        super(itemView);
    }

    abstract void bindItem(Context context,Gank gank);
}

the method of onCreateViewHolder

@Override
public GankListAdapter.ViewHolderItem onCreateViewHolder(ViewGroup parent, int viewType) {
    //目前有三种类型 ==0 是妹子福利图片类型
    View view = null;
    if (viewType == GankType.福利.ordinal()) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gank_item_girl, null);
        return new ViewHolderItemGirl(view);
    }else if(viewType == GankType.GANK_HEADER.ordinal()){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gank_item_header, null);
        return new ViewHolderItemHeader(view);
    } else {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gank_item_normal, null);
        return new ViewHolderItemNormal(view);
    }
}

error please

@Override
public void onBindViewHolder(final GankListAdapter.ViewHolderItem holder, int position) {
    final Gank gank = mGankList.get(position);
    holder.bindItem(mContext, gank);

    final View itemView = holder.itemView;

    ViewGroup.LayoutParams params = itemView.getLayoutParams();

    final GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(params);
    // Overrides xml attrs, could use different layouts too.
    if (gank.isHeader) {
        lp.headerDisplay = LayoutManager.LayoutParams.HEADER_STICKY;
        if (lp.isHeaderInline() || (mMarginsFixed && !lp.isHeaderOverlay())) {
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        } else {
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        }

        lp.headerEndMarginIsAuto = !mMarginsFixed;
        lp.headerStartMarginIsAuto = !mMarginsFixed;
    }
    lp.setSlm(LinearSLM.ID);
    lp.setColumnWidth(mContext.getResources().getDimensionPixelSize(R.dimen.grid_column_width));
    lp.setFirstPosition(gank.sectionFirstPosition);
    itemView.setLayoutParams(lp);
}

when execute GridSLM.LayoutParams.from(params); it will point params is null