如何根据在 recyclerview 中加载的图像数量更改 textview 中的文本

how to change text in textview according to the amount of images are loaded in recyclerview

你好,我想根据 recylerview 中有多少图像来更改文本(以数字表示)

例如,如果 recyclerview 中有 10 张图片,则图片上应该根据图片编号,例如图片 1,文本将为 1,对于图片 2,文本将为 2,依此类推

只是为了知道文本视图是图像视图上的叠加层

我希望数字不仅是 1,还应该根据图像进行排列,例如,如果 recyler 视图有 10 个图像,1 将在文本中显示 1,图像 2 将在文本中显示 2

这是代码XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="5dp"
        android:layout_marginTop="11dp"
        android:layout_marginEnd="5dp"
        app:cardElevation="1dp"
        app:cardMaxElevation="4dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:contentDescription="@string/todo"
                app:shapeAppearanceOverlay="@style/RoundedCorner" />

            <TextView
                android:id="@+id/rankedNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_gravity="start|left"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/circular_background_text"
                android:text="@string/_1"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold|italic"
                tools:ignore="ObsoleteLayoutParam,RtlCompat,RtlHardcoded" />
        </FrameLayout>
    </com.google.android.material.card.MaterialCardView>
</RelativeLayout>

后置适配器Class

public class PostAdapter_Search extends RecyclerView.Adapter<PostAdapter_Search.PostViewHolder> {
    public static List<Upload> mUploads;
    public Context mcontext;
    View view;


    public PostAdapter_Search(Context context, List<Upload> uploads) {
        mUploads = uploads;
        mcontext = context;
    }


    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mcontext).inflate(R.layout.post_item_container_search, parent, false);
        return new PostViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Upload uploadCurrent = mUploads.get(position);
        Glide.with(mcontext)
                .load(uploadCurrent.getmImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .fitCenter()
                .into(holder.imageView);


    }

    @Override
    public int getItemCount() {
        int limit = 10;
        return Math.min(mUploads.size(), limit);
    }

    public static class PostViewHolder extends RecyclerView.ViewHolder {
        ShapeableImageView imageView;
        TextView textView;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.rankedNumber);
        }


        public void setOnItemClickListener(PostAdapter.OnItemClickListener listener) {

        }
    }

}

抱歉,我从未使用过这种类型的库,但我添加了另一种方法来管理图像和照片,但我认为问题在于你不会去编辑你插入布局中的 Textview,所以试试这个,看看你是否得到了你需要的东西:

public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    Glide.with(mcontext)
            .load(uploadCurrent.getmImageUrl())
            .diskCacheStrategy(DiskCacheStrategy.DATA)
            .fitCenter()
            .into(holder.imageView);
///Add this
 PostViewHolder Myholder = (PostViewHolder) holder;
 Myholder.textView.setText(String.valueOf(position));

这只是一个示例,然后您可以插入您喜欢的文字。 我希望被证明是有帮助的