简体   繁体   中英

How to make recycler view horizontal and like table layout?

I have list data recycler view and I want to make it like a table layout and dynamical. Here is the example view of the table that I want to make: https://ibb.co/q7zYM64

And this is my condition app : https://ibb.co/JKXwthj

and this is my main_activity.java :

public class MainActivity extends AppCompatActivity {

private static final String URL_DATA = "http://sipermata.bappedawaykanan.com/api/admin/jumlah_penduduk_kabupaten/list?year=2019";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

private List<ListItem> listItems;

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

    recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    listItems = new ArrayList<>();

    loadRecyclerViewData();

}

private void loadRecyclerViewData(){
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading Data...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        JSONArray array = jsonObject.getJSONArray("data");
                        //JSONArray array = new JSONArray(s);

                        for (int i = 0 ; i<array.length(); i++){
                            JSONObject o = array.getJSONObject(i);
                            String id = o.getString("id");
                            String kec = o.getString("kecamatan");
                            String avg = o.getString("avg");
                            //String tahun = o.getString("year");

                            JSONObject isitahun = o.getJSONObject("year");
                            Iterator<String>itr = isitahun.keys();
                            //for(int j = 0; j<=isitahun.length();j++){
                            while (itr.hasNext()){
                                String key = itr.next();
                               JSONObject a = isitahun.getJSONObject(key);
                               String tahun = a.getString("value");

                                ListItem item = new ListItem(
                                        id, kec, avg, key, tahun
                                );
                                listItems.add(item);
                            }

                        }
                        adapter = new MyAdapter(listItems,getApplicationContext());
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            progressDialog.dismiss();
            Toast.makeText(getApplicationContext(), volleyError.getMessage(),Toast.LENGTH_LONG).show();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

}

You can set layout as horizontal.

recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManager);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM