gpt4 book ai didi

java - Textview 可见性在 recyclerview 中自动创建

转载 作者:行者123 更新时间:2023-11-29 04:08:12 25 4
gpt4 key购买 nike

在我的应用程序中,我将文本可见性设置为不可见,并且我使用的是 firebase 数据库。因此,如果来 self 的 firebase 数据库的权限为真,则 TextView 将设置为可见。它正在工作,但在回收站 View 列表项中出现问题,它在列表的每第 8 项之后可见。请查看我的问题的截图

使用 log.d 我检查了我的项目是否具有 true 权限,结果显示为 true,因为我只给出了一个 true 值。

这是我截图的链接 https://ibb.co/YDpfgxF

Menuadapter.java - 我的 TextView 是“推荐的”

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“推荐”

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

最佳答案

我实际上并没有设法理解你的问题,但我认为如果你在像这样检查权限状态后添加一个 else 就可以解决

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

}

关于java - Textview 可见性在 recyclerview 中自动创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56875879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com