我正在使用 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;
}