我有类似的东西,我用新的 RecyclerView 解决了。
我创建了一个带有 a 的片段回收视图 https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html。
xml 上的 RecyclerView:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/filter_subtypes" android:layout_width="match_parent" android:layout_height="match_parent" />
在您的片段/活动上(在我的片段案例中为 OnViewCreated)。
我找到 RecyclerView 并设置一个适配器 - 一个继承自的普通适配器类RecyclerView.Adapter-
然后我创建一个 GridLayoutManager
final GridLayoutManager mng_layout = new GridLayoutManager(this.getActivity(), TOTAL_CELLS_PER_ROW/*In your case 4*/);
然后我重写此方法来设置动态的列数(单元格)
mng_layout.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch( adapterSubtype.getItemViewType(position) ) {
case FilterSubtypesAdapter.TYPE_LOW:
return TOTAL_CELLS_PER_ROW;
case FilterSubtypesAdapter.TYPE_HIGH:
return 2;
default:
return -1;
}
}
});
myRecyclerView.setLayoutManager(mng_layout);
这样,您将获得行上单元格的动态数量。
额外的:
然后,如果您在适配器上使用相同的视图/类型视图,您将获得相同的 w & h 视图。您需要为 TYPE_HIGH 创建 2 个 xml 视图,并为 TYPE_LOW 创建其他视图。
因此,在您的适配器中,您需要有 2 种数据(1 种用于高图像,1 种用于低图像)。
您必须重写此方法
@Override
public SubtypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
if (viewType==TYPE_HIGH) {
view = inflater.inflate(R.layout.item_image_high, parent, false);
} else {
view = inflater.inflate(R.layout.item_image_low, parent, false);
}
return new SubtypeViewHolder(view, viewType);
}
@Override
public int getItemViewType(int position) {
return (list.get(position).getType()==Subtype_type.HIGH) ? TYPE_HIGH : TYPE_LOW;
}
我希望我说得很清楚,有任何问题请告诉我。