单击事件不会传播到子视图

发布于 2024-12-05 03:49:41 字数 1417 浏览 2 评论 0原文

我有一个活动 A1,它有多个视图

,另一个类 ViewGenerator 有一个通过方法 getView() 返回视图的方法,

我通过为 A1 创建一个对象并调用 getView 来调用此方法。 我的班级运转良好,我能够看到风景。但是该视图的任何子视图都不可单击,因此不会在该子视图上触发单击事件。

这里是A1的getView方法,

public View getView(Main ctx) {

    return getArticleView();

}

这里是getArticleView

private View getArticleView(Main ctx) {
    LayoutInflater lft = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout lin = (RelativeLayout) lft.inflate(
            R.layout.article_view, null);
    RelativeLayout articleView = (RelativeLayout) lin
            .findViewById(R.id.article_view);

    summaryStuff(ctx);

    return articleView;
}

,这里是summaryStuff

private void sharingStuff(final Main ctx) {
    LayoutInflater lft = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout lin = (RelativeLayout) lft.inflate(R.layout.summary_bar,
            null);
    bookmarkRibbon = (ImageView) lin.findViewById(R.id.bookmark_selector);
    bookmarkRibbon.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            bookmarkRibbon.setImageResource(R.drawable.bookmark_yellow);
        }
    });
}

,你可以看到bookmarkRibbon有一个监听器。但是它不可点击,或者至少没有触发事件

get View 是从 Activity 的 onCreate 方法调用的

I have an activity A1 which has several views

and another class ViewGenerator which has a method that returns a view via method getView()

I call this method by creating an object for A1 and calling getView.
my class is working fine and I am able to get the view. however any child of that view is not clickable, click event is not fired on that child.

here is A1's getView method

public View getView(Main ctx) {

    return getArticleView();

}

here is getArticleView

private View getArticleView(Main ctx) {
    LayoutInflater lft = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout lin = (RelativeLayout) lft.inflate(
            R.layout.article_view, null);
    RelativeLayout articleView = (RelativeLayout) lin
            .findViewById(R.id.article_view);

    summaryStuff(ctx);

    return articleView;
}

here is summaryStuff

private void sharingStuff(final Main ctx) {
    LayoutInflater lft = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout lin = (RelativeLayout) lft.inflate(R.layout.summary_bar,
            null);
    bookmarkRibbon = (ImageView) lin.findViewById(R.id.bookmark_selector);
    bookmarkRibbon.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            bookmarkRibbon.setImageResource(R.drawable.bookmark_yellow);
        }
    });
}

as you can see bookmarkRibbon has a listener. however it is not clickable or at least the event is not fired

get View is called from Activity's onCreate method

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

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

发布评论

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

评论(1

梦里寻她 2024-12-12 03:49:41

ImageView默认是不可点击的,即无法注册点击事件。

确保

bookmarkRibbon.setClickable(true);

在引用 imageview 后立即将其设置为可点击。

ImageView, by default are not clickable, i:e cannot register click events.

make sure you set The imageview clickable

bookmarkRibbon.setClickable(true);

right after referencing it.

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