Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MikePenz FastAdapter usage

I am trying to implement the fastadapter. I just copied the sample code and tried to get it working. When I try it out it does not show anything in my recycler view. With debugging I found out that bindView() ist not getting called.

My SampleItem.jave

public class SimpleItem extends AbstractItem<SimpleItem, SimpleItem.ViewHolder> {
public StringHolder name;
public StringHolder description;

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

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

@Override
public ViewHolder getViewHolder(@NonNull View v) {
    return new ViewHolder(v);
}

public void setName(String name){
    this.name = new StringHolder(name);
}

public void setDescription(String description){
    this.description = new StringHolder(description);
}

@Override
public String toString() {
    return String.valueOf(name.getText()) + description.getText();
}

/**
 * our ViewHolder
 */
protected static class ViewHolder extends FastAdapter.ViewHolder<SimpleItem> {
    @BindView(R.id.material_drawer_name)
    TextView name;
    @BindView(R.id.material_drawer_description)
    TextView description;

    public ViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
    }

    @Override
    public void bindView(SimpleItem item, List<Object> payloads) {
        Log.d("onBind","bind view called"); //does not get called
        StringHolder.applyTo(item.name, name);
        StringHolder.applyToOrHide(item.description, description);
    }

    @Override
    public void unbindView(SimpleItem item) {
        name.setText(null);
        description.setText(null);
    }
}
}

My Activity

public class SampleActivity extends AppCompatActivity {
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_view);
    ButterKnife.bind(this);
    //create the ItemAdapter holding your Items
    ItemAdapter itemAdapter = new ItemAdapter();
    //create the managing FastAdapter, by passing in the itemAdapter
    FastAdapter fastAdapter = FastAdapter.with(itemAdapter);

    //set our adapters to the RecyclerView
    recyclerView.setAdapter(fastAdapter);

    ArrayList<SimpleItem> items = new ArrayList<>();
    for (int i = 0 ; i < 20;i++){
        SimpleItem simpleItem = new SimpleItem();
        simpleItem.setName("this is a name");
        simpleItem.setDescription("this is a description - " + i);
        items.add(simpleItem);
    }
    Log.d("items:" , items.toString());
    //set the items to your ItemAdapter
    itemAdapter.add(items);
}
}

My build.gradle

//for fastadapter
implementation "com.mikepenz:fastadapter:$rootProject.fastAdapterVersion@aar"
implementation  "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
implementation  "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
implementation "com.mikepenz:fastadapter-commons:$rootProject.fastAdapterVersion@aar"
implementation "com.mikepenz:fastadapter-extensions-expandable:$rootProject.fastAdapterVersion@aar"
implementation "com.mikepenz:fastadapter-extensions:$rootProject.fastAdapterVersion@aar"
implementation  "com.android.support:design:$rootProject.supportLibraryVersion"
//The tiny Materialize library used for its useful helper classes - also needed for fastadapter
implementation "com.mikepenz:materialize:$rootProject.materializeVersion@aar"

//butterknife
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

With ext:

fastAdapterVersion = "3.2.4"
materializeVersion = "1.1.2"
supportLibraryVersion = '26.1.0'

Thanks in adavance for any help.

like image 975
Stein. Avatar asked Jan 02 '23 17:01

Stein.


1 Answers

You need to add LayoutManager to RecyclerView.

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
like image 162
Sz-Nika Janos Avatar answered Jan 05 '23 15:01

Sz-Nika Janos