Change the alpha of recyclerview item with beacon

2019-09-11 04:33发布

问题:

In a Fragment I have a list of items construct with RecyclerView (adapter and viewholder). Each item of the list is linked with a estimote beacon. So i want to highlight the item view (setAlpha or add a imageView) when the beacon is detected.

The beacon detection is in the fragment file :

beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(Region region, List<Beacon> list) {
                if (!list.isEmpty()) {
                    Beacon nearestBeacon = list.get(0);   
                    replaceIconBeaconCard(nearestBeacon);                     
                    Log.d("Airport", "Nearest places: " + nearestBeacon);
                }
            }
        });

Actually, I display a beacon icon on the item view. But the purpose is to highlight the detected item and put a alpha on the others items.

private void replaceIconBeaconCard(Beacon beacon){
        for(SItem item: items) {
            if (!item.getMajor().equals("")) {
                int i = item.getId().intValue();
                SCard card = listCards.get(i-1);
                if (Integer.parseInt(item.getMajor()) == beacon.getMajor()) {
                    rv.getLayoutManager().scrollToPosition(i);                    
                    int resID = getResources().getIdentifier("beacon_ice", "drawable", getActivity().getPackageName());
                    card.setField_1(resID);
                } else {
                    card.setField_1(Color.TRANSPARENT);
                }
            }
        }
        mAdapter.notifyDataSetChanged();
    }

I don't know how I can access to item layout and change the alpha.

回答1:

Finally i access to the layout alpha with the object SCard in the adapater :

In the activity i set the alpha

card.setField_2(0.5F);

In the viewholder i add the setter

 public void setItemCardField2(float alpha) {
        swipeLayout.setAlpha(alpha);
    }

And the adapter make the job

final ShowroomCard card = mItemList.get(position);
viewHolder.setItemCardField2(card.getField_2());