如何使用ViewHolder获取屏幕尺寸?

时间:2016-03-23 10:51:13

标签: android android-recyclerview android-viewholder

我需要根据屏幕高度更改cardView高度。如何在TileAdapter中获得屏幕高度?

public static class ViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {

    public CardView cardView;

    // ........................

    public ViewHolder(final View itemView) {
        super(itemView);
        cardView = (CardView) itemView.findViewById(R.id.card_view);

        Display mdisp = itemView.getContext().getWindowManager().getDefaultDisplay();
        Point mdispSize = new Point();
        mdisp.getSize(mdispSize);
        height = mdispSize.y;

        Toast.makeText(itemView.getContext(),height,Toast.LENGTH_SHORT).show();
        cardView.setMinimumHeight(height);
    }

    // ..........................
}

1 个答案:

答案 0 :(得分:0)

试试这个 - Get screen dimensions in pixels

使用 DisplayMetrics 代替显示