在Horizo​​ntalsCrollview中使用基本适配器进行画廊

发布于 2025-02-13 22:21:55 字数 2751 浏览 0 评论 0原文

我是在测验应用程序上工作的新秀开发人员,但具有一些其他功能,例如奖励(收藏品)。这个想法是,在用户完成测验后,他们会得到一张卡片(英雄的图片)作为奖励,然后可以在画廊中检查这些卡片。但是我的问题是画廊本身。 画廊小部件不再支持,因此我想使用标准的水平滚动视图。 它应该看起来像这样:

“布局”

在底部,您可以看到滚动视图,应在其中显示卡片,可以在其中滑动他们左右。如果您点击卡,则将显示在IV中。还有电视可以提供一些信息,但这现在不相关。 问题是我不能使用基本适配器将图像填充到scrollview( setadapter )中。 我知道可以使用ListView或GridView完成,但是我希望画廊像这样(水平) 我什至尝试使用 ViewPager 尝试过它,但这也不起作用(我也不确定ViewPager是否是一种很好的方法)。当我尝试使用ViewPager启用ScrollView( viewPager.setAdapter(cardGalleryAdapter); )时,我遇到了一个错误,认为需要适配器,而不是我的自定义基础适配器。 我想念什么吗?还是还有其他方法如何做到这一点? 我还应该提到,我在片段而不是活动本身中这样做,这可能是问题吗? 我的代码:

适配器类:

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类:

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) {

}

}

感谢您的任何响应。

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:

Layout

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2025-02-20 22:21:55

假设您想在下面的视图中以水平方式滚动项目。您可以使用recyclerview并为其创建一个适配器。确保将定向水平设置为回收库。

请参阅以下信息: https://developer.android.com/guide/topics/ui/layout/recyclerview#:~: text=recyClerview%20IS%20 the%20ViewGroup%20That,by%20that,by%20a%20A%20A%20View; >。

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.

Refer to this: https://developer.android.com/guide/topics/ui/layout/recyclerview#:~:text=RecyclerView%20is%20the%20ViewGroup%20that,by%20a%20view%20holder%20object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文