无法创建所需的布局xml(Android)

时间:2018-05-03 05:03:38

标签: android xml gridview layout android-recyclerview

我正在开发一个巴士预订应用程序。我希望在下面给出座位预订布局。

Like this

And I am getting this

每行将有4个座位,如何在第2和第3列之间留下1个空白行,在最后一行中它应显示所有5个座位。

我的布局没有格式化为ineed。

SeatSelection活动

public class SeatSelection extends AppCompatActivity implements AdapterView.OnItemClickListener{

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
ArrayList<Item> gridArray = new ArrayList<Item>();
TextView txtSeatNo;
ImageView imageView;
public Bitmap seatIcon;
public Bitmap seatSelect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seat_selection);

    seatIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_avl);
    seatSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_std);

    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 5);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new SeatAdapter();
    recyclerView.setAdapter(adapter);


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Item item = gridArray.get(position);
    Bitmap seatcompare = item.getImage();
    if (seatcompare == seatIcon)
    {
        seatSelected(position);
    }
    else
    {
        seatDeselected(position);

    }
}

SeatAdapter seatAdapter;
public void seatSelected(int pos)
{
    gridArray.remove(pos);
    gridArray.add(pos, new Item(seatSelect, "Selected"));
    seatAdapter.notifyDataSetChanged();
}

public void seatDeselected(int pos)
{
    gridArray.remove(pos);
    int i = pos + 1;
    gridArray.add(pos, new Item(seatIcon, "Seat" + i));
    seatAdapter.notifyDataSetChanged();
}
public class SeatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    class ViewHolderOriginal extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView textView;
        ViewHolderOriginal(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.item_image);
            textView = (TextView) itemView.findViewById(R.id.item_text);
        }
    }

    class ViewHolderBlank extends RecyclerView.ViewHolder{
        ViewHolderBlank(View itemView) {
            super(itemView);

        }

    }
    // 0= Original view 1= Blank View
    @Override
    public int getItemViewType(int position) {
        if((getItemCount()-(position-1))<5)
            return 0;
        else
            return position % 5 == 2 ? 1 : 0;
    }

    @Override
    public int getItemCount() {
        return 60;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (viewType == 1)
            return new ViewHolderBlank(inflater.inflate(R.layout.item_blank, parent, false));
        else
            return new ViewHolderOriginal(inflater.inflate(R.layout.seatrow_grid, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolderBlank) {

        } else if (holder instanceof ViewHolderOriginal) {

        }
    }

}
}

物品

public class Item
 {
public Bitmap image;
public String title;
public boolean isSelected;


public boolean isSelected()
{
    return isSelected;
}


public void setSelected(boolean isSelected)
{
    this.isSelected = isSelected;
}


public Item(Bitmap image, String title)
{
    super();
    this.image = image;
    this.title = title;
}


public Bitmap getImage()
{
    return image;
}

public void setImage(Bitmap image)
{
    this.image = image;
}

public String getTitle()
{
    return title;
}

public void setTitle(String title)
{
    this.title = title;
}

}

1 个答案:

答案 0 :(得分:1)

简单逻辑是为中间列添加黑色值。

1)如何为GridLayoutManger找到中间列,其中列数为5。

if (position % 5 == 2) {...}

条件适用于你的中间栏目。

2)使用getItemViewType标识添加空白视图或原始视图

@Override
    public int getItemViewType(int position) {
        return position % 5 == 2 ? 1 : 0;
    }

3)不需要在最后一行添加黑色视图,因此修改逻辑

// 0= Original view 1= Blank View
  @Override
            public int getItemViewType(int position) {
                if((getItemCount()-(position-1))<5)
                return 0;
                else
                return position % 5 == 2 ? 1 : 0;
            }

4)在onCreateViewHolder

中使用ViewType
 @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (viewType == 1)
            return new ViewHolderBlank(inflater.inflate(R.layout.item_blank, parent, false));
        else
            return new ViewHolderOriginal(inflater.inflate(R.layout.item_add_orignal, parent, false));
}

5)检查onBindViewHolder中的viewHolder类型并执行逻辑

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof ViewHolderBlank) {

    } else if (holder instanceof ViewHolderOriginal) {

    }
}

6)添加我们删除的额外项目为空白视图

 @Override
    public int getItemCount() {
        return totalItemList.size()+(totalItemList.size()/5);
    }

当您尝试访问totalItemList.get(position)时,您必须管理黑色条目,否则会导致异常

输出

enter image description here