Skip to content

Just use the ViewFinder instead of any ViewHolder

License

Notifications You must be signed in to change notification settings

vivchar/ViewFinder

Repository files navigation

ViewFinder Release API API

  • Just use the ViewFinder instead of any ViewHolder;
  • No more need to create a ViewHolder each time in your project;
  • The ViewFinder has the same features as a ViewHolder;
  • The ViewFinder can dynamically provide any link to any view;
  • So you can easily add a new view type to your list.

In few words: The ViewHolder has one function - to provide a link to the view in a layout. So when you use the RecyclerView and you need to add a new View Type in your list - you must create a new ViewHolder. It is fine when you have a couple types. But there is no need to create each time a new Holder. With this library you no need create a ViewHolder each time. The ViewFinder as a universal ViewHolder. It can provide a link to anyone view.

Example

import com.github.vivchar.viewfinder.ViewHolder;

public class YourAdapter extends RecyclerView.Adapter<ViewHolder> {

    @Override
    public ViewFinder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        return new ViewFinder(LayoutInflater.from(mContext).inflate(getLayoutID(viewType), parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        final BaseItem item = mItems.get(position);

        if (item instanceof YourItem) {
            YourItem yourItem = (YourItem)item;
        
            final SwitchCompat switchCompat = viewHolder.find(R.id.item_switch);

            viewHolder.getViewFinder()
                .find(R.id.custom_view, (ViewProvider<CustomView>) view -> {
                    ... 
                })
                .setText(R.id.item_name, yourItem.getName())
                .setBackgroundResource(R.id.item_logo, yourItem.getLogoResource())
                .setOnClickListener(v -> {
                    switchCompat.setChecked(!switchCompat.isChecked());
                    mListener.onItemClicked(yourItem);
                }));
        } else if (item instanceof OtherItem) {
            //No need to create new ViewHolder, you just use the ViewFinder again
            //See onCreateViewHolder() method, it has only one ViewHolder 
            viewHolder.getViewFinder()
                .find(...)
                .setVisible(...)
                .setText(...);
        }
    }
    
    private int getLayoutID(int viewType) {
        if (viewType == YOUR_TYPE) {
            return R.layout.your_item;
        } else if (...) {
            return R.layout.other_item;
        } else if (...) {
            //...
        }
    }

    //...

}

Gradle

To get this Library into your project:

Step 1. Add the JitPack repository to your build file

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

    dependencies {
        compile 'com.github.vivchar:ViewFinder:2.0.0'
    }

Try to use it too

https://github.com/vivchar/RendererRecyclerViewAdapter

License

Copyright 2017 Vitaly Vivchar

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.