巴士座位预订管理

时间:2016-05-02 09:05:32

标签: android android-recyclerview recycler-adapter android-gridlayout gridlayoutmanager

目前我在URL -

XML数据格式下面得到这个
<Seat>
<ColumnNo>0</ColumnNo>
<Deck>2</Deck>
<Height>2</Height>
<IsAvailable>1</IsAvailable>
<RowNo>0</RowNo>
<SeatLabel>E</SeatLabel>
<Width>1</Width>
</Seat>

<Seat>
<ColumnNo>1</ColumnNo> 
<Deck>1</Deck> 
<Height>2</Height> 
<IsAvailable>1</IsAvailable> 
<RowNo>0</RowNo>
<SeatLabel>SL5</SeatLabel> 
<Width>1</Width>
</Seat>

<Seat>
<ColumnNo>2</ColumnNo> 
<Deck>1</Deck> 
<Height>1</Height> 
<IsAvailable>1</IsAvailable> 
<RowNo>0</RowNo>
<SeatLabel/> 
<Width>1</Width>
</Seat>


<Seat>
<ColumnNo>3</ColumnNo> 
<Deck>1</Deck> 
<Height>2</Height> 
<IsAvailable>1</IsAvailable> 
<RowNo>0</RowNo>
<SeatLabel>SL1</SeatLabel> 
<Width>1</Width>
</Seat>

我已经制作了布局图像,我要点击空置座位并禁用可用的座位为0 ( <IsAvailable>0</IsAvailable> )

enter image description here

我很困惑,我应该如何制作以下版面,然后填充我得到的XML数据。

  • 如何制作座位布局。
  • 如何使用XML数据填充席位,点击后将转移到下一个活动以进行进一步处理。
  • 启用根据其可用性禁用座位。

我能够在我的代码中解析XML中的数据/值....我想知道如何制作布局格式并使这些XML值适合特定的席位。

2 个答案:

答案 0 :(得分:1)

使用表格布局进行设计。您可以使用此代码进行设计参考此link

enter image description here

{
    "_id": ObjectId("572992d6fc8b7a5c613248f6"),
    "userId": "0001",
    "fname": "Ankur",
    "lname": "Vishnoi",
    "address1": "Palam",
    "city": "New Delhi",
    "state": "Delhi",
    "pin": 110077,
    "cart": [
        {
            "productId": 8,
            "cartQty": 1
        }, 
        {
            "productId": 2,
            "cartQty": 3
        },
   ]
} 

答案 1 :(得分:1)

我就是这样做的,它仍处于测试阶段,我正在努力改进它。用户可以动态分配行中的行数和席位数(参见文本数据)

预测试代码可在Github

上找到

enter image description here enter image description here

enter image description here

包含测试数据的总线布局活动

package com.serveroverload.busbooking;

