我正在创建座位选择屏幕,但我很困惑,我该如何实现这一观点。我想将所有选定的席位访问到一个单独的Java文件中。 Plz,帮助我
编辑:我已经尝试过此代码,但是使用此代码,我无法在单个gridview中创建这样的视图。现在我使用2个单独的网格视图和两个带有单独适配器的Java文件。Seat_Select.java
public class Seat_Select extends AppCompatActivity {
GridView androidgridview;
int[] image = {
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
R.drawable.rect_select, R.drawable.rect_select,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat__select);
androidgridview = findViewById(R.id.grid);
SeatAdapter seatAdapter=new SeatAdapter(Seat_Select.this,image);
androidgridview.setAdapter(seatAdapter);
androidgridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), "Grid Item " + (position + 1) + " Selected", Toast.LENGTH_LONG).show();
}
});
}
SeatAdapter.java
public class SeatAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
int image[];
public SeatAdapter(Context context,int[] image) {
this.context = context;
this.image=image;
layoutInflater=(LayoutInflater.from(context));
}
@Override
public int getCount() {
return image.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView =layoutInflater.inflate(R.layout.grid2col,null);
ImageView imageView= convertView.findViewById(R.id.grid_image);
imageView.setImageResource(image[position]);
return convertView;
}
}
grid2col.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/grid_image"
android:layout_width="30dp"
android:layout_height="70dp">
</ImageView>
</LinearLayout>
activity_seat_select.xml
<RelativeLayout>
<android.support.v7.widget.CardView
android:layout_width="190dp"
android:layout_height="390dp"
android:layout_below="@+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp">
<GridView
android:id="@+id/grid"
android:layout_width="70dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:columnWidth="30dp"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:horizontalSpacing="1dp"
android:verticalSpacing="8dp"
/>
<GridView
android:id="@+id/grid2"
android:layout_width="35dp"
android:numColumns="1"
android:layout_height="match_parent"
android:layout_gravity="start"
android:gravity="center">
</GridView>
</android.support.v7.widget.CardView>
</RelativeLayout>
答案 0 :(得分:1)
请检查SeatBookingRecylerView。此示例具有完整的源代码。它可能对你有所帮助。这是使用RecyclerView
。
答案 1 :(得分:0)
<强> GridView的强>
GridView是一个ViewGroup,它以二维可滚动网格显示项目。使用ListAdapter将网格项自动插入到布局中。您可以在布局文件中使用GridView,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:numColumns="auto_fit"
android:verticalSpacing="16dp"
android:horizontalSpacing="16dp"
android:padding="16dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<强> GridLayout的强>
网格布局主要用于在单元格中对齐子视图,单元格是不可见线条的水平和垂直截距。每个视图子项都放在一个单元格中,单元格用水平和垂直索引编号。
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="3"
android:orientation="horizontal">
<Space />
<Button
android:id="@+id/button1"
android:layout_gravity="left|top"
android:text="@string/button_name" />
<Button
android:id="@+id/button3"
android:layout_gravity="left|top"
android:text="@string/button_name" />
<Button
android:id="@+id/button2"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:text="@string/button_name" />
<Button
android:id="@+id/button4"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="2"
android:text="@string/button_name" />
<Button
android:id="@+id/button5"
android:layout_column="1"
android:layout_row="2"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="@string/button_name" />
</GridLayout>