Chirayu Bansal
Chirayu Bansal

Reputation: 19

Text view Visiblity is repeating automatically in recycler view

In my app I have set text visiblity to invisible and i am using firebase database. So, if from my firebase databse permission is true the text view is set to visible. It is working but problem in in recycler view list items it is visible after every 8th item of list. Please check Screenshot for my problem

using log.d I have checked my items having permission true and ther result is shown true as i have given only one true value.

Here is the link to my screenshot https://ibb.co/YDpfgxF

Menuadapter.java - my text view was "recommended"

package com.example.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.constraintlayout.widget.Constraints.TAG;

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.menuViewHolder>{

    Context context;
    ArrayList<dish> dishes;

    public MenuAdapter(Context c , ArrayList<dish> p)
    {
        context = c;
        dishes = p;

    }

    @NonNull
    @Override
    public menuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(TAG ,"On Create View Holder: called");

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);

        return new MenuAdapter.menuViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull menuViewHolder holder, final int position) {
        holder.dishname.setText(dishes.get(position).getDishname());
        holder.dishrate.setText(dishes.get(position).getDishrate());



        Picasso.get().load(dishes.get(position).getDishImage()).placeholder(R.drawable.ic_launcher_background).into(holder.dishImage);


        holder.dishImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, " : " + dishes.get(position).getDishImage() );
                Toast.makeText(context,"clicked on" + dishes.get(position).getDishname(), Toast.LENGTH_SHORT).show();
            }
        });


        boolean flag = dishes.get(position).getPermission();
        if (flag){
            Log.d(TAG," recommended : " + dishes.get(position).getDishname());
            holder.recommended.setVisibility(View.VISIBLE);

        }



    }

    @Override
    public int getItemCount() {
        return dishes.size();
    }

    class menuViewHolder extends RecyclerView.ViewHolder {

        TextView dishname, dishrate, recommended;
        ImageView dishImage;


            public menuViewHolder(@NonNull View itemView)
            {
                super(itemView);
                dishname = (TextView) itemView.findViewById(R.id.dishname);
                dishrate = (TextView) itemView.findViewById(R.id.dishrate);
                dishImage = (ImageView) itemView.findViewById(R.id.dishImage);
                recommended = (TextView) itemView.findViewById(R.id.recommended);

            }
        }

}

CardView.xml - textview id "recommended"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="wrap_content"
    android:layout_margin="1dp">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="1dp"
        app:cardCornerRadius="2dp"
        app:cardMaxElevation="8dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:gravity="center_vertical">



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginStart="10dp"
            android:id="@+id/dishname"
            android:textColor="@color/dishnamecolor"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Recommended"
            android:layout_marginStart="10dp"
            android:background="@color/recommended"
            android:id="@+id/recommended"
            android:textColor="#ffffff"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:id="@+id/dishrate"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:textColor="@color/dishratecolor"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end"

        >

        <ImageView
            android:id="@+id/dishImage"
            android:layout_width="100dp"
            android:scaleType="fitXY"
            android:layout_marginRight="10dp"
            android:layout_gravity="end"
            android:layout_marginLeft="20dp"
            android:layout_height="100dp" />
    </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>
I/art: Do partial code cache collection, code=26KB, data=29KB
    After code cache collection, code=25KB, data=28KB
    Increasing code cache capacity to 128KB
D/Constraints: On Create View Holder: called
D/Constraints: On Create View Holder: called
D/Constraints: On Create View Holder: called
V/FA: Inactivity, disconnecting from the service
I/art: Do partial code cache collection, code=57KB, data=60KB
I/art: After code cache collection, code=57KB, data=60KB
    Increasing code cache capacity to 256KB
D/Constraints:  recommended : Chili Paneer(Dry)

Upvotes: 1

Views: 73

Answers (3)

Emiliano Schiavone
Emiliano Schiavone

Reputation: 101

I cannot comment an answer, so Im creating this answer.

You should include this because as you know you are using a RecyclerView, is a component that recycle your item/view so probably is recycling an item that has the textview visible

boolean flag = dishes.get(position).getPermission();
    if (flag){
        holder.recommended.setVisibility(View.VISIBLE);

    } else {
        holder.recommended.setVisibility(View.GONE);
    }

Upvotes: 1

Shariful Islam Mubin
Shariful Islam Mubin

Reputation: 2286

boolean flag = dishes.get(position).getPermission();
    if (flag){
        holder.recommended.setVisibility(View.VISIBLE);

    } else {
        holder.recommended.setVisibility(View.GONE);
    }

Upvotes: 1

ahmad shafizadeh
ahmad shafizadeh

Reputation: 79

i didn't actually manage to understand your problem but i think it would be solve if you add an else after checking the status of permission like this

if (flag){
        Log.d(TAG," recommended : " + dishes.get(position).getDishname());
        holder.recommended.setVisibility(View.VISIBLE);
    }else{
        holder.recommended.setVisibility(View.INVISIBLE);

 }

Upvotes: 1

Related Questions