在实现IItem的FastAdapter上实现OnClickListener

时间:2017-06-05 08:13:58

标签: android fastadapter

我正在学习FastAdapterRealmHere是我的模型,这就是我在OnClick中实施Fragment的方式:

fastAdapter.withOnClickListener(new FastAdapter.OnClickListener<ProductsModel>() {
    @Override
    public boolean onClick(View v, IAdapter<ProductsModel> adapter, ProductsModel item, int position) {
        Toast.makeText(getActivity(), "got it", Toast.LENGTH_SHORT).show();
        return false;
    }
});

但我没有让Toast出现。谁能告诉我我错过了什么?

更新:这是我的模特

public class ProductsModel extends RealmObject implements IItem<ProductsModel, ProductsModel.ViewHolder>{
    @PrimaryKey
    private String code;

    private String name, generic, packSize;
    private int quantity, status;


    //variables needed for adapter
    protected boolean isSelected = false; // defines if the item is selected

    @Ignore
    protected Object tag;// defines if this item is isSelectable

    @Ignore
    protected boolean isSelectable = true;


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

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

    public String getPackSize() {
        return packSize;
    }

    public void setPackSize(String packSize) {
        this.packSize = packSize;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getGeneric() {
        return generic;
    }

    public void setGeneric(String generic) {
        this.generic = generic;
    }

    @Override
    public Object getTag() {
        return tag;
    }

    @Override
    public ProductsModel withTag(Object tag) {
        this.tag = tag;
        return this;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    @Override
    public ProductsModel withEnabled(boolean enabled) {
        return null;
    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    @Override
    public ProductsModel withSetSelected(boolean selected) {
        return null;
    }

    @Override
    public boolean isSelectable() {
        return isSelectable;
    }

    @Override
    public ProductsModel withSelectable(boolean selectable) {
        this.isSelectable = selectable;
        return this;
    }

    @Override
    public int getType() {
        return R.id.pwdsList;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.item_product;
    }

    @Override
    public View generateView(Context ctx) {
        ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), null, false));
        bindView(viewHolder, Collections.EMPTY_LIST);
        return viewHolder.itemView;
    }

    @Override
    public View generateView(Context ctx, ViewGroup parent) {
        ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), parent, false));
        bindView(viewHolder, Collections.EMPTY_LIST);
        return null;
    }


    private ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder getViewHolder(ViewGroup parent) {
        return getViewHolder(LayoutInflater.from(parent.getContext()).inflate(getLayoutRes(), parent, false));
    }

    @Override
    public void bindView(ViewHolder holder, List<Object> payloads) {
        holder.name.setText(name + " " + packSize + " (" + quantity + ")");
        holder.generic.setText(generic);
        holder.itemView.setSelected(isSelected());
    }

    @Override
    public void unbindView(ViewHolder holder) {
        holder.name.setText(null);
        holder.generic.setText(null);
    }

    @Override
    public boolean equals(int id) {
        return false;
    }

    @Override
    public ProductsModel withIdentifier(long identifier) {
        return null;
    }

    @Override
    public long getIdentifier() {
        return 0;
    }


    static class ViewHolder extends RecyclerView.ViewHolder{

        ATextView name, generic;

        public ViewHolder(View itemView) {
            super(itemView);

            name = (ATextView) itemView.findViewById(R.id.name);
            generic = (ATextView) itemView.findViewById(R.id.generic);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的商品不是enabled,这会导致点击事件无法转发。只需更改代码即可返回isEnabled=true

@Override
public boolean isEnabled() {
    return true;
}