0

So here is my RecyclerView's xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/bikename_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

And here's the layout inflated by the RecyclerView's Adapter.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bikenames_cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical" >

            <ImageView
                android:id="@+id/bikenames_imageview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/bikenames_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </android.support.v7.widget.CardView>

 </LinearLayout>

So here I need to find the width of CardView (@id/bikenames_cardview) in my Activity or in the RecyclerView adapter. I have wrote RecyclerView adapter also.

Adapter code

public class BikeNamesAdapter extends RecyclerView.Adapter<BikeNamesViewHolder> {

    BikeNames bikeNames;
    Context context;

    public BikeNamesAdapter(BikeNames bikeNames) {
        this.bikeNames = bikeNames;
    }

    @Override
    public int getItemCount() {
        return bikeNames.getLength();
    }

    @Override
    public BikeNamesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.cardview_bike_names, parent, false);
        return new BikeNamesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BikeNamesViewHolder holder, int position) {
         holder.textViewBikeName.setText(bikeNames.getBikeNameByIndex(position));

    }

}

I might have skipped some codes, to reduce length here.

capt.swag
  • 10,335
  • 2
  • 41
  • 41

1 Answers1

7

Set listener to your view in onCreateViewHolder

ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
  viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      viewWidth = view.getWidth();
      viewHeight = view.getHeight();
    }
  });
}

Also check this and this question.

Community
  • 1
  • 1
Anton Kovalyov
  • 932
  • 4
  • 13
  • noot working for me,calling `ViewTreeObserver` in `onCreateViewHolder` returns 0, what i did is getting width of recyclerview from activity and height of ImageView present into adapter from activity and pass both width and height to adapter, simply solve the problem :) – Ashu Kumar Jul 21 '17 at 07:37