Recyclerview,我无法通过getChildAt(位置)获取项目视图。空对象引用

时间:2015-02-03 13:29:58

标签: android android-recyclerview get-childitem

我在导航抽屉里面使用了recyclelerView,我正在使用这个库Twoway-view获得点击和选择支持。

它完美无缺,我可以在每个位置的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方法之外获取第一个项目时,我得到了崩溃(空引用):

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, 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();
        }
    });

5 个答案:

答案 0 :(得分:11)

就我而言,它就像魅力一样工作

View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
    View icon = viewItem.findViewById(R.id.view);

答案 1 :(得分:8)

我最终解决了这个问题(source):

recyclerViewDrawer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(0).findViewById(R.id.imageViewDrawerIcon);
            TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(0).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(0).findViewById(R.id.relativeLayoutDrawerItem);
            relativeLayoutDrawerItem.setFocusableInTouchMode(true);
            // unregister listener (this is important)
            recyclerViewDrawer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

顺便说一句,如果有人有更好的解决方案,我想知道。

答案 2 :(得分:4)

我会使用findViewHolderForAdapterPosition()findViewByPosition()来查找该索引的根视图。但是,确保加载数据的方式取决于您。

答案 3 :(得分:3)

这对我有用。

recyclerView.post(new Runnable() {
  @Override
  public void run() {
    View viewItem = recycleView.getLayoutManager().findViewByPosition(position);
    View icon = viewItem.findViewById(R.id.view);
  }
});

答案 4 :(得分:0)

findViewByPositionLayoutManager的一种方法,适用于所有LayoutManager。

因此您可以使用recycleView.getLayoutManager()findViewByPosition(pos)

recyclerView.findViewHolderForAdapterPosition(pos)也可以工作。

有关更多信息,请查看此RecyclerView - Get view at particular position