ExpandableListAdapter.getChildView() 与 AbsListView.LayoutParams 交互

发布于 2024-11-05 17:37:24 字数 815 浏览 3 评论 0 原文

我正在开发 BaseExpandableListAdapter 的扩展,并具有以下 getChildView() 实现:

public View getChildView(int groupPosition, int childPosition, 
    boolean isLastChild, View convertView, ViewGroup parent) 
{
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams
        (ViewGroup.LayoutParams.FILL_PARENT, 48);
    RelativeLayout layout = new RelativeLayout(myExpandableListActivity.this);
    layout.setLayoutParams(lp);
    TextView name = new TextView(myExpandableListActivity.this);
    name.setText("testing...");
    layout.addView(name);
    return layout;
}

此代码正在运行,当我运行应用程序、展开组并单击子项时,父应用程序能够检测到 onChildClick( )正确。

但是,我注意到如果我不调用 setLayoutParms(),onChildClick() 将不再起作用。我的问题是,当我将 AbsListView.LayoutParams 分配给返回的子视图时会发生什么?为什么需要让我的 onChildClick() 在父应用程序中响应?

提前致谢!

I'm working on an extension of BaseExpandableListAdapter, and have the following implementation of getChildView():

public View getChildView(int groupPosition, int childPosition, 
    boolean isLastChild, View convertView, ViewGroup parent) 
{
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams
        (ViewGroup.LayoutParams.FILL_PARENT, 48);
    RelativeLayout layout = new RelativeLayout(myExpandableListActivity.this);
    layout.setLayoutParams(lp);
    TextView name = new TextView(myExpandableListActivity.this);
    name.setText("testing...");
    layout.addView(name);
    return layout;
}

This code is working, and when I run the application, expand a group and click on a child, the parent application is able to detect onChildClick() correctly.

However, I notice that the onChildClick() does not work anymore if I don't call the setLayoutParms(). My question is, what is happening when I assign AbsListView.LayoutParams to the child view being returned? Why is this required to have my onChildClick() respond in the parent application?

Thanks in advance!

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

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

发布评论

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

评论(1

中性美 2024-11-12 17:37:24

在进一步研究代码的其他部分之后,看起来省略 setLayoutParms() 并不是导致我的 onChildClick() 无法工作的原因。我还使用布局充气器来设置我的RelativeLayout,其中包括一个复选框。事实证明,是复选框导致孩子无法点击,我必须进一步研究这一点。

RelativeLayout spellLayout = (RelativeLayout) 
    getLayoutInflater().inflate(R.layout.testLayout, null);

感谢您的阅读!

After playing with other parts of the code some more, it looks like leaving out setLayoutParms() was not what was causing my onChildClick() to not work. I was also using a layout inflater to set up my RelativeLayout, which included a CheckBox. Turns out it was the CheckBox that was causing the child to be unclickable, something that I'll have to look into further.

RelativeLayout spellLayout = (RelativeLayout) 
    getLayoutInflater().inflate(R.layout.testLayout, null);

Thanks for reading!

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