它工作得很好,我可以在每个位置的OnClick方法中更改文本和图标的颜色而没有问题:
itemClickSupport.setonItemClickListener(new ItemClickSupport.OnItemClickListener() { @OverrIDe public voID onItemClick(RecyclerVIEw parent,VIEw vIEw,int position,long ID) { TypedValue typedValue = new TypedValue(); MainActivity.this.gettheme().resolveAttribute(R.attr.colorAccent,typedValue,true); final int color = typedValue.data; //Todo Icon and text colors for (int i = 0; i < drawerTitles.length; i++){ if (i == position){ ImageVIEw imageVIEwDrawerIcon = (ImageVIEw) recyclerVIEwDrawer.getChildAt(i).findVIEwByID(R.ID.imageVIEwDrawerIcon); TextVIEw textVIEwDrawerTitle = (TextVIEw) recyclerVIEwDrawer.getChildAt(i).findVIEwByID(R.ID.textVIEwDrawerItemTitle); imageVIEwDrawerIcon.setcolorFilter(color); if(Build.VERSION.SDK_INT > 15){ imageVIEwDrawerIcon.setimageAlpha(255); }else{ imageVIEwDrawerIcon.setAlpha(255); } textVIEwDrawerTitle.setTextcolor(color); relativeLayout relativeLayoutDrawerItem = (relativeLayout) recyclerVIEwDrawer.getChildAt(i).findVIEwByID(R.ID.relativeLayoutDrawerItem); relativeLayoutDrawerItem.setFocusableIntouchMode(true); }else{ ImageVIEw imageVIEwDrawerIcon = (ImageVIEw) recyclerVIEwDrawer.getChildAt(i).findVIEwByID(R.ID.imageVIEwDrawerIcon); TextVIEw textVIEwDrawerTitle = (TextVIEw) recyclerVIEwDrawer.getChildAt(i).findVIEwByID(R.ID.textVIEwDrawerItemTitle); imageVIEwDrawerIcon.setcolorFilter(getResources().getcolor(R.color.md_text)); if(Build.VERSION.SDK_INT > 15){ imageVIEwDrawerIcon.setimageAlpha(138); }else{ imageVIEwDrawerIcon.setAlpha(138); } textVIEwDrawerTitle.setTextcolor(getResources().getcolor(R.color.md_text)); relativeLayout relativeLayoutDrawerItem = (relativeLayout) recyclerVIEwDrawer.getChildAt(i).findVIEwByID(R.ID.relativeLayoutDrawerItem); relativeLayoutDrawerItem.setFocusableIntouchMode(false); } } //Todo Fragments (closedrawers before setfragment) final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @OverrIDe public voID run() { // Do something after some time } },250); mDrawerLayout.closeDrawers(); } });
问题是我想要访问该方法中的第一个项目,但在它之前(设置选择的第一个项目,使用不同的颜色等).
但是当我尝试获取OnClick方法之外的第一个项目时,我得到了崩溃(null引用):
ImageVIEw imageVIEwDrawerIcon = (ImageVIEw) recyclerVIEwDrawer.getChildAt(0).findVIEwByID(R.ID.imageVIEwDrawerIcon);
所有代码:
// Setup RecyclerVIEw insIDe drawer recyclerVIEwDrawer = (RecyclerVIEw) findVIEwByID(R.ID.recyclerVIEwDrawer); recyclerVIEwDrawer.setHasFixedSize(true); recyclerVIEwDrawer.setLayoutManager(new linearlayoutmanager(MainActivity.this)); ArrayList<DrawerItem> drawerItems = new ArrayList<>(); final String[] drawerTitles = getResources().getStringArray(R.array.drawer); final TypedArray drawerIcons = getResources().obtainTypedArray(R.array.drawerIcons); for (int i = 0; i < drawerTitles.length; i++) { drawerItems.add(new DrawerItem(drawerTitles[i],drawerIcons.getDrawable(i))); } drawerIcons.recycle(); adapterDrawer = new DrawerAdapter(drawerItems); recyclerVIEwDrawer.setAdapter(adapterDrawer); // Here is the problem ImageVIEw imageVIEwDrawerIcon2 = (ImageVIEw) recyclerVIEwDrawer.getChildAt(0).findVIEwByID(R.ID.imageVIEwDrawerIcon); // RecyclerVIEw item Listener. ItemClickSupport itemClickSupport = ItemClickSupport.addTo(recyclerVIEwDrawer); itemClickSupport.setonItemClickListener(new ItemClickSupport.OnItemClickListener() { @OverrIDe public voID onItemClick(RecyclerVIEw parent,250); mDrawerLayout.closeDrawers(); } });解决方法 在我的情况下,它的工作就像一个魅力
VIEw vIEwItem = recycleVIEw.getLayoutManager().findVIEwByposition(position); VIEw icon = vIEwItem.findVIEwByID(R.ID.vIEw);总结
以上是内存溢出为你收集整理的android – Recyclerview,我无法通过getChildAt(位置)获取项目视图.空对象引用全部内容,希望文章能够帮你解决android – Recyclerview,我无法通过getChildAt(位置)获取项目视图.空对象引用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)