gpt4 book ai didi

android - 在 recyclerview Gridlayoutmanager 中创建总线布局时出现间距问题

转载 作者:可可西里 更新时间:2023-11-01 18:49:07 26 4
gpt4 key购买 nike

我正在使用带有 GirdLayout 管理器的回收器 View 来创建总线布局

我的问题是间距,我根据行列填充数据

这就是我获取布局的方式 enter image description here

这就是我想要的布局: enter image description here

我想要第 3 行和第 2 列的元素,位于第 2 行第 0 列的卧铺座位旁边,如图所示我怎样才能删除那个空间,项目应该根据其上面的项目容纳。

 customGridAdapter = new CustomGridViewAdapter(getActivity(), busSeatModel, false, fareCategory, BusSeatLayout.this);
RtlGridLayoutManager gridLayoutManager = new RtlGridLayoutManager(getActivity(), busSeatModel.getMaxLowerColumn());
seatLayout.setLayoutManager(gridLayoutManager);
seatLayout.setAdapter(customGridAdapter);

这是我的 customGridAdapter onBindViewHolder

public class CustomGridViewAdapter extends RecyclerView.Adapter<CustomGridViewAdapter.MyViewHolder> {

Context context;
BusSeatModel busSeatModel;
HashMap<Integer, HashMap<Integer, BusSeatItemModel>> data = new HashMap<>();
LayoutInflater inflater;
boolean upper;
HashMap<Integer, BusSeatItemModel> seatLowerModels;
BusSeatItemModel lowerModel;
int maxColumn = 0;
int maxRow = 0;
BusSeatLayout busSeatLayout;
int fare;

public CustomGridViewAdapter(Context context, BusSeatModel busSeatModel, boolean upper, int fare, BusSeatLayout busSeatLayout) {
this.context = context;
this.busSeatModel = busSeatModel;
inflater = LayoutInflater.from(context);
this.fare = fare;
this.busSeatLayout = busSeatLayout;
this.upper = upper;
if (upper) {
data = busSeatModel.getBusSeatUpperModels();
maxColumn = busSeatModel.getMaxUpperColumn();
maxRow = busSeatModel.getMaxUpperRow();
} else {
data = busSeatModel.getBusSeatLowerModels();
maxColumn = busSeatModel.getMaxLowerColumn();
maxRow = busSeatModel.getMaxLowerRow();
}
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = inflater.inflate(R.layout.seatrow_grid, parent, false);

MyViewHolder myViewHolder = new MyViewHolder(view);

return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
try {

int row = GetRow(position);
int column = GetCol(position);

Log.d(" Row Column ", "" + row + " " + column);

seatLowerModels = new HashMap<>();

if (data.containsKey(row)) {
seatLowerModels = data.get(row);
if (seatLowerModels.containsKey(column)) {
lowerModel = seatLowerModels.get(column);
Log.v(" same fare ", " model fare " + lowerModel.getBaseFare() + " category selected " + fare);
if (fare == -1) {
Log.v(" fare is all ", "++++ ");

holder.imageItem.setImageResource(lowerModel.getDrawableName(false));

} else {
Log.v(" fare is not all ", "");
if (lowerModel.getBaseFare() == fare) {
Log.v(" fare is same ", "");
holder.imageItem.setImageResource(lowerModel.getDrawableName(false));
} else {
Log.v(" fare is diff ", "");
holder.imageItem.setImageResource(lowerModel.getDrawableName(true));
}
}
holder.imageItem.setVisibility(View.VISIBLE);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}


private int GetCol(int pos) {
int col = pos % (maxColumn);
return col;
}

private int GetRow(int pos) {
int row = pos / (maxColumn);
return row;
}

@Override
public int getItemCount() {
return maxColumn * maxRow;
}


public class MyViewHolder extends RecyclerView.ViewHolder {

ImageView imageItem;


public MyViewHolder(View itemView) {
super(itemView);


imageItem = (ImageView) itemView.findViewById(R.id.item_image);}
}

这是回收站

<RelativeLayout
android:id="@+id/rlRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center">

<android.support.v7.widget.RecyclerView
android:id="@+id/rvFareCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#ffffff"
android:paddingBottom="6dip"
android:paddingTop="8dip"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

和座位布局xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:padding="4dp"
android:visibility="invisible"></ImageView>

</RelativeLayout>

最佳答案

当然,问题出在您的元素不具有相同高度这一事实。您需要为元素分配不同的行跨度。我建议为较高的元素(左侧的元素)分配 2 的行跨度,为较小的元素分配 1。

我认为你可以通过一些技巧来做到这一点,比如将你的网格布局管理器设置为水平方向并使用 GridLayoutManager.setSpanSizeLookup(..) 来控制跨度(因为你现在是水平的,跨度将作用于行)。这可能需要您重新设计从后备数组中获取元素的逻辑。另一种选择是(据我所知)实现您自己的自定义布局管理器。

关于android - 在 recyclerview Gridlayoutmanager 中创建总线布局时出现间距问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39411952/

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