java 获取RecycleView中一行的宽度和高度 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31161040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:12:29  来源:igfitidea点击:

Get Width and Height of one row in RecycleView - Android

javaandroidheightwidth

提问by KiKo

I'm creating RecycleViewwith some items. So I need to get the width and height of the one row of RecycleView

我正在创建RecycleView一些项目。所以我需要得到一行的宽度和高度RecycleView

Here I'm creating RecycleView:

我在这里创建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....

And this is my ArtikliAdapter:

这是我的 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

图片

How can I get width and height of one row?

如何获得一行的宽度和高度?

回答by Amrut Bidri

@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

        }
    });
  .....
}

I hope this will help you!!

我希望这能帮到您!!