Thursday, October 4, 2018

Android RecyclerView with LoadMore and MultiView Layout using simple library

Android RecyclerView is one of the toughest topics for the Android beginners.
Many more tutorials related to RecyclerView are found on the internet along with the Android Developer Blog itself.

So not writing more about the introduction part, lets go on implementation of RecyclerView with a simple Library called awesomelib2.1.2.

Add the following line in dependencies part of build.gradle in app directory.
implementation 'com.hereshem.lib:awesomelib:2.1.2'



Now we are ready for the code implementation

Simple RecyclerView Implementation

Add RecyclerView in the layout file using the following
<com.hereshem.lib.recycler.MyRecyclerView
     android:id="@+id/recycler" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     app:layoutmanager="LinearLayoutManager" />
Create View Holder extending MyViewHolder from Library with the type of Class to bind them. Here Event class is bounded to the ViewHolder using the following
public static class EVHolder extends MyViewHolder<Events> {
    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 e) {
        date.setText(e.date);
        title.setText(e.title);
        summary.setText(e.summary);
    }
}
Now we are ready for creating RecyclerView Adapter
List<Events> items = new ArrayList<>();
MyRecyclerView recycler = findViewById(R.id.recycler);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, items, EVHolder.class, R.layout.row_event);
recycler.setAdapter(adapter);
Moreover - adding click listener and load more
recycler.setOnItemClickListener(new MyRecyclerView.OnItemClickListener() {
    @Override
    public void onItemClick(int position) {
        Toast.makeText(MainActivity.this, "Recycler Item Clicked " + position, Toast.LENGTH_SHORT).show();
    }
});

recycler.setOnLoadMoreListener(new MyRecyclerView.OnLoadMoreListener() {
    @Override
    public void onLoadMore() {
        loadData();
    }
});
loadData();
That's it, doesn't it seems to be easy?? 😊😊

Multi Layout RecyclerView

Create some more ViewHolders
public static class TVHolder extends MyViewHolder<String> {
    TextView title;
    public TVHolder(View v) {
        super(v);
        title = v.findViewById(R.id.title);
    }
    @Override
    public void bindView(String c) {
        title.setText(c);
    }
}

public static class DVHolder extends MyViewHolder<Integer> {
    TextView title;
    public DVHolder(View v) {
        super(v);
    }
    @Override
    public void bindView(Integer c) {
    }
}
Now define holder with MultiLayoutHolder where the multiple class data type is bounded with their respective ViewHolder and their layouts.
MultiLayoutHolder holders = new MultiLayoutHolder()
                .add(Events.class, EVHolder.class, R.layout.row_event)
                .add(String.class, TVHolder.class, R.layout.row_simple)
                .add(Integer.class, DVHolder.class, R.layout.row_divider);
Finally create an adapter with simple line of codes
List<Object> items = new ArrayList<>();
MultiLayoutAdapter adapter = new MultiLayoutAdapter(this, items, holders);
recycler.setAdapter(adapter);
With these lines MultiLayout is automatically selected with the type of data added to the items.
items.add(new Events(...)); binds the Event class item with EVHolder and row_event layout.
items.add("Hem Shrestha"); binds the String with TVHolder and row_simple layout.
items.add(26); binds Integer item with DVHolder and row_divider layout.

Visit this Github repo for Full Source Code and more implementation examples.

MultiLayout in RecyclerView is very complex task which can be easily implemented using the library as mentioned above.

If you have any comments about the library or want to contribute on it please send a pull request to this repository.

Happy Coding. !!!

No comments:

Post a Comment