Perhaps not quite correctly put it. The bottom line is this. I have a Recyclerview. I wrote my adapter for it. There are 3 types of cells. Depending on the type of cell connects its template (layout). One of the types of advertising cells. For it connects the following layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ad_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="vertical"> </LinearLayout> In the adapter, this template is connected as follows:
public PostFeedAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { this.mContext = parent.getContext(); View view; switch (viewType) { case HEADER: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_header_layout, parent, false); return new RecyclerViewHolder(view, viewType, headerType, header.parsed() && header.isFriendignoreBtnsEnabled()); case POST: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_post_layout, parent, false); return new RecyclerViewHolder(view, viewType, headerType, header.parsed() && header.isFriendignoreBtnsEnabled()); case ADVERT: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adspost_layout, parent, false); return new RecyclerViewHolder(view, viewType, headerType, header.parsed() && header.isFriendignoreBtnsEnabled()); default: return null; } } Namely this is adspost_layout. To make an ad request, I need to specify the size of the ad unit. In the examples of Google code, I found this method:
mRecyclerView.post(new Runnable() { @Override public void run() { final LinearLayout lv = (LinearLayout) findViewById(R.id.ad_view); final int adWidth = lv.getWidth(); } }); But this method does not work for me. My lv turns out to be null, so I can't get its size. Honestly, I first meet the .post (new Runnable () method, so I don’t quite understand how it works and what needs to be done to make it work correctly.
How do I get the correct width of the LinearLayout?