【发布时间】:2017-09-26 12:32:44
【问题描述】:
在 gridview 的帮助下,我希望屏幕如下所示。我可以使用 setSpanSizeLookup 添加 1 列和 2 列
但是如何添加动态宽度的两列呢?
final GridLayoutManager gridLayoutManager=new GridLayoutManager(ExploreMenuActivity.this,2);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if(position == 0)
{
return 2;
}else {
return 1;
}
}
});
更新:
我使用了 google 的 Flexbox 布局。
implementation 'com.google.android:flexbox:0.3.1'
我的主要活动
FlexboxLayoutManager flexboxLayoutManager=new FlexboxLayoutManager(ExploreMenuActivity.this);
flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
flexboxLayoutManager.setAlignItems(AlignItems.STRETCH);
recycler_ExploreProduct.setLayoutManager(flexboxLayoutManager);
我的 .xml 文件
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/img"
android:layout_width="@dimen/_140sdp"
android:background="@color/color_white_gray"
android:layout_height="@dimen/_140sdp"
android:scaleType="fitCenter"
android:src="@drawable/p_7" />
和我的适配器
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), aryImages[position], null);
holder.bindTo(drawable, position);
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.img);
}
void bindTo(Drawable drawable, int position) {
imageView.setImageDrawable(drawable);
ViewGroup.LayoutParams lp = imageView.getLayoutParams();
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
Log.d(TAG,"## bindTo before if");
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
Log.d(TAG,"## bindTo inside if");
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
if (position == 0) {
flexboxLp.width = screenWidth / 3;
} else if (position == 1) {
flexboxLp.width = screenWidth / 2;
} else if (position == 2) {
flexboxLp.width = screenWidth / 2;
} else if (position == 3) {
flexboxLp.width = screenWidth / 3;
} else if (position == 4) {
flexboxLp.width = screenWidth / 3;
} else if (position == 5) {
flexboxLp.width = screenWidth / 2;
} else if (position == 6) {
flexboxLp.width = screenWidth;
} else if (position == 7) {
flexboxLp.width = screenWidth / 3;
} else if (position == 8) {
flexboxLp.width = screenWidth / 2;
} else if (position == 9) {
flexboxLp.width = screenWidth;
} else if (position == 10) {
flexboxLp.width = screenWidth / 2;
} else if (position == 11) {
flexboxLp.width = screenWidth / 3;
} else if (position == 12) {
flexboxLp.width = screenWidth;
} else if (position == 13) {
flexboxLp.width = screenWidth / 3;
} else if (position == 14) {
flexboxLp.width = screenWidth / 2;
} else if (position == 15) {
flexboxLp.width = screenWidth / 2;
} else if (position == 16) {
flexboxLp.width = screenWidth / 3;
} else if (position == 17) {
flexboxLp.width = screenWidth;
} else if (position == 18) {
flexboxLp.width = screenWidth / 3;
} else if (position == 19) {
flexboxLp.width = screenWidth / 2;
}
}
}
现在我可以使用上述静态条件实现输出,
当我的数据来自 API 时,我怎样才能动态地做这些事情。
【问题讨论】:
标签: android dynamic android-recyclerview layout-manager android-gridview