web-dev-qa-db-ja.com

RecycleViewで1行の幅と高さを取得-Android

いくつかのアイテムでRecycleViewを作成しています。したがって、RecycleViewの1行の幅と高さを取得する必要があります

ここで私はRecycleViewを作成しています:

    RecyclerView rvSmetki = (RecyclerView) findViewById(R.id.rvArtikli);
    rvSmetki.setLayoutManager(new GridLayoutManager(this, 3););
    rvSmetki.setAdapter(new ArtikliAdapter(this));

    // Here I want to get width and height....

そして、これは私のArtikliAdapterです。

public class ArtikliAdapter extends RecyclerView.Adapter<ArtikliAdapter.ViewHolder> {

    private static Context context;

    private LayoutInflater inflater;
    private ArrayList<Artikl> artikliList;

    public ArtikliAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        artikliList = LogInActivity.getArtiklList();
    }

    @Override
    public ArtikliAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View row = inflater.inflate(R.layout.artikli_row, parent, false);
        return new ArtikliAdapter.ViewHolder(row);
    }

    @Override
    public void onBindViewHolder(ArtikliAdapter.ViewHolder holder, int position) {
        Artikl currentArtikl = artikliList.get(position);

        holder.tvNaziv.setText(currentArtikl.getNaziv());
        holder.tvCena.setText("Цена: " + currentArtikl.getProdaznaCena());
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlArtikl;
        private TextView tvNaziv;
        private TextView tvCena;

        public ViewHolder(View itemView) {
            super(itemView);

            rlArtikl = (RelativeLayout) itemView.findViewById(R.id.rlArtikl);
            tvNaziv = (TextView) itemView.findViewById(R.id.tvNaziv);
            tvCena = (TextView) itemView.findViewById(R.id.tvCena);
        }
    }
}

Image

1つの行の幅と高さを取得するにはどうすればよいですか?

21
KiKo
@Override
public void onBindViewHolder(final MyAdapter.MyViewHolder holder, int position) 
{
 .....
    holder.itemView.post(new Runnable()
    {
        @Override
        public void run()
        {

            int cellWidth = holder.itemView.getWidth();// this will give you cell width dynamically
            int cellHeight = holder.itemView.getHeight();// this will give you cell height dynamically

        }
    });
  .....
}

これがお役に立てば幸いです!!

31
Amrut Bidri