import com.example.busbooking.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BusLayoutActivity extends Activity {

    // Test Data Inputs
    private final int NUMBER_OF_SEATS_LEFT_UPPER = 5;
    private final int NUMBER_OF_SEATS_RIGHT_UPPER = 5;
    private final int NUMBER_OF_ROWS_LEFT_UPPER = 1;
    private final int NUMBER_OF_ROWS_RIGHT_UPPER = 2;

    private final int NUMBER_OF_SEATS_LEFT_LOWER = 8;
    private final int NUMBER_OF_SEATS_RIGHT_LOWER = 7;
    private final int NUMBER_OF_ROWS_LEFT_LOWER = 1;
    private final int NUMBER_OF_ROWS_RIGHT_LOWER = 3;

    private Double totatCost = 0.0;
    private int totalSeats = 0;
    private TextView totalPrice;
    private TextView totalBookedSeats;

    private LayoutParams seatParams;

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

        // Fetch Required Layouts
        RelativeLayout busLayoutUpper = (RelativeLayout) findViewById(R.id.upper_deck);
        RelativeLayout busLayoutBottom = (RelativeLayout) findViewById(R.id.lower_deck);
        totalPrice = (TextView) findViewById(R.id.total_cost);
        totalBookedSeats = (TextView) findViewById(R.id.total_seats);

        // Layout Param for Seats
        seatParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        seatParams.weight = 1;
        seatParams.leftMargin = 5;
        seatParams.rightMargin = 5;

        // Add Bottom Seats
        addLeftSeats(NUMBER_OF_ROWS_LEFT_UPPER, NUMBER_OF_SEATS_LEFT_UPPER, busLayoutBottom);
        addRightSeats(NUMBER_OF_ROWS_RIGHT_UPPER, NUMBER_OF_SEATS_RIGHT_UPPER, busLayoutBottom);

        // Add Upper Seats
        addLeftSeats(NUMBER_OF_ROWS_LEFT_LOWER, NUMBER_OF_SEATS_LEFT_LOWER, busLayoutUpper);
        addRightSeats(NUMBER_OF_ROWS_RIGHT_LOWER, NUMBER_OF_SEATS_RIGHT_LOWER, busLayoutUpper);

    }

    /*
     * This function draw seats for right row
     */
    private void addRightSeats(int numberOfRowsRight, float numberOfSeatsInRow, ViewGroup busLayout) {
        int previousRow;

        // ADD RIGHT ROWS

        previousRow = 0;

        // Begin adding rows
        for (int rightRowCount = 0; rightRowCount < numberOfRowsRight; rightRowCount++) {

            // Adding Linear layout as row
            LinearLayout rightRow = new LinearLayout(getApplicationContext());
            rightRow.setGravity(Gravity.CENTER);
            rightRow.setId(100 + rightRowCount);

            // make seats equal distance
            rightRow.setWeightSum(numberOfSeatsInRow);

            // if it is first row add row to window (ALIGN_PARENT_BOTTOM)
            // else add row above window row (ABOVE)
            if (previousRow != rightRowCount) {
                RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                newParams.addRule(RelativeLayout.ABOVE, 100 + previousRow);
                newParams.setMargins(10, 10, 10, 10);
                rightRow.setLayoutParams(newParams);

            } else {

                // Layout params for first row (Window seat), adding margin and
                // bottom alignment
                RelativeLayout.LayoutParams rightRowParam = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                rightRowParam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                rightRowParam.setMargins(10, 10, 10, 10);
                rightRow.setLayoutParams(rightRowParam);
            }

            // Add Seats in row we have added
            for (int rowCount = 0; rowCount <= numberOfSeatsInRow; rowCount++) {

                // Left Upper
                final Seat rightRowSeat = (Seat) LayoutInflater.from(this).inflate(R.layout.seat, null);
                rightRowSeat.setLayoutParams(seatParams);
                rightRowSeat.setSeatNumber("SL" + rowCount);
                rightRowSeat.setGravity(Gravity.CENTER);

                rightRowSeat.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        updateCost(rightRowSeat);

                        Toast.makeText(getApplicationContext(), "seat # " + rightRowSeat.getSeatNumber(), 300).show();

                    }
                });

                // add seat to row
                rightRow.addView(rightRowSeat);

            }

            // and then add row to bus layout
            busLayout.addView(rightRow);

            // update row counter
            previousRow = rightRowCount;

        }
    }

    /*
     * This function draw seats for left row
     */
    private void addLeftSeats(int numberOfRowsLeft, float nuumberOfSeatsInRow, ViewGroup busLayout) {
        // Adding Left side rows

        int previousRow = 0;

        for (int leftRowCount = 0; leftRowCount < numberOfRowsLeft; leftRowCount++) {

            // Adding Linear layout as row
            LinearLayout LeftRow = new LinearLayout(getApplicationContext());
            LeftRow.setGravity(Gravity.CENTER);
            LeftRow.setId(leftRowCount);

            // Equi distance seats
            LeftRow.setWeightSum(nuumberOfSeatsInRow);

            // if it is first row add row to window
            // else add row below window row (BELOW)
            if (previousRow != leftRowCount) {
                RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                newParams.addRule(RelativeLayout.BELOW, previousRow);
                newParams.setMargins(10, 10, 10, 10);
                LeftRow.setLayoutParams(newParams);

            } else {
                RelativeLayout.LayoutParams leftRowParam = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                leftRowParam.setMargins(10, 10, 10, 10);
                LeftRow.setLayoutParams(leftRowParam);
            }

            // Add Seats in row
            for (int rowCount = 0; rowCount <= nuumberOfSeatsInRow; rowCount++) {

                // Left Upper
                final Seat leftRowSeat = (Seat) LayoutInflater.from(this).inflate(R.layout.seat, null);
                leftRowSeat.setLayoutParams(seatParams);
                leftRowSeat.setSeatNumber("SL " + rowCount);
                leftRowSeat.setGravity(Gravity.CENTER);

                leftRowSeat.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        updateCost(leftRowSeat);

                        Toast.makeText(getApplicationContext(), "seat # " + leftRowSeat.getSeatNumber(), 300).show();
                    }
                });

                LeftRow.addView(leftRowSeat);

            }

            // add row to bus layout
            busLayout.addView(LeftRow);

            // update row counter
            previousRow = leftRowCount;

        }
    }

    private void updateCost(final Seat ud_LeftWindowSeat) {

        if (ud_LeftWindowSeat.setSelected()) {

            totatCost += ud_LeftWindowSeat.getSeatPrice();
            ++totalSeats;

        } else {
            totatCost -= ud_LeftWindowSeat.getSeatPrice();
            --totalSeats;
        }

        totalPrice.setText("" + totatCost);
        totalBookedSeats.setText("" + totalSeats);
    }
}

