简体   繁体   English

文本视图Visiblity在回收器视图中自动重复

[英]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. 在我的应用程序中,我将文本visiblity设置为不可见,我正在使用firebase数据库。 So, if from my firebase databse permission is true the text view is set to visible. 因此,如果从我的firebase数据库权限为true,则文本视图将设置为可见。 It is working but problem in in recycler view list items it is visible after every 8th item of list. 它在循环器视图列表项中工作但问题是在列表的每个第8项之后可见。 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. 使用log.d我检查了我的项目有权限是真的,结果显示为真,因为我只给了一个真值。

Here is the link to my screenshot https://ibb.co/YDpfgxF 这是我的截图https://ibb.co/YDpfgxF的链接

Menuadapter.java - my text view was "recommended" Menuadapter.java - 我的文字视图是“推荐”

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" CardView.xml - textview id“推荐”

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

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 我实际上并没有设法理解你的问题,但我认为如果你在检查了这样的权限状态后添加了一个else就会解决

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

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

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

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 你应该包括这个,因为你知道你正在使用RecyclerView,是一个回收你的项目/视图的组件,所以可能是回收一个文本视图可见的项目

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM