4

我正在尝试实现 fastadapter。我只是复制了示例代码并试图让它工作。当我尝试它时,它不会在我的回收站视图中显示任何内容。通过调试,我发现 bindView() 没有被调用。

我的 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);
    }
}
}

我的活动

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);
}
}

我的 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'

带分机:

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

提前感谢您的帮助。

4

2 回答 2

9

您需要将 LayoutManager 添加到 RecyclerView。

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
于 2018-03-06T10:51:59.550 回答
0

根据我的经验,我以这种方式解决了这个问题:

@Override
public void bindView(SimpleItem item, List<Object> payloads) {
}

@Override
public void unbindView(SimpleItem item) {
}

getType()方法不对,需要把recyclerView的ID放上负责显示数据。

在主类中(就在 ViewHolder 类的上方)放置了 2 个被覆盖的方法:

@Override
public void bindView(ViewHolder holder, List<Object> payloads) {
    super.bindView(holder, payloads);
    Log.d("onBind","bind view called"); //does not get called
    holder.name.setText(name);
    holder.description.setText(description);
}

@Override
public void unbindView(ViewHolder holder) {
    holder.name.setText("");
    holder.description.setText("");
}

希望这有帮助;)

于 2018-05-21T16:00:13.383 回答