我有RecyclerView
一個固定的尺寸比
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintDimensionRatio="35:16"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
我需要顯示ImageView
里面的串列RecyclerView
,但是螢屏上顯示的影像數量是固定的,其余的影像需要滾動才能顯示,類似于影像
ImageView
我在里面創建了這個布局:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@ id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/image2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
這里的問題
layout_width
我嘗試了很多,但是如果沒有設定andlayout_hieght
固定數字,影像就不會顯示。我想要的是使用Hight和Screen Width動態ImageView
設定hight-width 。RecyclerView
uj5u.com熱心網友回復:
解決方案非常簡單:
傳入DisplayMetrics
配接器建構式以獲取螢屏寬度。
DisplayMetrics displayMetrics;
public SearchSliderOneAdapter(DisplayMetrics displayMetrics) {
this.displayMetrics = displayMetrics;
}
并計算配接器內部函式ImageView
= ScreenWidth\numberOfImage的寬度。onCreateViewHolder
public SearchSliderOneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_image_slider, parent, false);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
displayMetrics.widthPixels / numberOfImage,
GridLayout.LayoutParams.MATCH_PARENT, 1);
inflate.setLayoutParams(params);
return new SearchSliderOneViewHolder(inflate, listener);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488246.html
標籤:爪哇 安卓 安卓布局 android-recyclerview 安卓图像视图