MCPcopy Index your code
hub / github.com/drstranges/DataBinding_For_RecyclerView

github.com/drstranges/DataBinding_For_RecyclerView @v0.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.1.0 ↗ · + Follow
182 symbols 393 edges 35 files 63 documented · 35%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ActionHandler

Release

Overview

This library provides simple and flexible way to configuration RecyclerView using android data binding.

Add library as gradle dependency

repositories { 
    jcenter()
}
dependencies {
    compile 'com.drextended.rvdatabinding:rvdatabinding:0.1.0'
}

Features

  • .ListConfig - Simple builder for configuration RecyclerView.
  • .BindableAdapter - Simple Recycler View Adapter for using with data-binding.
  • .BindingHolder - Simple View Holder for using with data-binding.
  • .DividerItemDecoration - Simple divider decoration for RecyclerView.
  • .ColorDividerItemDecoration - Simple colored divider decoration for RecyclerView.
  • .LoadMoreScrollListener - Simple RecyclerView ScrollListener for implementing lazy loading list (endless list).
  • .TwoWayLoadingScrollListener - Simple RecyclerView ScrollListener for implementing two-way lazy loading list (endless list).
  • .BaseBindingAdapterDelegate - Base RecyclerView item delegate for using with data-binding.
  • .ModelItemDelegate - Generic ActionDelegate. You can use this one if you do not want to implement custom one.

Usage

1. At first you definitely should have pojo-models for items

    public class User implements BaseModel {
        public String name;
        public String avatar;
    }
    public class Location implements BaseModel {
        public String name;
        public String image;
    }
    public class Advertisement implements BaseModel {
        public String label;
        public String image;
    }

2. Then you should create layouts for each item and for your list page

fragment_page.xml

    <layout ...>
        <data>
            <variable
                name="viewModel"
                type="com.drextended.rvdbsample.viewmodel.ListViewModel"/>
        </data>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:listConfig="@{viewModel.getListConfig()}"/>

    </layout>

item_user.xml

    <layout xmlns:...>
        <data>
            <import type="com.drextended.rvdbsample.model.ActionType"/>

            <variable
                name="user"
                type="com.drextended.rvdbsample.model.User"/>

            <variable
                name="actionHandler"
                type="com.drextended.actionhandler.listener.ActionClickListener"/>
        </data>

        <android.support.v7.widget.CardView
            ...
            app:actionHandler="@{actionHandler}"
            app:actionType="@{ActionType.OPEN}"
            app:actionTypeLongClick="@{ActionType.MENU}"
            app:model="@{user}">

            <FrameLayout ...>

                <ImageView
                    ...
                    app:glideAnimation="@{R.anim.scale_in}"
                    app:glideCacheStrategy='@{"RESULT"}'
                    app:glidePath="@{user.avatar}"
                    app:glidePlaceholder="@{@android:drawable/ic_menu_gallery}"
                    app:glideTransform='@{"CIRCLE"}'
                    tools:src="https://github.com/drstranges/DataBinding_For_RecyclerView/raw/v0.1.0/@android:drawable/ic_menu_gallery"/>

                <TextView
                    ...
                    android:text="@{user.name}"/>

            </FrameLayout>
        </android.support.v7.widget.CardView>
    </layout>

The same for item_location.xml and for item_advertisement.xml

3. Then create ItemDelegates for items if you do not want to use generic

UserDelegate.java

    public class UserDelegate extends ActionAdapterDelegate<BaseModel, ItemUserBinding> {

        public UserDelegate(final ActionClickListener actionHandler) {
            super(actionHandler);
        }

        @Override
        public boolean isForViewType(@NonNull final List<BaseModel> items, final int position) {
            return items.get(position) instanceof User;
        }

        @NonNull
        @Override
        public BindingHolder<ItemUserBinding> onCreateViewHolder(final ViewGroup parent) {
            return BindingHolder.newInstance(R.layout.item_user, LayoutInflater.from(parent.getContext()), parent, false);
        }

        @Override
        public void onBindViewHolder(@NonNull final List<BaseModel> items, final int position, @NonNull final BindingHolder<ItemUserBinding> holder) {
            final User user = (User) items.get(position);
            holder.getBinding().setUser(user);
            holder.getBinding().setActionHandler(getActionHandler());
        }

        @Override
        public long getItemId(final List<BaseModel> items, final int position) {
            return items.get(position).getId();
        }
    }

4. Assemble all this in your viewmodel

AllInOneListViewModel.java

    // Used action-handler library for handling click/long click in items: https://github.com/drstranges/ActionHandler
    final ActionClickListener actionHandler = new ActionHandler.Builder()
            //.addAction(null, new SimpleAnimationAction()) // Applied for any actionType
            //.addAction(null, new TrackAction()) // Applied for any actionType
            .addAction(ActionType.OPEN, new OpenProfileAction())
            .addAction(ActionType.MENU, new CompositeAction<String>(
                    (context, model) -> getTitle(model),
                    new ActionItem(ActionType.MENU_ITEM_1, new OpenSecondActivity(), R.string.menu_item_1),
                    new ActionItem(ActionType.MENU_ITEM_2, new ShowToastAction(), R.string.menu_item_2)
                    ))
            .setActionInterceptor(this)
            .setActionFiredListener(this)
            .build();

    // Build generic bindable adapter. Just list all used item delegates
    mAdapter = new BindableAdapter<>(
            new UserDelegate(actionHandler), // you can create custom delegate
            //new ModelActionItemDelegate<BaseModel>(actionHandler, User.class, R.layout.item_user, BR.user), // or use generic
            new ModelActionItemDelegate<BaseModel>(actionHandler, Location.class, R.layout.item_location, BR.location),
            new ModelActionItemDelegate<BaseModel>(actionHandler, Advertisement.class, R.layout.item_advertisment, BR.advertisment)
    );

    // Config for RecyclerView
    mListConfig = new ListConfig.Builder(mAdapter)
            .setLayoutManagerProvider(new SimpleGridLayoutManagerProvider(mSpanCount, getSpanSizeLookup())) // if not set, LinearLayoutManager applied by default
            .addItemDecoration(new ColorDividerItemDecoration(color, spacing, SPACE_LEFT|SPACE_TOP, false))
            //.setDefaultDividerEnabled(true)
            .addOnScrollListener(new OnLoadMoreScrollListener(mCallback))
            .setItemAnimator(getCustomItemAnimator())
//            .setHasFixedSize(true)
            .setItemTouchHelper(getItemTouchHelper())
            .build(context);

    loadData();

Used Library:

Inspired by:

https://github.com/sockeqwe/AdapterDelegates

http://hannesdorfmann.com/android/adapter-delegates

License

Copyright 2016 Roman Donchenko

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Extension points exported contracts — how you extend this code

LayoutManagerProvider (Interface)
The provider of LayoutManager for RecyclerView [6 implementers]
rvdatabinding/src/main/java/com/drextended/rvdatabinding/ListConfig.java
BaseModel (Interface)
Base model [7 implementers]
sample/src/main/java/com/drextended/rvdbsample/model/BaseModel.java
IdHolder (Interface)
Interface to help BindableAdapter getting itemId for items You must implementing this interface in you custom AdapterDel [5 …
rvdatabinding/src/main/java/com/drextended/rvdatabinding/delegate/IdHolder.java
ListViewModel (Interface)
Base viewmodel [6 implementers]
sample/src/main/java/com/drextended/rvdbsample/viewmodel/ListViewModel.java
ViewTypeClause (Interface)
(no doc) [6 implementers]
rvdatabinding/src/main/java/com/drextended/rvdatabinding/delegate/ModelItemDelegate.java
SimpleCallback (Interface)
The simple callback [2 implementers]
sample/src/main/java/com/drextended/rvdbsample/util/SimpleCallback.java
OnLoadMoreListener (Interface)
Listener for implementing lazy loading list (endless list)
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/LoadMoreScrollListener.java
OnLoadMoreListener (Interface)
(no doc)
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/TwoWayLoadingScrollListener.java

Core symbols most depended-on inside this repo

get
called by 16
rvdatabinding/src/main/java/com/drextended/rvdatabinding/ListConfig.java
getAdvertisment
called by 10
sample/src/main/java/com/drextended/rvdbsample/util/DummyDataProvider.java
getBinding
called by 9
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/BindingHolder.java
build
called by 5
rvdatabinding/src/main/java/com/drextended/rvdatabinding/ListConfig.java
newInstance
called by 4
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/BindingHolder.java
check
called by 4
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/DividerItemDecoration.java
needDrawIfFirst
called by 4
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/DividerItemDecoration.java
needDrawIfLast
called by 4
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/DividerItemDecoration.java

Shape

Method 137
Class 36
Interface 9

Languages

Java100%

Modules by API surface

rvdatabinding/src/main/java/com/drextended/rvdatabinding/ListConfig.java24 symbols
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/TwoWayLoadingScrollListener.java12 symbols
rvdatabinding/src/main/java/com/drextended/rvdatabinding/delegate/ModelItemDelegate.java10 symbols
sample/src/main/java/com/drextended/rvdbsample/viewmodel/LocationListViewModel.java8 symbols
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/LoadMoreScrollListener.java8 symbols
rvdatabinding/src/main/java/com/drextended/rvdatabinding/adapter/DividerItemDecoration.java8 symbols
sample/src/main/java/com/drextended/rvdbsample/viewmodel/UserListViewModel.java7 symbols
sample/src/main/java/com/drextended/rvdbsample/view/PageFragment.java7 symbols
sample/src/main/java/com/drextended/rvdbsample/viewmodel/delegate/UserDelegate.java6 symbols
sample/src/main/java/com/drextended/rvdbsample/viewmodel/delegate/LocationDelegate.java6 symbols
sample/src/main/java/com/drextended/rvdbsample/viewmodel/delegate/AdvertisementDelegate.java6 symbols
sample/src/main/java/com/drextended/rvdbsample/viewmodel/AllInOneListViewModel.java6 symbols

For agents

$ claude mcp add DataBinding_For_RecyclerView \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page