【问题标题】:How to use GridView or GridLayout?如何使用 GridView 或 GridLayout?
【发布时间】:2018-03-23 15:35:52
【问题描述】:

我正在创建一个座位选择屏幕,但我很困惑,我该如何实现这个视图。我想将所有选定的席位访问到一个 Java 文件中。请帮帮我

编辑:我已经尝试过这段代码,但是使用这段代码,我无法在单个 gridview 中创建这样的视图。现在我为此使用了 2 个单独的 gridviews 和两个带有单独适配器的 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>

【问题讨论】:

  • 你已经尝试过了吗?
  • 我已经更新了我的问题@Dumbo

标签: java android gridview grid-layout


【解决方案1】:

请查看SeatBookingRecylerView。这个例子有完整的源代码。它可能对你有帮助。这是使用RecyclerView

【讨论】:

    【解决方案2】:

    网格视图

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

    【讨论】:

    • 那么如何实现图像中显示的视图,当我使用**垂直和水平间距**时,它适用于所有列,但我不希望这样
    • 如果您从 web 服务获取项目,则使用 GridView 并创建自定义适配器来实现此目的,或者如果您想创建静态 UI,则使用 GridLayout。
    • 是的,先生,但我的问题是,使用 GridView 如何实现上面显示的结构。请帮帮我。
    猜你喜欢
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2020-12-18
    • 2014-08-02
    • 2012-04-18
    • 2016-10-19
    相关资源
    最近更新 更多