隐藏/显示单个项目上的视图(图像或按钮)单击Recyclerview列表

时间:2017-05-24 13:03:15

标签: android android-recyclerview

我真的需要一些关于recyclerview列表的帮助。

假设有一个包含10个项目的列表,假设我点击一个项目(在位置1),我需要在其上显示视图(按钮/图像)点击特定项。但是,当我点击任何其他项目(位置5)时,视图(按钮/图片)应该在位置5处可见,而不是在位置1 < / strong>即可。谁能告诉我怎样才能达到这个结果?如果有人帮我这个,我真的很感激。如果您想了解更多详情,请发表评论。

感谢。

4 个答案:

答案 0 :(得分:0)

在recyclelerview适配器中处理onBindViewHolder(Holder,Position)方法中的点击监听器时,您必须选择视图的位置而非固定数字(例如:1,2,3)

前:

======

 @Override
public void onBindViewHolder(final YourHolder holder, final int position) {

 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          view.setVisibility(View.VISIBLE);
        }
    });

答案 1 :(得分:0)

我已经更新了mi代码,这工作正常!

您需要为ItemAdapter(在recyclerView上充气的项目)添加布尔属性。

public class YourItem{
    private String title;
    private String image;
    private boolean showView;

    public String getTitle() {
        return title;
    }
    public String getImage() {
        return image;
    }

    public void setShowView(boolean showView) {
       this.showView = showView;
    }

    public boolean mustShowView() {
       return showView;
    }

}

然后在适配器上:

public class YourAdapter extends RecyclerView.Adapter<YourAdapter.MyViewHolder> {

    ...
    private int lastPosition;
    ...
    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private ImageView image;
        private View.OnClickListener onRowClicked = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (lastPosition != getAdapterPosition()) {
                    Prize lastItem = getItem(lastPosition);
                    lastItem.setShowView(false);
                    adapterList.set(lastPosition, lastItem);
                    lastPosition = getAdapterPosition();
                }
                Prize item = getItem(getAdapterPosition());
                item.setShowView(!item.mustShowView());
                adapterList.set(getAdapterPosition(), item);
                notifyDataSetChanged();
            }
        };

        MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            title = (TextView) itemView.findViewById(R.id.prize_title);
            itemView.setOnClickListener(onRowClicked);
        }

        void bind(final Prize item) {
            title.setText(item.getTitle().toUpperCase());
            Picasso.with(context).load(item.getImage()).into(image);
            image.setVisibility(item.mustShowView() ? View.VISIBLE : View.GONE);
        }
    }
}

答案 2 :(得分:0)

我希望这段代码足以解释这样做的方法:

    public class myAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
      private List<Object> list;
      public myAdapter(List<Object> list){
         this.list=list;
      }
     ItemViewHolder openedRow=null; 
     @Override
     public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
            ItemViewHolder view = new ItemViewHolder(v);
            v.setOnClickListener(this);
            return view;
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
            final ItemViewHolder myRowHolder = (ItemViewHolder) holder;

              myRowHolder.itemView.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
   if(openedRow!=null)
   { 
      openedRow.image.setVisibility(View.GONE);
   }
        openedRow=myRowHolder;
        openedRow.image.setVisibility(View. VISIBLE);
    }
    });
    }



public static class ItemViewHolder extends RecyclerView.ViewHolder {
    ImageView image;

    public ItemViewHolder(View itemView) {
        super(itemView);
                   image = (ImageView) 
   itemView.findViewById(R.id.img_item_main);

    }
    }

    }

答案 3 :(得分:0)

我有这个问题并改变这些: 1 - 不要在xml中设置android:visibility =“visible”或“gone”的属性 2 - 在adapter()中的onBindViewHolder()函数中设置此属性:

holder.yourView.setVisibility(View.GONE);
    holder.yourBottun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!(holder.yourView.isShown() ) {
                holder.yourView.setVisibility(View.VISIBLE);
            } 
        }
    });