gpt4 book ai didi

android - 在 android 屏幕上安装 recyclerview 项目

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:26 25 4
gpt4 key购买 nike

我有麻烦了!我有这个 RecyclerView,我在其中使用 GridLayoutManager 来实现两列和几行。但这是我的问题:我在这个 RecyclerView 中最多有 8 个项目,我想根据屏幕尺寸调整它们

到目前为止我得到了这个:

enter image description here

使用这段代码:

        Rect rectangle = new Rect();
Window window = ((Activity)context).getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;

final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();

int softButtonsHeight = 0;

DisplayMetrics metrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

DisplayMetrics realMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
((Activity)context).getWindowManager().getDefaultDisplay().getRealMetrics(realMetrics);

if(realMetrics.heightPixels > metrics.heightPixels){
softButtonsHeight = realMetrics.heightPixels - metrics.heightPixels;
}
}

ImageView img_Logo = (ImageView)rootView.findViewById(R.id.img_logo_detalhe);

float logoHeight = 0;
//convertendo na mão tamanho do sponsor
if(img_Logo.getVisibility() != GONE) {
logoHeight = 100 * context.getResources().getDisplayMetrics().density;
}

double sizeInPx = (metrics.heightPixels - titleBarHeight - softButtonsHeight - mActionBarSize - logoHeight) / Math.round(list.size() / 2D);

itensAdapter = new OptionItensAdapter(context, list, (int)sizeInPx);
rvOptions.setAdapter(itensAdapter);

在我的 onBindViewHolder 处的 OptionItensAdapter 构造函数中:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, sizeInPx);
holder.imageButton.setLayoutParams(params);

你有什么想法可以让我实现这个目标吗?提前致谢。

最佳答案

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_list, null);

int height = parent.getMeasuredHeight() / 4;
int width = parent.getMeasuredWidth();

view.setLayoutParams(new RecyclerView.LayoutParams(width, height));

return new ViewHolder(view);
}

关于android - 在 android 屏幕上安装 recyclerview 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com