SHARE
TWEET

Untitled

a guest Jun 18th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Du brauchst ein Interface welches deine beiden Models implementieren um eine gemeinsame List zu machen
  2. public interface Buyable {
  3.  
  4. }
  5.  
  6. public class DrinksSelection implements Buyable {
  7.     ...
  8. }
  9.  
  10. public class ShishaSelection implements Buyable {
  11.     ...
  12. }
  13.  
  14. // In deiner Warenkorb Activity holst du dir alle shishas und drinks und fügst die in eine List vom Typ Buyable
  15. // und gibst die dann deinem Adapter
  16. // List<Buyable> list = new ArrayList()
  17. // list.addAll(shishas)
  18. // list.addAll(drinks)
  19.  
  20. public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  21.    
  22.     private final int DRINK_TYPE = 1;
  23.     private final int SHISHA_TYPE = 2;
  24.     private List<Buyable> list;
  25.  
  26.     public MyAdapter(List<Buyable> list) {
  27.         this.list = list;
  28.     }
  29.  
  30.     class ShishaViewHolder extends RecyclerView.ViewHolder {
  31.         ...
  32.         public ShishaViewHolder(View itemView){
  33.         ...
  34.         }
  35.     }
  36.  
  37.     class DrinkViewHolder extends RecyclerView.ViewHolder {
  38.         ...
  39.         public DrinkViewHolder(View itemView){
  40.         ...
  41.     }
  42.  
  43.     @Override
  44.     public int getItemViewType(int position) {
  45.         if (list.get(position) instanceof DrinkSelection) {
  46.             return DRINK_TYPE;
  47.         } else {
  48.             return SHISHA_TYPE;
  49.         }
  50.     }
  51.  
  52.     @Override
  53.     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  54.          switch (viewType) {
  55.              case DRINK_TYPE: return new DrinkViewHolder(...);
  56.              case SHISHA_TYPE: return new ShishaViewHolder(...);
  57.              ...
  58.          }
  59.     }
  60.  
  61.     @Override
  62.     public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
  63.         switch (holder.getItemViewType()) {
  64.             case DRINK_TYPE:
  65.                 DrinkViewHolder drinkViewHolder = (DrinkViewHolder) holder;
  66.                 ...
  67.                 break;
  68.  
  69.             case 2:
  70.                 ShishaViewHolder shishaViewHolder = (ShishaViewHolder) holder;
  71.                 ...
  72.                 break;
  73.         }
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top