根据Recycler Adapter中的位置在ImgeView上设置Alfa

时间:2016-11-26 06:43:07

标签: android android-recyclerview

我有一个RecyclerView,我正在填充4个imageView。我需要的是,当First Item位于顶部可见位置时,它必须是Alfa 1和其他物品必须在0.2f Alfa上。

当我滚动我的回收者视图然后,物品谁位于最高位置,它应该被设置为阿尔法1.我做了什么..

我的适配器

  @Override
public void onBindViewHolder(PageCircleIndiAdapter.ViewHolder holder, int position) {
    final PageModels pageModels = pageList.get(position);
    if (pageList.get(position) != null) {
        holder.imageView.setImageResource(pageModels.resourceId);

    }
    if (position == 0) {
        holder.rlyt.setAlpha(1f);
        pageCircleIndicator.changeIndicator(position);
    }

     else if (position >firstVisible && position<lastVisible) {
        holder.rlyt.setAlpha(1f);
        pageCircleIndicator.changeIndicator(position);
    } else if(position==pageList.size()-1){
        holder.rlyt.setAlpha(1f);
        pageCircleIndicator.changeIndicator(position);
    }
    else {
        holder.rlyt.setAlpha(0.2f);
        pageCircleIndicator.changeIndicator(position);
    }
}

**我的MainActivity Scroll Listener **

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();

            int firstVisible = manager.findFirstVisibleItemPosition();
            int lastVisible = manager.findLastVisibleItemPosition();
            Log.i("TAG", "onScrolled: " + firstVisible);
            Log.i("TAG", "Last Index: " + lastVisible);
            pageAdapter.changeItem(firstVisible, lastVisible);

        }

    });

    createIndicator();
    pageAdapter.notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:1)

默认情况下,在xml布局中保留项目布局alfa 0.2f。

现在,当您使用changeItem方法将第一个可见项位置传递给addOnScrollListener中的适配器时。在适配器中进行如下更改:

@Override
public void onBindViewHolder(PageCircleIndiAdapter.ViewHolder holder, int position) {
    final PageModels pageModels = pageList.get(position);
    if (pageList.get(position) != null) {
        holder.imageView.setImageResource(pageModels.resourceId);

    }
    if (position == 0 || position == firstVisible) {
        holder.rlyt.setAlpha(1f);
        pageCircleIndicator.changeIndicator(position);
    }else {
        holder.rlyt.setAlpha(0.2f);
        pageCircleIndicator.changeIndicator(position);
    }
}