android 在回收器视图网格布局管理器中创建总线布局时出现间距问题

u5rb5r59  于 2023-03-21  发布在  Android
关注(0)|答案(2)|浏览(164)

我正在使用GirdLayout管理器的回收器视图创建总线布局。我的问题是间距,我根据行列填充数据。
我是这样设计的:

这就是我想要的布局:

我想要第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);

这是我的customGridAdapteronBindViewHolder

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);}
    }
}

这是recyclerview

<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>
b91juud3

b91juud31#

当然,问题是因为元素的高度不同,需要给元素分配不同的行跨度,我建议给较高的元素(左边的元素)分配行跨度2,给较小的元素分配行跨度1。
我认为你可以通过一个技巧来实现它,比如设置你的网格布局管理器为水平方向,并使用GridLayoutManager.setSpanSizeLookup(..)来控制跨度(因为你的网格布局管理器现在是水平的,所以跨度将作用于行),这可能需要你重新设计从支持数组中获取元素的逻辑,另一种选择恐怕是(据我所知)实现你自己的定制布局管理器。

pw136qt2

pw136qt22#

我建议将回收器视图条目布局实现为TableLayoutGridLayout,它们在定义行/列跨度方面提供了更大的灵活性。
一些有用的链接:
http://android-pro.blogspot.com.eg/2010/02/table-layout.htmlhttps://developer.android.com/guide/topics/ui/layout/grid.html

相关问题