androidandroid-recyclerviewtextviewimageviewsettext

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


Hello i want to change the text (which are in numbers ) according to how many images are there in a recylerview

for eg If there is 10 images in a recyclerview there should be numbers on the image according to the image like for image 1 the text will be 1 for image 2 the text will 2 and so on

just for knowing the textview is an overlay on the image view

I want the Numbers to be not only 1 it should be arrage according to images for eg if recyler view has 10 image 1 will show 1 in text image 2 will show 2 in text

I want the Numbers to be not only 1 it should be arrage according to images for eg if recyler view has 10 image 1 will show 1 in text image 2 will show 2 in text

here is the code 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>

PostAdapter 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) {

        }
    }

}

Solution

  • Sorry but this type of library I've never used but I add another method to manage images and photos, but I think the problem is that you don't go to edit your Textview that you inserted in the layout, So try this and check if you get what you need:

    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));
    

    it is just an example, then you can insert the text you prefer. I hope to be proved helpful