对不可见项

时间:2016-09-24 07:05:26

标签: android android-recyclerview

我正在使用recyclerview进行导航抽屉菜单。当用户选择某个项目时,它会突出显示。我还使用 addOnGlobalLayoutListener 。当我在recyclerview中有5或7个项目时,所有工作都很完美。但是,当我使用超过7个项目时,它会破碎。我错误的原因是我尝试使用getChildAt(position)来创建未创建的项目。

我在这行中有错误

    ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);  

代码:

recyclerViewDrawer = (RecyclerView) findViewById(kz.qobyzbook.R.id.recyclerViewDrawer);
        recyclerViewDrawer.setHasFixedSize(true);
        recyclerViewDrawer.setLayoutManager(new LinearLayoutManager(DMPlayerBaseActivity.this));

    ArrayList<DrawerItem> drawerItems = new ArrayList<>();
    final String[] drawerTitles = getResources().getStringArray(kz.qobyzbook.R.array.drawer);
    final TypedArray drawerIcons = getResources().obtainTypedArray(kz.qobyzbook.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);

    recyclerViewDrawer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            for (int i = 0; i < drawerTitles.length; i++) {

                if (recyclerViewDrawer.getChildAt(i).isEnabled()) {
                    if (i == sharedPreferences.getInt("FRAGMENT", 0)) {
                        ImageView imageViewDrawerIcon = (ImageView) recyclerViewDrawer.getChildAt(i).findViewById(R.id.imageViewDrawerIcon);
                        TextView textViewDrawerTitle = (TextView) recyclerViewDrawer.getChildAt(i).findViewById(kz.qobyzbook.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(kz.qobyzbook.R.id.relativeLayoutDrawerItem);
                        TypedValue typedValueDrawerSelected = new TypedValue();
                        getTheme().resolveAttribute(kz.qobyzbook.R.attr.colorPrimary, typedValueDrawerSelected, true);
                        int colorDrawerItemSelected = typedValueDrawerSelected.data;
                        colorDrawerItemSelected = (colorDrawerItemSelected & 0x00FFFFFF) | 0x30000000;
                        relativeLayoutDrawerItem.setBackgroundColor(colorDrawerItemSelected);

                    } 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(kz.qobyzbook.R.color.md_text));
                        if (Build.VERSION.SDK_INT > 15) {
                            imageViewDrawerIcon.setImageAlpha(138);
                        } else {
                            imageViewDrawerIcon.setAlpha(138);
                        }
                        textViewDrawerTitle.setTextColor(getResources().getColor(kz.qobyzbook.R.color.md_text));
                        RelativeLayout relativeLayoutDrawerItem = (RelativeLayout) recyclerViewDrawer.getChildAt(i).findViewById(kz.qobyzbook.R.id.relativeLayoutDrawerItem);
                        relativeLayoutDrawerItem.setBackgroundColor(getResources().getColor(kz.qobyzbook.R.color.md_white_1000));
                    }
                }
            }
        // unregister listener (this is important)
        recyclerViewDrawer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
    });

我使用recyclerview的布局

<?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:orientation="vertical"
        >
    <RelativeLayout
        android:id="@+id/relativeLayoutMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:id="@+id/imageViewCover"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/drawer_header3"
            android:scaleType="fitStart"
            />
    </RelativeLayout>

    <!-- Set visibility to visible to edit -->


    <LinearLayout
        android:layout_below="@+id/relativeLayoutMain"
        android:id="@+id/linearLayoutMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        android:visibility="visible">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewDrawer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
             />

    </LinearLayout>
</LinearLayout>

和字符串文件

<array name="drawerIcons">
        <item>@drawable/ic_allsongs</item>
        <item>@drawable/ic_allsongs</item>
        <item>@drawable/ic_favorite</item>
        <item>@drawable/ic_allsongs</item>
        <item>@drawable/ic_allsongs</item>
        <item>@drawable/ic_library_music</item>
        <item>@drawable/ic_favorite</item>
        <item>@drawable/ic_equalizer</item>
        <item>@drawable/ic_content_send</item>
        <item>@drawable/ic_allsongs</item>
    </array>

0 个答案:

没有答案