Implement OnClickListener on FastAdapter implementing IItem

99封情书 提交于 2020-01-05 08:17:09

问题


I am learning to use FastAdapter with Realm. Here is my model and this is how I implement OnClick in a 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;
    }
});

But I don't get the Toast appearing. Can anybody please tell me what am I missing?

Update: Here is my model

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:


Your item is not enabled which results in the click event not being forwarded. Just change your code to return isEnabled=true

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


来源:https://stackoverflow.com/questions/44364540/implement-onclicklistener-on-fastadapter-implementing-iitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!