横向recyclerView程序自动切换并高亮选中item

思路:
Adapter实现点击和高亮事件,然后在recyclerView滚动到对应位置并performClick:
mRecyclerView.scrollToPosition(index);
mRecyclerView.postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        if(mRecyclerView.findViewHolderForAdapterPosition(index)!=null )
        {

            mRecyclerView.findViewHolderForAdapterPosition(index).itemView.performClick();
        }
    }
},50);
一定要用postDelayed,不然切换到屏幕外的item会报错null,参考:
http://stackoverflow.com/questions/32836844/android-recyclerview-findviewholderforadapterposition-returns-null
其中adapter代码如下:
public class PlayerScoreAdapter extends RecyclerView.Adapter /*implements View.OnClickListener*/{
    private  Player[]  players;
    private Context context;
    private int selectedPos = 0;

    public PlayerScoreAdapter(Player[] players, Context context) {
        this.players = players;
        this.context = context;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    //define interface
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , String data);
    }

    //创建新View,被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup,  int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_player_score, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        //将创建的View注册点击事件
//        view.setOnClickListener(this);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setSelected(selectedPos == position);
        // Handle item click and set the selection
//        viewHolder.itemView.setFocusable(true);
        ImageView imageView = (ImageView)viewHolder.itemView.findViewById(R.id.head_image);
        if(selectedPos == position){
            imageView.setImageAlpha(255);
        }else{
            imageView.setImageAlpha(100);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Redraw the old selection and the new
                notifyItemChanged(selectedPos);
                selectedPos = position;
                notifyItemChanged(selectedPos);
//                notifyItemRangeChanged(0, players.length);
                mOnItemClickListener.onItemClick(v,(String)v.getTag());
            }
        });


        Glide.with(context).load(players[position].getHeadImg())
                .bitmapTransform(new CropCircleTransformation(context))
                .crossFade(1000).error(R.mipmap.head_sample).into(viewHolder.mImageView);
        //将数据保存在itemView的Tag中,以便点击时进行获取
        viewHolder.itemView.setTag(players[position].getName());
    }

    /*@Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(String)v.getTag());
        }
    }*/

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        return players.length;
    }
    //自定义的ViewHolder,持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public ViewHolder(View view){
            super(view);
            mImageView = (ImageView) view.findViewById(R.id.head_image);
        }
    }
}

你可能感兴趣的:(横向recyclerView程序自动切换并高亮选中item)