【问题标题】:How to make seat layouts like in redbus?如何制作像redbus一样的座位布局?
【发布时间】:2017-12-28 08:43:11
【问题描述】:

如何在每行的 3 个元素之后在网格视图内分配空间?

预期视图如下:

.

这是我的主要活动代码。 请检查我上面给出的屏幕 我想要在 android 中的网格视图内有一个空间,如何在每行的 3 个元素之后在网格视图内提供空间?

    public  void submit() {

        String URLL= "https://api.myjson.com/bins/14h7rp";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URLL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                     try {
                            JSONObject object=new JSONObject(response);
                            String status=object.getString("Status");
                            String Data=object.getJSONObject("Data").getString("Seat_details");
                            JSONArray jsonArray=new JSONArray(Data);
                            for (int i=0;i<jsonArray.length();i++){
                                JSONObject J=jsonArray.getJSONObject(i);
                                String seats=J.getString("seats");
                                JSONArray jsonArray1=new JSONArray(seats);
                                for (int k=0;k<jsonArray1.length();k++){
                                    JSONObject l=jsonArray1.getJSONObject(k);
                                    String id=l.getString("id");
                                    String name=l.getString("name");

                                    String bookedStatus=l.getString("bookedStatus");

                                    m=new Model();
                                    m.setId(id);
                                    m.setName(name);

                                    m.setBookedStatus(bookedStatus);
                                    arrayList.add(m);

                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter=new MainActivityAdapter(arrayList,MainActivity.this);
                        recyclerView.setAdapter(adapter);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
            @Override
    }

}

我的 Recylerview 初始化如下

 private void init_views() {
    recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
    int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
    recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),5);
    recyclerView.setLayoutManager(layoutManager);
   /* DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
            layoutManager.getOrientation(layoutManager));
    recyclerView.addItemDecoration(dividerItemDecoration);*/
}

【问题讨论】:

标签: android json gridview android-recyclerview


【解决方案1】:

你得到答案的方法之一,但它不是正确的方法

                      try {
                        JSONObject object=new JSONObject(response);
                        String status=object.getString("Status");
                        String Data=object.getJSONObject("Data").getString("Seat_details");
                      //  System.out.println("_______Data_________" + Data);
                        JSONArray jsonArray=new JSONArray(Data);
                        for (int i=0;i<jsonArray.length();i++){
                            JSONObject J=jsonArray.getJSONObject(i);
                            String seats=J.getString("seats");
                            JSONArray jsonArray1=new JSONArray(seats);

                            for (int k=0;k<jsonArray1.length();k++){
                                if (k%COLUMNS==3) {

                                    m=new Model();
                                    m.setId("");
                                    m.setName("");
                                    m.setSeatKey("");
                                    m.setRowNumber("");
                                    m.setColumnIndex("");
                                    m.setBookedStatus("2");
                                    arrayList.add(m);
                                }  else{
                                    JSONObject l=jsonArray1.getJSONObject(k);
                                    String id=l.getString("id");
                                    String name=l.getString("name");
                                    String seatKey=l.getString("seatKey");
                                    String rowNumber=l.getString("rowNumber");
                                    String columnIndex=l.getString("columnIndex");
                                    String bookedStatus=l.getString("bookedStatus");
                                    System.out.println("______name_________" + name);

                                    m=new Model();
                                    m.setId(id);
                                    m.setName(name);
                                    m.setSeatKey(seatKey);
                                    m.setRowNumber(rowNumber);
                                    m.setColumnIndex(columnIndex);
                                    m.setBookedStatus(bookedStatus);
                                    arrayList.add(m);

                                }


                            }

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

声明 COLUMN 变量

  private static final int COLUMNS = 6;

【讨论】:

  • 布局在哪里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2016-01-24
  • 2014-07-01
相关资源
最近更新 更多