2

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

在 instanceiteItem 中,我正在像这样实例化我的页面视图

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中的视图,但它总是返回null,有人知道吗?

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

更新 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;
}
4

1 回答 1

4

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

此处有对此功能的增强请求。

我建议找到一种方法来做你试图用 ID 做的事情。例如,您可以拥有一个可以迭代的 ID的TypedArray 。您可以在每个配置的基础上更改此数组(这样它不在您的代码中,并且您拥有在 XML 中定义标签的所有好处)。

于 2013-08-19T16:17:16.383 回答