座位的自定义TextView

package com.serveroverload.busbooking;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class Seat extends TextView {

    public Seat(Context context) {
        super(context);

    }

    public Seat(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public Seat(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    private String seatNumber;
    private Boolean isSelected = false;
    private Boolean isWomen = false;
    private Double seatPrice = 100.00;

    public Boolean getIsWomen() {
        return isWomen;
    }

    public void setIsWomen(Boolean isWomen) {
        this.isWomen = isWomen;
    }

    public Double getSeatPrice() {
        return seatPrice;
    }

    public void setSeatPrice(Double seatPrice) {
        this.seatPrice = seatPrice;
    }

    public Boolean getIsSelected() {
        return isSelected;
    }

    public Boolean setSelected() {

        this.isSelected = !this.isSelected;

        if (getIsSelected()) {
            setBackgroundColor(0xff99cc00);

        } else {
            setBackgroundColor(0xff33b5e5);

        }

        return isSelected;

    }

    public String getSeatNumber() {
        return seatNumber;
    }

    public void setSeatNumber(String seatNumber) {
        this.seatNumber = seatNumber;

        setText(seatNumber);
    }

}

活动布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF"
        android:orientation="vertical"
        android:weightSum="2"
        tools:context="com.serveroverload.busbooking.MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="Lower Deck"
            android:textColor="#F00"
            android:textSize="20sp" />

        <RelativeLayout
            android:id="@+id/upper_deck"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/bus_outline"
            android:minHeight="150dp" >
        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="Upper Deck"
            android:textColor="#F00"
            android:textSize="20sp" />

        <RelativeLayout
            android:id="@+id/lower_deck"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:background="@drawable/bus_outline"
            android:minHeight="150dp" >
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/total_seats_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Number Of Seats"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/total_seats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:textColor="#0F0"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/total_cost_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Total Cost"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/total_cost"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:textColor="#0F0"
                android:textSize="20sp" />
        </RelativeLayout>
    </LinearLayout>

</ScrollView>

布局充气seat.xml的简单座位布局

<com.serveroverload.busbooking.Seat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:background="@android:color/holo_blue_light"
    android:gravity="center"
    android:textColor="#000"
    android:textSize="20sp" />

总线轮廓形状drawable bus_outline.xml

<corners
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="0dp"
    android:radius="2dp"
    android:topRightRadius="0dp" />

<stroke
    android:width="2dp"
    android:color="@android:color/holo_orange_dark" />