1. 程式人生 > >android recycleview設定成gridview顯示樣式

android recycleview設定成gridview顯示樣式

android.support.v7.widget.RecyclerView equitemntLlist;
equitemntLlist=(android.support.v7.widget.RecyclerView)findViewById(R.id.equitemntLlist);
equitemntLlist.setHasFixedSize(true);
RecyclerView.LayoutManager greidlayoutManager = new GridLayoutManager(this,6);
equitemntLlist.setLayoutManager(greidlayoutManager);
@Override
public void getIcon(List<Equitment> list) {
if (equitmentAdapter == null || "".equals(equitmentAdapter)) {
    equitmentAdapter = new EquitmentAdapter(list);
    equitemntLlist.setAdapter(equitmentAdapter);
} else {
    equitmentAdapter.setList(list);
}
equitmentAdapter.setOnRecyclerViewListener(this);
}
ublic class EquitmentAdapter extends RecyclerView.Adapter {
    public static interface OnRecyclerViewListener {
        void onEquitmentItemClick(int position, String route,Boolean isShow,List<Equitment> list);
        boolean onEquitmentItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    public void setList(List<Equitment> list) {
        this.list = list;
    }

    public List<Equitment> getList() {
        return list;
    }

    private List<Equitment> list;

    public EquitmentAdapter(List<Equitment> list) {
        this.list = list;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.equitment_item, null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new PersonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        PersonViewHolder holder = (PersonViewHolder) viewHolder;
        holder.position = position;
        Equitment equitment = list.get(position);
        holder.descriptionTv.setText(equitment.getDescription());
        holder.imageNameTv.senametImageResource(equitment.getImageName());
        holder.route=equitment.getDescription();
        holder.isShow=equitment.getCanShow();
        if(equitment.getCanShow()) {
            holder.imSuccess.setVisibility(View.VISIBLE);
        }else{
            holder.imSuccess.setVisibility(View.INVISIBLE);
        }
        Log.d("EquitmentText",equitment.getDescription()+" "+equitment.getImageName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public View rootView;
        public TextView descriptionTv;
        public ImageView imageNameTv,imSuccess;
        public int position;
        public String route;
        public Boolean isShow;

        public PersonViewHolder(View itemView) {
            super(itemView);
            descriptionTv = (TextView) itemView.findViewById(R.id.tv_description);
            imageNameTv = (ImageView) itemView.findViewById(R.id.im_equitmet);
            imSuccess = (ImageView) itemView.findViewById(R.id.im_success);
            rootView = itemView.findViewById(R.id.equitment_root);
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onEquitmentItemClick(position,route,isShow,list);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onEquitmentItemLongClick(position);
            }
            return false;
        }
    }


}
@Override
public void onEquitmentItemClick(int position, String route,Boolean isShow,List<Equitment> list) {
      if(list != null && !"".equals(list) && list.size()>0 && position<list.size()) {
          list.get(position).setCanShow(!isShow);
          tv_title_name.setText("校園智慧");
          equitmentAdapter.setList(list);        
         //點選區域性重新整理
          equitmentAdapter.notifyItemChanged(position);
          Log.d("onEquitmentItemClick",list.get(position).getmCode());
      }
}


結果