Android关于RecyclerView 设置选中效果和AndroidTV Item选中效果

Android关于RecyclerView设置选中效果和AndroidTV Item选中效果

Android关于RecyclerView设置选中效果

项目里需求2级菜单,要设置recyclerview高亮.

代码块

点击事情里面设置:

nAdapter.refreshItem(position);

adapter里面自定义

private int selectedPos = -1;
private int oldPos = -1;

getView(ViewHolder helper) {
    if(selectedPos == helper.getPosition()) {
        //相同设置高亮
    } else {
        //不同设置
    }
}

public void refreshItem(int position) {
    if (selectedPos != -1) {
        oldPos  = selectedPos;
    }
    selectedPos = position;
    if (oldPos != -1) {
        notifyItemChanged(oldPos);
    }
    notifyItemChanged(selectedPos);
}

AndroidTV Item选中效果

最近看了谷歌TV的adapter,感觉很有意思:

public class CardPresenter extends Presenter {
private static final String TAG = "CardPresenter";

private static final int CARD_WIDTH = 313;
private static final int CARD_HEIGHT = 176;
private static int sSelectedBackgroundColor;
private static int sDefaultBackgroundColor;
private Drawable mDefaultCardImage;

private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
    int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
    //这里还可以设置选中放大 失去焦点缩小
    view.setBackgroundColor(color);
    view.findViewById(R.id.info_field).setBackgroundColor(color);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    Log.d(TAG, "onCreateViewHolder");

    sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
    sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
    mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);

    ImageCardView cardView = new ImageCardView(parent.getContext()) {
        @Override
        public void setSelected(boolean selected) {
            updateCardBackgroundColor(this, selected);
            super.setSelected(selected);
        }
    };

    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    updateCardBackgroundColor(cardView, false);
    return new ViewHolder(cardView);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
    Movie movie = (Movie) item;
    ImageCardView cardView = (ImageCardView) viewHolder.view;

    if (movie.getImgUrl() != null) {
        cardView.setTitleText(movie.getName());
        cardView.setContentText(movie.getContent());
        cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
        Glide.with(viewHolder.view.getContext())
                .load(movie.getImgUrl())
                .centerCrop()
                .error(mDefaultCardImage)
                .into(cardView.getMainImageView());
    } else {
        cardView.setTitleText("woo");
        cardView.setContentText("hiiiiii");
        cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
    }
}

@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
     Log.d(TAG, "onUnbindViewHolder");
     ImageCardView cardView = (ImageCardView) viewHolder.view;
     // Remove references to images so that the garbage collector can free up memory
     cardView.setBadgeImage(null);
     cardView.setMainImage(null);
     cardView.setBadgeImage(null);
     cardView.setMainImage(null);
    }
}

把present改造成adapter即可。
这个最好配合谷歌的android.support.v17.leanback.widget.HorizontalGridView和VerticalGridView使用。

你可能感兴趣的:(android)