Android findViewWithTag 返回 null

标签 android xml

我正在使用 PagerAdapter,并希望使用 findViewWithTag 方法,但它总是返回 null。

在 instantiateItem 中,我像这样实例化我的页面 View

public Object instantiateItem(View view, int position) {
    View pageView = getPage(position);
    ((ViewPager) view).addView(pageView);
    return pageView;
} 

在 getPage 方法中我正在这样做:

 LinearLayout pageView = (LinearLayout)mInflater.inflate(R.layout.pageritem_news,null,false);

在 for 循环中,我试图通过标签获取 pageView 内的 View ,但它总是返回 null,有人有任何想法吗?

也许问题可能是由 pageritem_news.xml 引起的,因为我使用 include 将 View 包含到布局中并在 <include /> 中定义 android:tag在 XML 中?

更新 pageritem_news.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:baselineAligned="false"
    android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_0_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="0"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_0_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="5"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_0_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="10"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_1_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="1"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_1_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="6"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_1_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="11"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_2_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="2"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_2_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="7"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_2_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="12"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_3_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="3"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_3_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="8"
        layout="@layout/listitem_news"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_3_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:tag="13"
        layout="@layout/listitem_news"
        android:visibility="invisible" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_divider_height"
    android:background="@drawable/horizontal_divider" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:weightSum="3" >

    <include
        android:id="@+id/newspage_item_row_4_left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/listitem_news"
        android:tag="4"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_4_middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/listitem_news"
        android:tag="9"
        android:visibility="invisible" />

    <include
        android:id="@+id/newspage_item_row_4_right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/listitem_news"
        android:tag="14"
        android:visibility="invisible" />
</LinearLayout>

</LinearLayout>

PagerAdapter的相关代码部分:

@Override
public Object instantiateItem(View view, int position) {
    View pageView = getPage(position);
    ((ViewPager) view).addView(pageView);
    return pageView;
}


private View getPage(int position) {
    LinearLayout pageView = (LinearLayout)mInflater.inflate(R.layout.pageritem_news,null ,false);

    int index=0;
    View viewToBuild = null;
    while (index < mMovers.size()) {
        viewToBuild = pageView.findViewWithTag(String.valueOf(index));
        System.out.println("View to build: " + viewToBuild);
        if (viewToBuild != null) {
            buildNewsView(viewToBuild, mNews.get(index));
        }
        index++;
    }

    return pageView;
}

最佳答案

根据 the documentation ,您只能覆盖 layout_*使用 <include /> 时的属性(本质上,任何会进入相应 LayoutParams 对象的内容)。

有对此功能的增强请求 here .

我建议找到一种方法来完成您想要使用 ID 执行的操作。例如,您可以有 TypedArray您可以迭代的 ID 数。您可以在每个配置的基础上更改此数组(这样它就不会出现在您的代码中,并且您可以享受在 XML 中定义标签的所有好处)。

关于Android findViewWithTag 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18314853/

相关文章:

java - 尝试获取在android中绘制的每个随机圆的x,y坐标

在 Android 中使用 FFmpeg 时出现 Android NDK 错误?

c++ - 尝试使用 boost 读取 xml 时出现空指针

java - 使用 JAXB 仅编码选定的字段

xml - Applescript - 解析 XML

Swift:缩小 XML

java - 从项目(3点)中选择时如何更改位于菜单栏中的项目

android - SQLite 将列转换为逗号分隔的字符串

android - libgdx 还是 playn?

javascript - 如何通过 JavaScript 在 JSON 中构建此 XML 结构?