RecyclerView的快速和易于使用的Adapter:FastAdapter

jopen 8年前發布 | 21K 次閱讀 Android開發 移動開發 FastAdapter

FastAdapter

RecyclerView是Android中最常用的組件之一,使用RecyclerView的時候,必須實現Adapter中提供的相應接口。所以每次調用RecyclerView時,就需要重復實現Adapter中的這些方法。FastAdapter就是用來解決重用問題的,使用FastAdapter后,我們就只需關心item與view的部分就可以了。

The FastAdapter is here to simplify this process. You don't have to worry about the adapter anymore. Just write the logic for how your view/item should look like, and you are done. This library has a fast and highly optimized core which provides core functionality, most apps require. It also prevents common mistakes by taking away those steps from the devs. Beside being blazing fast, minimizing the code you need to write, it is also really easy to extend. Just provide another Adapter implementation, hook into the adapter chain, custom select / deselection behaviors. Everything is possible.

A quick overview:

  • Click / Long-Click listeners
  • Selection / Multi-Selection
  • Expandable items
  • Write less code, get better results
  • Simple Drag & Drop
  • Headers
  • Footers
  • Highly optimized code
  • Includes suggestions from the Android Team
  • Easily extensible
  • Chain other Adapters
  • Comes with useful Helpers
    • ActionModeHelper
    • More to come...

Preview

Demo

You can try it out here Google Play (or download the latest release from GitHub)

Screenshots

Image

Include in your project

Using Maven

compile('com.mikepenz:fastadapter:0.9.1@aar') {
    transitive = true
}

How to use

1. Implement your item (the easy way)

Just create a class which extends the AbstractItem as shown below. Implement the methods, and your item is ready.

public class SampleItem extends AbstractItem<SampleItem, SampleItem.ViewHolder> {
    public String name;
    public String description;

    //The unique ID for this type of item
    @Override
    public int getType() {
        return R.id.fastadapter_sampleitem_id;
    }

    //The layout to be used for this type of item
    @Override
    public int getLayoutRes() {
        return R.layout.sample_item;
    }

    //The logic to bind your data to the view
    @Override
    public void bindView(ViewHolder viewHolder) {
        //call super so the selection is already handled for you
        super.bindView(viewHolder);

        //bind our data
        //set the text for the name
        viewHolder.name.setText(name);
        //set the text for the description or hide
        viewHolder.description.setText(description);
    }

    //The viewHolder used for this item. This viewHolder is always reused by the RecyclerView so scrolling is blazing fast
    protected static class ViewHolder extends RecyclerView.ViewHolder {
        protected TextView name;
        protected TextView description;

        public ViewHolder(View view) {
            super(view);
            this.name = (TextView) view.findViewById(com.mikepenz.materialdrawer.R.id.material_drawer_name);
            this.description = (TextView) view.findViewById(com.mikepenz.materialdrawer.R.id.material_drawer_description);
        }
    }
}

2. Set the Adapter to the RecyclerView

//create our FastAdapter which will manage everything
FastItemAdapter fastAdapter = new FastItemAdapter();

//set our adapters to the RecyclerView
//we wrap our FastAdapter inside the ItemAdapter -> This allows us to chain adapters for more complex useCases
recyclerView.setAdapter(fastAdapter);

//set the items to your ItemAdapter
fastAdapter.add(ITEMS);

Libs used in sample app:

Mike Penz:

Other Libs:

項目地址: https://github.com/mikepenz/FastAdapter

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!