I'm a rookie developer working on a Quiz app but with some additional features such as rewards (collectibles). The idea is that after the user finishes a quiz, they get a card (a picture of the hero) as a reward and then they can check those in a sort of a gallery. But my problem is with the gallery itself. The Gallery widget is no longer supported so I wanted to use standard Horizontal Scroll view. It should look like this:
At the bottom, you can see the scroll view, where the cards should be displayed and you can swipe between them left-right. If you tap the card, it will be displayed in the IV. Also there is a TV for some info, but that's not relevant now. The problem is I can't use Base Adapter to fill the images into ScrollView (setAdapter). I know it can be done using ListView or GridView but I wanted the gallery be like this (Horizontal) I even tried it using ViewPager, but that doesn't work either (also I'm not sure if ViewPager is good a way to do this). When I've tried ViewPager insted ScrollView (viewPager.setAdapter(cardGalleryAdapter);), I got an error that an Adapter is required and not my custom Base Adapter. Am I missing something? Or is there any other way how to do this? Also I should mention that I'm doing this in fragment, not activity itself, could that be the issue? My code:
Adapter Class:
public class CardGalleryAdapter extends BaseAdapter {
private Context context;
private int [] cards;
public CardGalleryAdapter(Context c, int [] images){
context = c;
this.cards = images;
}
@Override
public int getCount() {
return cards.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(cards[position]);
imageView.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
return imageView;
}
}
Fragment Class:
public class DeckFragment extends Fragment implements View.OnClickListener {
HorizontalScrollView gallery;
ImageView cardView;
TextView cardText;
CardGalleryAdapter cardGalleryAdapter;
DeckCards deckCards;
ViewPager2 viewPager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//Getting objects
View view = inflater.inflate(R.layout.fragment_deck, container, false);
//Gallery as HorizontalScrollView it not working
gallery.findViewById(R.id.gallery);
gallery.setOnClickListener(this);
cardView.findViewById(R.id.cardView);
cardText.findViewById(R.id.cardText);
viewPager.findViewById(R.id.viewPager);
//Separate class, where the array of the cards is located
deckCards = new DeckCards();
cardGalleryAdapter = new CardGalleryAdapter(getContext(), deckCards.getCards());
//ViewPager instead HorizontalScrollView, but I get the mentioned error
viewPager.setAdapter(cardGalleryAdapter);
return view;
}
@Override
public void onClick(View v) {
}
}
Thanks for any response.
Suppose you want to scroll items in a horizontal manner in the below view. You can use Recyclerview and create an adapter for the same. Make sure you set the orientation horizontal to Recyclerview.