Search in sources :

Example 1 with FlatTypeClassAdapter

use of me.drakeet.multitype.FlatTypeClassAdapter in project MultiType by drakeet.

the class OneDataToManyActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    recyclerView = (RecyclerView) findViewById(R.id.list);
    adapter = new MultiTypeAdapter();
    adapter.setFlatTypeAdapter(new FlatTypeClassAdapter() {

        @NonNull
        @Override
        public Class onFlattenClass(@NonNull Object item) {
            return ((Data) item).typeClass;
        }
    });
    adapter.register(Data.Type1.class, new DataType1ViewBinder());
    adapter.register(Data.Type2.class, new DataType2ViewBinder());
    List<Data> dataList = getDataFromService();
    // 如果 Data 们不会自动把 int type -> Class typeClass 的话,
    // 需要自行处理,示例如下:
    // for (Data data : dataList) {
    //     data.typeClass = Data.getTypeClass(data.type);
    // }
    adapter.setItems(dataList);
    adapter.notifyDataSetChanged();
    assertAllRegistered(adapter, dataList);
    recyclerView.setAdapter(adapter);
    assertHasTheSameAdapter(recyclerView, adapter);
}
Also used : NonNull(android.support.annotation.NonNull) MultiTypeAdapter(me.drakeet.multitype.MultiTypeAdapter) FlatTypeClassAdapter(me.drakeet.multitype.FlatTypeClassAdapter)

Aggregations

NonNull (android.support.annotation.NonNull)1 FlatTypeClassAdapter (me.drakeet.multitype.FlatTypeClassAdapter)1 MultiTypeAdapter (me.drakeet.multitype.MultiTypeAdapter)1