Android 从另一个视图(而不是父视图)获取视图
我想将两个视图链接在一起。ViewPager和view指示此ViewPager状态,类似于选项卡,但显示方式可能不同(与通常的选项卡相同,一个文本视图,但仅文本会更改),它只是实现了一些界面。 在程序上我可以这样做,比如Android 从另一个视图(而不是父视图)获取视图,android,android-viewpager,Android,Android Viewpager,我想将两个视图链接在一起。ViewPager和view指示此ViewPager状态,类似于选项卡,但显示方式可能不同(与通常的选项卡相同,一个文本视图,但仅文本会更改),它只是实现了一些界面。 在程序上我可以这样做,比如 myViewPager.setIndicator(myIndicatorView); 但是,为此,我必须在某个父视图中找到这两个视图并调用此方法。我想简化它,并且能够在xml模式中简单地管理它。 这里是xml的一个示例 <LinearLayout android
myViewPager.setIndicator(myIndicatorView);
但是,为此,我必须在某个父视图中找到这两个视图并调用此方法。我想简化它,并且能够在xml模式中简单地管理它。
这里是xml的一个示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/someTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.example.views.TextTabs
android:id="@+id/info_tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/info_tabs_height"
android:background="@drawable/header_gradient" />
<com.example.views.InfoPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
custom:tabs="@id/info_tabs" />
</LinearLayout>
问题是我无法通过id获取视图。其思想是,指示器视图可以位于不同的位置,可以与寻呼机同级,也可以位于寻呼机的更高级别,等等。这就是为什么我试图从活动中找到它。但我甚至无法从父视图中找到它,因为getParent返回null
那么,我怎样才能从另一个不是他的父视图的视图中找到视图呢?或者您有其他解决方案吗?问题是该视图没有附加,所以我将一个新的ChangeListener添加到此视图,当它附加后,我将再次搜索视图
//This id equals R.id.info_tabs
int id = attr.getResourceId(R.styleable.ViewPager_tabs, -1);
if (id != -1) {
//i want to find view from all activity
View tabs = ((Activity) context).findViewById(id);
if (tabs != null && tabs instanceof ViewPagerTabs) {
// if tabs finded
mTabs = (ViewPagerTabs) tabs;
setIndicator(mTabs);
} else {
//If we didn't find view - add listener, and waiting while this view will attached
addOnAttachStateChangeListener(new OnAttachStateChangeListener {
@Override
public void onViewAttachedToWindow(View v) {
View tabs = ((Activity)getContext()).findViewById(mTabsViewId);
//
if (tabs != null && tabs instanceof ViewPagerTabs) {
setIndicator((ViewPagerTabs) tabs);
}
//Remove this listener it's not usefull anymore
removeOnAttachStateChangeListener(this);
}
@Override
public void onViewDetachedFromWindow(View v) {
}
});
}
}
看起来不错,但是调用
addOnAttachStateChangeListener(null)
实际上并没有删除侦听器,甚至可能导致空指针异常,因为您告诉它调用null。在ViewAttachedTowindow
下一次连接它时,如果您想这样做,我会使用removeOnAttachStateChangeListener
。
//This id equals R.id.info_tabs
int id = attr.getResourceId(R.styleable.ViewPager_tabs, -1);
if (id != -1) {
//i want to find view from all activity
View tabs = ((Activity) context).findViewById(id);
if (tabs != null && tabs instanceof ViewPagerTabs) {
// if tabs finded
mTabs = (ViewPagerTabs) tabs;
setIndicator(mTabs);
} else {
//If we didn't find view - add listener, and waiting while this view will attached
addOnAttachStateChangeListener(new OnAttachStateChangeListener {
@Override
public void onViewAttachedToWindow(View v) {
View tabs = ((Activity)getContext()).findViewById(mTabsViewId);
//
if (tabs != null && tabs instanceof ViewPagerTabs) {
setIndicator((ViewPagerTabs) tabs);
}
//Remove this listener it's not usefull anymore
removeOnAttachStateChangeListener(this);
}
@Override
public void onViewDetachedFromWindow(View v) {
}
});
}
}