我希望屏幕看起来像下面,在GridView的帮助下。我能够添加1列和两列使用setSpanSizeLookupAndroid Gridview与2列(但不同的ImageView宽度大小)
但如何添加两列动态宽度?
[1]
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;
}
}
});
更新[在这里输入的形象描述] [1]!]:
我用Flexbox的布局由谷歌。
compile '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。
你好感谢你对张贴,I HV尝试这种解决方案,昨天没有运气 – Shruti
让我知道你面对的问题,如果你还在挣扎...... –
如果我使用FlexboxLayout然后,classcast例外情况会发生,你只能在行布局中使用Imageview,但我需要在中心imageview顶部对齐3个图像视图,这是不可能使用Flex布局,第二个问题是我想实现上面问题中张贴的曲折布局,但是直到现在,我可以调整前两个位置,但不是第三,Flexbox布局表现奇怪 – Shruti