2017-06-05 208 views
1

我正在學習使用FastAdapterRealmHere是我的模型,這就是我在Fragment實施OnClick在FastAdapter上實現IIClickListener實現IItem

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

是否將fastadapter的withSelectable屬性設置爲true? –

+0

是'fastAdapter.withSelectable(true);' –

+0

您錯誤地用Realm實現了FastAdapter。 FastAdapter中的模型是回收站適配器中的一個項目,它不像您想象的那樣。錯誤在這裏'ProductsModel extends RealmObject'。你需要得到例子,並按照這裏https://github.com/mikepenz/FastAdapter/tree/develop/app。 –

回答

0

你的產品不enabled導致不被轉發點擊事件。只需更改您的代碼即可返回isEnabled=true

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

工作。我的'PrimaryKey'是一個'String'而不是'Long'。你的'getIdentifier()'不支持'String',所以我通過執行'new BigInteger(sb.toString())。longValue()'來轉換我的代碼,這不是一個好主意。無論如何,我可以使用我的主鍵作爲標識符嗎? –

+1

好吧,這是一個不同的問題。但是像這樣的東西會工作:private static hashString64Bit(CharSequence str){ \t long result = 0xcbf29ce484222325 L; \t final int len = str.length();對於(int i = 0; i mikepenz