-1

I have a simple RecyclerView where I show all the products from firebase but now I'd like to show them on a Carousel like this Library, the thing is that I have all on my ChooseProduct.java class and I'd like to create a custom Adapter to put the products as a Carousel.

First I get the products as follows :

FirebaseRecyclerOptions< CardPOJO > options =
            new FirebaseRecyclerOptions.Builder< CardPOJO >()
                    .setQuery(products_ref, CardPOJO.class)
                    .build();

And I store it on options, so then I create the Adapter

FirebaseRecyclerAdapter adapter = new   FirebaseRecyclerAdapter< CardPOJO, CardHolder>(options) {
        @Override
        public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //inflate the single recycler view layout(item)
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.card_product, parent, false);
            int width = parent.getMeasuredWidth() / 2;
            width -= mContext.getResources().getDimensionPixelSize(R.dimen._8sdp);
            final CardHolder cardViewHolder = new CardHolder(view,width);
            return cardViewHolder;
        }

        @Override
        public void onDataChanged() {
            super.onDataChanged();
            tv.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
        }

        @Override
        protected void onBindViewHolder(CardHolder holder, int position, final CardPOJO model) {
            holder.state.setText(model.getState());
            holder.cardName.setText(model.getName());
            switch (model.getState()){
                case "free":
                    //Img free
                    break;
                case "not_free":
                    //Img not free
                    break;
                default:
                    break;
            }
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(model.getState().equals("free")){
                       //stuff
                    }
                    else{
                       //stuff
                        }
                        root_ref.child("PurchasedProducts").child(currentuser).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                //stuff
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                    }

                }
            });


        }
    };
    adapter.startListening();
    products_recycler.setAdapter(adapter);

And my .xml has a RecyclerView like this :

<android.support.v7.widget.RecyclerView

        android:id="@+id/recycler_view_chooseCard"

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

So my question is, how do I change this to put a Carousel and use like the example a in.goodiebag.carouselpicker.CarouselPicker instead of a RecyclerView?

Also could I move all of this code to another one to make it clean?

Note

When I try to implement the CourouselPicker it says this :

setAdapter (android.support.v4.view.PagerAdapter) in CarouselPicker cannot be applied to (com.firebase.ui.database.FirebaseRecyclerAdapter)

What I have tried so far?

I changed the RecyclerView to

<in.goodiebag.carouselpicker.CarouselPicker
            android:id="@+id/carousel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:background="#CCC"
            app:items_visible="three" />

I initialized it : carouselPicker = (CarouselPicker) findViewById(R.id.carousel);

And then I tried to put the .setAdapter() with the FireBaseAdapter one, but they are not compatible...

carouselPicker.setAdapter(adapter);

IN CASE IT'S HARD TO DO THAT READ THIS

If that's to broad and it's too dificult to make it with ViewPager and those stuff, could be possible to create a RecyclerView that acts like carousel? You have the items horizontally and then the item on the middle you see it bigger than rest.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Why downvote? If you downvote someone you can say the reason so the OP won't do that again ;) – Skizo-ozᴉʞS ツ Dec 28 '17 at 17:18
  • if this `carouselPicker.setAdapter(adapter);` does not work, then maybe better to just use a custom adapter and not firebaserecycleradapter – Peter Haddad Dec 28 '17 at 17:43
  • That's what I'm asking for, how to create a different adapter instead of using firebaserecycleradapter – Skizo-ozᴉʞS ツ Dec 28 '17 at 17:58
  • Saw your comment [here](https://stackoverflow.com/a/47347222/4625829). Looks like the answer of @SUPERCILEX covered everything (+1). – AL. Dec 29 '17 at 06:52
  • @AL. Thanks for comming here, but do you know another way to get those items? I mean, it's cool that I'm using the `FirebaseAdapter` but, like now, I'd like to put those items on an HorizontalScrollView or Carousel, and I can not put them to a RecyclerView or something like that, do you know another way to do that? Since now I had a normal list where it was shown correctly the data, but now I'd like to put it on a better UI, but I've worked really little so if you know another way to do it feel free to put an answer :D – Skizo-ozᴉʞS ツ Dec 29 '17 at 11:51
  • You could still just use the `FirebaseAdapter` on a Horizontal RecyclerView, just have the middle item use a different (bigger as you mentioned) layout. Since the `FirebaseAdapter` extends a `RecyclerView.Adapter`, you should be able to override [`getItemViewType`](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemViewType(int)) – AL. Dec 30 '17 at 12:51

1 Answers1

2

The CarouselPicker is actually a ViewPager which is kinda sad. In any case, the RecyclerView adapter and PagerAdapter are two completely different things are don't mix. You'll essentially have to go custom—there are two options.

The first is simpler and better UX in my opinion: just don't make it realtime. If you're going to display that view in a dialog that the user is only going to be briefly interacting with, it'll be confusing when the carousel updates without animations and all the goodness of the RecyclerView. It would look something like this:

query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        List<CarouselPicker.PickerItem> items = new ArrayList<>();
        for (DataSnapshot child : snapshot.getChildren()) {
            CardPOJO card = child.getValue(CardPOJO.class);
            // Use card
            items.add(new CarouselPicker...);
        }
        mCarouselPicker.setAdapter(new CarouselPicker.CarouselViewAdapter(Activity.this, items, 0));
    }

    // ...
}

