如何在android中制作自定义图库视图

发布于 2024-10-07 19:54:03 字数 1354 浏览 1 评论 0原文

我正在尝试创建自定义图库视图。或者也许我不明白我应该在这里做什么。我需要覆盖 Gallery 类的 onFling() 方法,但我不知道如何执行此操作,因为我的主类必须从 Activity< 扩展/强>。

我尝试创建一个名为 CustomGallery 的类来扩展 Gallery,但是如果我尝试运行该应用程序,我会强制关闭。

如何重写 Gallery View 的 onFling() 方法?

谢谢!

编辑

我正在尝试下面克里斯蒂安的解决方案,但是,这个类到处都有错误。显然我做错了。建议?

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Gallery;

public class CustomGallery extends Gallery
{
    public CustomGallery(Context context)
    {
        super(context);
    }

    @Override
    public Gallery(Context context)
    {
        this(context, null);
    }

    @Override
    public Gallery(Context context, AttributeSet attrs)
    {
        this(context, attrs, R.attr.galleryStyle);
    }

    @Override
    public Gallery(Context context, AttributeSet attrs, int defStyle)
    {
        //
    }
}

编辑2

好的,这已经成功了,谢谢克里斯蒂安!

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Gallery;

public class CustomGallery extends Gallery
{
    public CustomGallery(Context context, AttributeSet attrs)
    {
        super(context, attrs);

    }

}

I am trying to create a custom Gallery view. Or maybe I'm not understanding what I'm supposed to do here. I need to overwrite the onFling() method of the Gallery class, but I do not see how I can do this as my main class must extend from Activity.

I have tried making a class called CustomGallery that extends Gallery, but If I try and run the app I get a force close.

How does one overwrite the onFling() method for a Gallery View?

Thanks!

EDIT

I am attempting Christian's solution below however, this class has errors all over it. Clearly I am doing this wrong. Suggestions?

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Gallery;

public class CustomGallery extends Gallery
{
    public CustomGallery(Context context)
    {
        super(context);
    }

    @Override
    public Gallery(Context context)
    {
        this(context, null);
    }

    @Override
    public Gallery(Context context, AttributeSet attrs)
    {
        this(context, attrs, R.attr.galleryStyle);
    }

    @Override
    public Gallery(Context context, AttributeSet attrs, int defStyle)
    {
        //
    }
}

EDIT 2

ok this got it working, thx Christian!

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Gallery;

public class CustomGallery extends Gallery
{
    public CustomGallery(Context context, AttributeSet attrs)
    {
        super(context, attrs);

    }

}

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

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

发布评论

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

评论(1

叫嚣ゝ 2024-10-14 19:54:03

<块引用>

如何覆盖 Gallery View 的 onFling() 方法?

  1. 创建一个扩展 Gallery 的类(例如 CustomGallery,XD)
  2. 重写方法
  3. 在布局中使用该类。

就像使用 Gallery 一样使用它:

<LinearLayout>
...
<com.your.package.CustomGallery
    android:layout_width="fill_parent"
    the rest of the things here/>
...
</LinearLayout>

确保重写构造函数方法:

public class CustomGallery extends Gallery{

    public CustomGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        // this could be empty, but must be here.
        // since it's a view to use from XML,
        // you must override this constructor
        // (not only the one that receives a context)
    }
}

How does one overwrite the onFling() method for a Gallery View?

  1. Create a class that extends Gallery (e.g. CustomGallery, XD)
  2. Override the method
  3. Use that class in your layout.

Just use it like if you were using a Gallery:

<LinearLayout>
...
<com.your.package.CustomGallery
    android:layout_width="fill_parent"
    the rest of the things here/>
...
</LinearLayout>

Make sure to override the constructor methods:

public class CustomGallery extends Gallery{

    public CustomGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        // this could be empty, but must be here.
        // since it's a view to use from XML,
        // you must override this constructor
        // (not only the one that receives a context)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文