Android Firebase Recyclerview-如何取消选择先前选择的项目?

时间:2018-10-27 15:00:23

标签: java android firebase android-recyclerview listitem

我有一个简单的问题。我有一个带有列表项的recyclerview,我能做的是突出显示您单击的项。我的问题是我不知道如何取消选择用户单击的上一个项目,因此只能选择一个项目。

private void loadCategories() {
    adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
            Category.class,
            R.layout.categoryaddquestions_layout,
            CategoryViewHolder.class,
            categories
    ) {

        @Override
        protected void populateViewHolder(CategoryViewHolder viewHolder, final Category model, int position) {
            viewHolder.category_name.setText(model.getName());


            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    view.setSelected(false);
                }
            });
        }
    };
    adapter.notifyDataSetChanged();
    listCategory.setAdapter(adapter);
}

这是我使用的视图持有人

public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView category_name;
    public ImageView category_image;
    public LinearLayout category_layout;
    private ItemClickListener itemClickListener;
    public CategoryViewHolder(View itemView) {
        super(itemView);
        category_layout = itemView.findViewById(R.id.categoryaddquestion_layout);
        category_image = itemView.findViewById(R.id.category_image);
        category_name =  itemView.findViewById(R.id.category_name);
        itemView.setOnClickListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view,getAdapterPosition(),false);
    }

}

2 个答案:

答案 0 :(得分:0)

在适配器中创建一个名为SelectedPosition的变量。单击后,将该变量更新为adapterPosition并重新加载recyclerView。

签入private void txtSID_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { string sid = ""; int isid = 0; sid = txtSID.Text.ToString(); if (int.TryParse(sid, out isid)) ; this.CustomerTableAdapter.Fill(this.CustomerInsttDataSet.Customer, isid); this.CustomerTableAdapter.Fill(this.CustomerInsttDataSet.InstallmentPaymentHistory, isid); this.reportViewer1.RefreshReport(); this.reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.PageWidth; } } 并说:

populateViewHolder

答案 1 :(得分:0)

这是我自己的解决方案。

@Override
        protected void populateViewHolder(CategoryViewHolder viewHolder, final 
Category model, int position) {

            viewHolder.category_name.setText(model.getName());
            if (position == Common.index) {
                viewHolder.category_layout.setSelected(true);
            } else {
                viewHolder.category_layout.setSelected(false);

            }



            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {

                    Common.index = position;
                    adapter.notifyDataSetChanged();
                }
            });
        }
    };
    adapter.notifyDataSetChanged();
    listCategory.setAdapter(adapter);
}

我刚刚使用了一个普通的java类,并通过公共static int索引解决了问题