If you really want it to be realtime, you can use a FirebaseArray like so:

final ObservableSnapshotArray<CardPOJO> cards = new FirebaseArray(query, new ClassSnapshotParser<>(CardPOJO.class));
cards.addChangeEventListener(new ChangeEventListener() {
    @Override
    public void onDataChanged() {
        List<CarouselPicker.PickerItem> items = new ArrayList<>();
        for (CardPOJO card : cards) {
            // Use card
            items.add(new CarouselPicker...);
        }
        mCarouselPicker.setAdapter(new CarouselPicker.CarouselViewAdapter(Activity.this, items, 0));
    }
})

Hope this helps!

SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61
  • I'm not going to show that info to dialog, I'll put on an Activity the .gif on the library it's just to show the carousel, I'll show like a list on Activity – Skizo-ozᴉʞS ツ Dec 29 '17 at 11:31
  • In case I'd like to do it in real time, where do I put that bunch of code? if I have allready my adapter? – Skizo-ozᴉʞS ツ Dec 29 '17 at 11:33
  • I tried the solution that is in real time, and yes, it shows as a carousel, but the thing is I have like a duplicated stuff, right? I have the FirebaseAdapter where I programmed all of the onBindViewHolder onCreateViewHolder, etc and using this I'm not able to using it, right? – Skizo-ozᴉʞS ツ Dec 29 '17 at 12:09
  • Yeah, my code is going to replace the adapter since you're using the carousel. Make sure to check out their documentation: https://github.com/GoodieBag/CarouselPicker/blob/master/README.md#java-. – SUPERCILEX Dec 29 '17 at 18:37
  • And if I don't use this carousel I can't do like a recyclerview custom adapter like I have now in firebase adapter? I mean, from now I have a simple list view, but I'd like to make it cool so I want to make like a horizontal scroll view or something you know? Like I have now where I discriminated by its state (free, not_free) so I can put one image or another, but in horizontal like a carousel? You got me? – Skizo-ozᴉʞS ツ Dec 29 '17 at 18:56
  • Oh yeah, you can make a horizontal list with RV: https://stackoverflow.com/a/28460399/4548500. – SUPERCILEX Dec 29 '17 at 19:19
  • Yes I know I can do it with rv, but the problem is how to change the firebase adapter to my new rc adapter? – Skizo-ozᴉʞS ツ Dec 29 '17 at 19:21
  • The adapter won't change, it doesn't really care about the recycler view. Just use the horizontal layout manager and make sure your view holder is `wrap_content` for the width. – SUPERCILEX Dec 29 '17 at 19:26
  • And I can keep my old code? With the firebase adapter? – Skizo-ozᴉʞS ツ Dec 30 '17 at 00:51
  • Yep, the adapter doesn't care. It's just RecyclerView changes. – SUPERCILEX Dec 30 '17 at 01:26
  • But I have to create a custom adapter to do the horizontalscrollview... right? And I will have to put the items from firebase to it, can you make a small sample of it to understand you? – Skizo-ozᴉʞS ツ Dec 30 '17 at 16:11