Android - recyclerview item gone 還是會佔空間

Android - recyclerview item gone 還是會佔空間

https://www.jianshu.com/p/08bc1d83d23b

重新定義高度即可

class ViewHolder extends RecyclerView.ViewHolder {

    ViewHolder(View itemView) {
        super(itemView);
    }

    public void setVisibility(boolean visible) {
        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
        if (visible) {
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            itemView.setVisibility(View.VISIBLE);
        } else {
            itemView.setVisibility(View.GONE);
            param.height = 0;
            param.width = 0;
        }
        itemView.setLayoutParams(param);
    }
}