mRecycleView = findViewById(R.id.recycle_view);
GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 1);
gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
mRecycleView.setLayoutManager(gridLayoutManager);
//关闭点击图片刷新的动画效果 不然感觉像bug
DefaultItemAnimator itemAnimator = (DefaultItemAnimator) mRecycleView.getItemAnimator();
itemAnimator.setSupportsChangeAnimations(false);
mPlatformdapter = new GamePlatformAdapter(mActivity, mGamePlatrorm);
mRecycleView.setAdapter(mPlatformdapter);
mPlatformdapter.setOnItemClickListener(new GamePlatformAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
GameInfoBean.GameTypeCategoryListBean bean = mGamePlatrorm.get(position);
}
@Override
public void onItemLongClick(View view, int position) {
}
});
public class GamePlatformAdapter extends RecyclerView.Adapter<GamePlatformAdapter.MyTVHolder> {
private final LayoutInflater mLayoutInflater;
private OnItemClickListener mOnItemClickListener;
private final Context mContext;
private final List<GameInfoBean.GameTypeCategoryListBean> mItems;
private int selectorPosition = 0;
public GamePlatformAdapter(Context context, List<GameInfoBean.GameTypeCategoryListBean> items) {
mLayoutInflater = LayoutInflater.from(context);
mContext = context;
mItems = items;
}
@NonNull
@Override
public GamePlatformAdapter.MyTVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyTVHolder(mLayoutInflater.inflate(R.layout.item_game_platform, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyTVHolder holder, int position) {
GameInfoBean.GameTypeCategoryListBean bean = mItems.get(position);
holder.mTextView.setText(bean.getName());
Glide.with(mContext).load(bean.getImg_url()).into(holder.mImageView);
if (selectorPosition == position) {
holder.mTextView.setTextColor(mContext.getResources().getColor(R.color.theme));
holder.mView.setVisibility(View.VISIBLE);
} else {
//其他的恢复原来的状态
holder.mTextView.setTextColor(mContext.getResources().getColor(R.color.gray_color));
holder.mView.setVisibility(View.INVISIBLE);
}
//设置点击监听
if (mOnItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
if (selectorPosition != position) {
//先取消上个item的勾选状态
notifyItemChanged(selectorPosition);
//设置新Item的勾选状态
selectorPosition = position;
notifyItemChanged(position);
} else if (selectorPosition == -1) {
selectorPosition = position;
notifyItemChanged(position);
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return mItems == null ? 0 : mItems.size();
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
this.mOnItemClickListener = mOnItemClickListener;
}
class MyTVHolder extends RecyclerView.ViewHolder {
private ImageView mImageView;
private TextView mTextView;
private View mView;
MyTVHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.iv_image);
mTextView = itemView.findViewById(R.id.tv_name);
mView = itemView.findViewById(R.id.view_line);
}
}
}