2017-08-30 28 views
0

我試圖在兩種活動中重用Firebase適配器,並根據兩種活動使用不同的onClick偵聽器行爲。針對同一Firebase適配器的不同OnClickListener

下面是代碼:

 public class ProductListAdapter { 

private Query mQuery; 
private Context mContext; 
private FirebaseRecyclerAdapter<Product,ProductViewHolder> mAdapter; 
public ProductListAdapter(Query query,Context context) { 

    mQuery = query; 
    mContext = context; 

    Update(); 
} 



public void Update(){ 
    mAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(
      Product.class, 
      R.layout.list_item_product, 
      ProductViewHolder.class, 
      mQuery 
    ){ 
     @Override 
     protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) { 
      viewHolder.mProductTitle.setText(model.getProductTitle()); 
      viewHolder.mProductDescription.setText(model.getProductDescription()); 
      Product product = getItem(position); 
      viewHolder.bindView(product); 


      Picasso.with(mContext) 
        .load(model.getPhotoUrl()) 
        .into(viewHolder.mThumbnail); 


     } 
    }; 


} 

public RecyclerView.Adapter getAdapter(){ 
    return mAdapter; 
} 

public static class ProductViewHolder extends RecyclerView.ViewHolder{ 

    Product mProduct; 
    TextView mProductTitle; 
    TextView mProductDescription; 

    ImageView mThumbnail; 

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

     mProductTitle = (TextView) itemView.findViewById(R.id.product_title); 
     mProductDescription = (TextView) itemView.findViewById(R.id.product_description); 
     mThumbnail = (ImageView) itemView.findViewById(R.id.list_image); 


    } 


    public void bindView(Product product){ 
     mProduct = product; 
    } 
} 

,這是調用活動,其調用firebaseadapter類的代碼,這個類是返回使用getAdapter方法和這是考慮到回收視圖適配器:

ProductListAdapter productListAdapter = 
    new    ProductListAdapter(query,getActivity()); 
    RecyclerView.Adapter adapter = productListAdapter.getAdapter(); 

    mRecyclerView.setAdapter(adapter); 
+0

問題是什麼? – Shailesh

+0

我想在其他活動中重用firebase適配器,但使用不同的onClickListener方法@Shailesh – Pheonix

+0

其中是onClick()在您的代碼中? – Yazan

回答

2

您可以在任何元素設置onClickListinner()裏面你viewHolder

嘗試類似的東西:

 if (condition) { 
      viewHolder.mThumbnail.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        ... doStuff 
       } 
      });  
     } else { 
      viewHolder.mThumbnail.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        ... doOtherStuff 
       } 
      }); 
     } 

關於再利用,最好是創造延伸FirebaseRecyclerAdapter一個類,並且它們在其內部限定的方法populateViewHolder,具有這種條件對於每個OnClickListener

As a example:

public class RecipeAdapter extends FirebaseRecyclerAdapter<Product, ProductViewHolder>{ 
    private static final String TAG = RecipeAdapter.class.getSimpleName(); 
    private Context context; 
    public RecipeAdapter(Class<Product> modelClass, int modelLayout, Class<ProductViewHolder> viewHolderClass, DatabaseReference ref, Context context) { 
     super(modelClass, modelLayout, viewHolderClass, ref); 
     this.context = context; 
    } 
    @Override 
    protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) { 
     ... 
    } 
} 

,然後設置適配器:

mAdapter = new RecipeAdapter<Product, ProductViewHolder>(
      Product.class, 
      R.layout.list_item_product, 
      ProductViewHolder.class, 
      mQuery, 
      this 
    ) 
+0

怎麼樣在10中使用適配器活動,那麼將有10個,如果其他,你認爲這是做這項工作的有效方式@GuilhermeFGL – Pheonix

+0

我更新了我的答案,這是一個「重用」解決方案。 – GuilhermeFGL

相關問題