在回收站视图中创建总线布局时出现间距问题Gridlayout管理器 [英] Spacing Issue while creating bus layout in recycler view Gridlayout manager

查看:103
本文介绍了在回收站视图中创建总线布局时出现间距问题Gridlayout管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GirdLayout Manager中使用回收站视图来创建公交车布局

I'm using recycler view with GirdLayout Manager for creating bus layout

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

my problem is with spacing I'm populating data according to row column

这就是我得到布局的方式

This is how I'm getting layout

这就是我希望我的布局是:

This is how i want my layout to be:

我希望第3行和第2列的项目在第2行第0列的卧铺座位旁边,如图所示 我该如何删除该空间,该项目应根据其上部项目容纳.

i want the item at row 3 and column 2 beside sleeper seat at row 2 column 0 like shown in pic how can i remove that space, item should accommodate according to its upper item.

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

这是我的customGridAdapter onBindViewHolder

this is my 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);}
}

}

这是recyclerview

this is the 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>

推荐答案

我建议将您的回收站视图项目布局实现为 GridLayout ,它们在定义行/列跨度方面提供了更大的灵活性.

I suggest implementing your recycler view item layout as a TableLayout or a GridLayout, they provide more flexibility in defining row/column spans.

一些有用的链接:

http://android-pro.blogspot.com.例如/2010/02/table-layout.html https://developer.android.com/guide/topics/ui/layout/grid.html

这篇关于在回收站视图中创建总线布局时出现间距问题Gridlayout管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