1

我有一个自定义组件(类似于信息面板) - 带有少量文本视图的 LinearLayout。我正在使用android注释。

问题
如果我尝试使用@AfterViews,则永远不会调用该方法(问题1)。
问题是当我尝试通过注释在文本视图中设置某些内容时,该文本视图为空(问题 2)..

如果我从另一个类尝试,我确信android注释可以工作,我可以轻松且没有任何问题地进入那个textView并在那里设置一些东西..变量不为null并且没有问题..但是在另一个类中我使用 AA,我也使用 Activity..:/ 所以我在想@EViewGroup 是否有问题.. 但是根据他们的 javadoc,@EVIewGroup 对 AfterViews 和 ViewById 没有问题..(根据他们的网站,快一切都是 viewGroup :] 我认为我选择了从线性布局扩展的正确可能性)

有人有一些提示我缺少什么吗?

-- 我在另一个片段中使用该自定义组件。
-- 这个类中有一个类调用方法(--> 在代码预览中--somethingIsCalling()),当想要在textView中显示某些东西时会触发该方法)

问题代码:

自定义组件布局(info_panel.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:id="@+id/infoPanel">
    <TextView
        android:id="@+id/txtAccName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical" />
</LinearLayout>

该自定义组件的 java 代码:

@EViewGroup(R.layout.info_panel)
public class InfoPanel extends LinearLayout {
    @ViewById(R.id.txtAccName)
    protected TextView txtName;

        public InfoPanel(Context ctx, AttributeSet attr)
    {
        super(ctx, attr);
        LayoutInflater.from(ctx).inflate(R.layout.info_panel, this);
    }

    @AfterViews
    protected void init() 
    {
            // PROBLEM 1, THIS METHOD IS NEVER CALLING
        txtName.setText("call after view");
    }

    public void somethingIsCalling()
    {
            // PROBLEM 2, txtName WHEN ITS CALLED IS NULL
            txtName.setText("something is calling");
    }
}
4

2 回答 2

4

以下是我将如何重写您的代码以使其工作:

layout_info_panel.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/txtAccName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right|center_vertical" />
</merge>

请注意如何<LinearLayout/><merge/>此处替换 root 但仍然能够使用所有<LinearLayout/>特定属性(例如layout_weight="1")(https://github.com/excilys/androidannotations/wiki/Enhance-custom-views#how-to-create-it-
这是可能的,因为您的自定义class InfoPanel extends LinearLayout.
作为奖励,您可以删除 1 级层次结构,这总是很小的性能提升(http://www.curious-creature.org/2012/12/01/android-performance-case-study/

InfoPanel.java

@EViewGroup(R.layout.info_panel)
public class InfoPanel extends LinearLayout {
    @ViewById(R.id.txtAccName)
    protected TextView txtName;

    public InfoPanel(Context ctx, AttributeSet attr) {
        super(ctx, attr);
        // remove this inflation, AndroidAnnotations does this for you in @EViewGroup(R.layout.info_panel)
        // LayoutInflater.from(ctx).inflate(R.layout.info_panel, this);
    }

    @AfterViews
    protected void init() {
            // PROBLEM 1 would be fine now
        txtName.setText("call after view");
    }

    public void somethingIsCalling() {
            // PROBLEM 2 also will be resolved IF you call it after @AfterViews method has done binding.
            txtName.setText("something is calling");
    }
}

现在无论你想在xml中包含你的InfoPanel,确保包含生成的类(一个带有_underscore的类),否则你的字段确实总是为空,即

编辑:显然在这种情况下,这会导致OP出现问题并更改<com.stackoverflow.answers.InfoPanel>为已<com.stackoverflow.answers.InfoPanel_>解决的字段是null.

layout_ordinary_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <com.stackoverflow.answers.InfoPanel_
        android:id="@+id/infoPanelView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    …
</LinearLayout>

https://github.com/excilys/androidannotations/wiki/Enhance-custom-views#how-to-use-it-
我建议您阅读 AndroidAnnotations wiki 中的所有 Cookbook,有几个更常见的陷阱那里。

于 2013-10-28T21:13:44.517 回答
0

尝试从@EViewGroup 中删除参数,它为我解决了一个类似的问题。

于 2013-10-24T19:01:04.687 回答