Simple Android RecyclerView example

前端 未结 11 1436
名媛妹妹
名媛妹妹 2020-11-22 02:03

I\'ve made a list of items a few times using Android\'s RecyclerView, but it is a rather complicated process. Going through one of the numerous tutorials online

11条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 02:48

    Based on different sources I have created Simple Implementation of RecyclerView using a Simple Library.

    Add this line in build.gradle

    implementation 'com.hereshem.lib:awesomelib:2.0.1'
    

    AddCreate a RecyclerView by adding MyRecyclerView in activity_main.xml with

    
    

    Now in the MainActivity, Create a ViewHolder by passing the name of Class that needs to bind

    public static class EVHolder extends MyViewHolder {
        TextView date, title, summary;
        public EVHolder(View v) {
            super(v);
            date = v.findViewById(R.id.date);
            title = v.findViewById(R.id.title);
            summary = v.findViewById(R.id.summary);
        }
        @Override
        public void bindView(Events c) {
            date.setText(c.date);
            title.setText(c.title);
            summary.setText(c.summary);
        }
    }
    

    Create Items list variable and adapters with very few lines by passing items, class and layout in the adapter

    List items = new ArrayList<>();
    MyRecyclerView recycler = findViewById(R.id.recycler);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, items, EVHolder.class, R.layout.row_event);
    recycler.setAdapter(adapter);
    

    ClickListener can be added with following lines

    recycler.setOnItemClickListener(new MyRecyclerView.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Toast.makeText(MainActivity.this, "Recycler Item Clicked " + position, Toast.LENGTH_SHORT).show();
        }
    });
    

    Its all done.

    More example and implementation can be found here . Hope this helps !!!

提交回复
热议问题