0
CardView的寬度因此,這裏是我的RecyclerView的XML如何獲得內部RecyclerView
<?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>
下面是由RecyclerView的適配器充氣佈局。
<?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>
所以在這裏我需要找到CardView的寬度(@ ID/bikenames_cardview)在我的活動或RecyclerView適配器。我也寫過RecyclerView適配器。
適配器代碼
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));
}
}
我可能會跳過一些代碼,在這裏減少的長度。
noot爲我工作,調用'onCreateViewHolder'中的'ViewTreeObserver'返回0,我所做的是從活動中獲取recyclerview的寬度和ImageView的活動和高度的寬度,並將寬度和高度都傳遞給適配器,只需解決問題:) –