我想在View
内记录特定孩子RelativeLayout
的ID。我使用下面的代码来实现。但ID会被记录两次。
代码:
ViewGroup parentView = (ViewGroup) v.findViewById(R.id.category_layout);
View childView;
for(int i = 0; i < parentView.getChildCount(); i++) {
childView = parentView.getChildAt(i);
int resID = childView.getId();
if(childView instanceof ViewPager ){
Log.e("Id",Integer.toString(resID));
}
}
的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/category_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/pager"
></android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler"
android:layout_below="@+id/pager">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
日志:
E/Id: 2131492989
E/Id: 2131492989