如何根据特定条件在 RecyclerView 上为某些项目隐藏 ImageView?

Posted

技术标签:

【中文标题】如何根据特定条件在 RecyclerView 上为某些项目隐藏 ImageView?【英文标题】:How can I hide an ImageView on my RecyclerView for certain items based on a particular condition? 【发布时间】:2021-05-21 21:15:03 【问题描述】:

我有一个 RecyclerView,其中一个 ImageView 是项目的一部分。如果满足特定条件,我想从 RecyclerView 中的项目中隐藏 ImageView。我怎样才能做到这一点?我附上了我希望它看起来如何的图像。

我只是在我的 xml 布局文件中定义 ImageViews,所以我不知道如何根据我的 android 活动中的特定条件实际删除它。我附上了适配器类的代码和我的活动。

这是我的适配器类的代码

适配器类

public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ReportViewHolder> 

    private ArrayList<ReportItem> reportlist;
    private OnItemClickListener mListener;
    private Context mContext;

    public ReportAdapter(ArrayList<ReportItem> reportlist, Context context) 
        this.reportlist = reportlist;
        this.mContext = context;
    

    public interface OnItemClickListener 
        void onItemClick(int position);
    

    public void setOnItemClickListener(OnItemClickListener listener) 
        mListener = listener;
    

    public static class ReportViewHolder extends RecyclerView.ViewHolder 

        public TextView departureDate;
        public TextView flightNumber;
        public View relativelayout;

        public ReportViewHolder(@NonNull View itemView, OnItemClickListener listener, Context context) 
            super(itemView);

            departureDate = itemView.findViewById(R.id.departureDaterecyclerview);
            flightNumber =  itemView.findViewById(R.id.flightnumberrecyclerview);
            relativelayout = itemView.findViewById(R.id.relativeLayoutReports);

            itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    if(listener != null) 
                        int position = getAdapterPosition();
                        if(position != RecyclerView.NO_POSITION) 
                            listener.onItemClick(position);


                        
                    

                
            );
        
    

    @NonNull
    @Override
    public ReportViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.report_listing_item, parent, false);
        ReportViewHolder rvh= new ReportViewHolder(v,mListener,mContext);
        return rvh;
    

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ReportViewHolder holder, int position) 

        ReportItem currentItem = reportlist.get(position);

        if(position%2==0)
            holder.relativelayout.setBackgroundColor(mContext.getResources().getColor(R.color.reportlistingteal));

         else 
            holder.relativelayout.setBackgroundColor(mContext.getResources().getColor(R.color.reportlistinglightteal));
        
        holder.departureDate.setText((currentItem.getDepartureDate()));
        holder.flightNumber.setText(currentItem.getFlightNumber());
    

这是我的活动文件的代码

活动文件

public class ReportListingActivity extends AppCompatActivity 

    private Button uploadAllBtn;
    private EditText searchFlights;
    private RecyclerView mRecyclerView;
    private ReportAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    ArrayList<ReportItem> reportitems = new ArrayList<>();
    private FlightViewModel flightViewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_listing);

        uploadAllBtn = findViewById(R.id.uploadAllReports);
        searchFlights = findViewById(R.id.searchFlightText);
        mRecyclerView = findViewById(R.id.recyclerView);


        flightViewModel = new ViewModelProvider(this).get(FlightViewModel.class);

        flightViewModel.getAllFlights().observe(this, new Observer<List<Flight>>() 
            @Override
            public void onChanged(List<Flight> flight_list) 

                if (flight_list.size() == 0) return;

                String flightno = flight_list.get(0).getFlightNumber();
                String flightdate = flight_list.get(0).getDate();

                String[] flight_details = new String[2];
                flight_details[0]= flightno;
                flight_details[1] = flightdate;

                Log.v("pp", flight_details[0]);

                for(int i = 0; i <flight_list.size();i++)
                    String flightnumber = flight_list.get(i).getFlightNumber();
                    String departuredate = flight_list.get(i).getDate();
                    

                    reportitems.add(new ReportItem(flightnumber,departuredate));

                    
                
                mRecyclerView.getAdapter().notifyDataSetChanged();
                flightViewModel.getAllFlights().removeObservers(ReportListingActivity.this);
            
        );

        mLayoutManager = new LinearLayoutManager(ReportListingActivity.this);
        mAdapter = new ReportAdapter(reportitems, ReportListingActivity.this);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

    

举报项目

public class ReportItem 

    private String departureDate;
    private String flightNumber;

    public ReportItem(String departureDate, String flightNumber) 
        this.departureDate = departureDate;
        this.flightNumber = flightNumber;
    

    public String getDepartureDate() 
        return departureDate;
    

    public String getFlightNumber() 
        return flightNumber;
    


【问题讨论】:

ReportItem 类中添加一个字段,该字段将告诉每个项目是否应该在onBindViewHolder() 函数中显示或隐藏ImageView。 我已经用报告项更新了代码,我在哪里添加字段? 如何在 OnBindViewHolder 方法中指定? 【参考方案1】:

为每个 RecyclerView 项目在 ReportItem 类中添加一个布尔标志。您需要指定在创建每个项目时哪些行显示或隐藏此字段:

public class ReportItem 

    private String departureDate;
    private String flightNumber;
    private Boolean showMailIcon;

    public ReportItem(String departureDate, String flightNumber, Boolean showMailIcon) 
        this.departureDate = departureDate;
        this.flightNumber = flightNumber;
        this.showMailIcon = showMailIcon
    

    public String getDepartureDate() 
        return departureDate;
    

    public String getFlightNumber() 
        return flightNumber;
    

    public String getShowMailIcon() 
        return showMailIcon;
    

然后更新 onBindViewHolder() 方法覆盖以使用此标志来显示/隐藏 ImageView:

@Override
public void onBindViewHolder(@NonNull ReportViewHolder holder, int position) 

    ReportItem currentItem = reportlist.get(position);
  
    if (currentItem.getShowMailIcon() == true) 
        holder.mailIcon.setVisibility(View.VISIBLE);
     else 
        holder.mailIcon.setVisibility(View.GONE);
    
  

    //.......

【讨论】:

以上是关于如何根据特定条件在 RecyclerView 上为某些项目隐藏 ImageView?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Studio 的 datasnapshot/recyclerview 中创建条件?

我们可以根据特定的 recyclerview 项目点击通过单个界面传递多种类型的对象(项目)吗

如何在 Elastic Beanstalk 上为特定容器设置文件夹权限

如何在 RecyclerView 项目更改上触发 MainActivity 映射修改

如何在 Android Studio 中禁用 recyclerview 特定数据?

如何在特定字段 MongoDB 上查找 Distinct No of Document 并根据条件打印文档数?