Android: Changing visibility of a view in recyclerView

佐手、 提交于 2019-12-06 05:07:54

问题


I have implemented a recyclerView in my project.

I have a Button in my recyclerView row. The code for my each row of recyclerView is like this:

savedmessage_custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
    android:id="@+id/message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="Dummy text" />
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:visibility="gone"/>
</LinearLayout>

The visibility of button is gone. I want to change the visibility of this button to 'visible' when someone clicks on the message textView above it. I implemented a simple onClickLiestener() on the message (textView) and changed the visibility of buttonon click of the message. I knew that wasn't going to work but I wanted to see the results. The results are weird. If I click on the textView of row 1, the button of row 7,17,19 etc also becomes visible. I can guess this might be coz of caching of the viewHolder.

MyViewHolder is something like this:

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView message;
    public MyViewHolder(final View itemView) {
        super(itemView);
        message = (TextView) itemView.findViewById(R.id.message);
        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemView.findViewById(R.id.button).setVisibility(View.VISIBLE);
            }
        });
    }
}

Can someone guide me how can I change the visibility of a button, of a perticular row only, of my recyclerView?


回答1:


Move the click logic away from the ViewHolder:

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView message;
    Button button;
    public MyViewHolder(View itemView) {
        super(itemView);
        message = (TextView) itemView.findViewById(R.id.message);
        button = (Button) itemView.findViewById(R.id.button);
    }
}

and place it inside the method onBindViewHolder of your adapter:

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.button.setVisibility(View.GONE);        
    holder.message.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holder.button.setVisibility(View.VISIBLE);
        }
    });
}

ViewHolder is reused by the RecyclerView, that's why you are seeing the button visible in other rows.




回答2:


Yes it is recycling the view so this things are happening. For that you have to create global variable

ArrayList<Boolean> isClicked = new ArrayList<>();

and save the value isClicked.

and onBindView holder put something like this:

if(isClicked.get(position)){
    view.setvisiblity(View.VISIBLE);
 }else{
    view.setvisiblity(View.GONE);
}

and on OnClickListener save value in the arraylist as well as set visiblity.




回答3:


Try this. Remove Visiblity=gone from XML. Change MyViewHOlder Constructor to;

public MyViewHolder(final View itemView) {
    super(itemView);
    message = (TextView) itemView.findViewById(R.id.message);
    button =(Button)itemView.findViewById(R.id.button);
    button.setVisibility(View.Gone);
    message.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button.setVisibility(View.VISIBLE);
        }
    });
}

Another way; Still no Visibility=Gone in XML

public void onBindViewHolder(final ViewHolder holder, final int position) {
   holder.message.setOnClickListener(new View.OnCLickListener{
       @Override
       public void onClick(View v){
          holder.button.setVisibiltiy(View.VISIBLE);
       }
   });
   holder.button.setVisibity(View.GONE);
}



回答4:


You need to manage visibility of particular button, of a particular row in @Override method onBindViewHolder rather than at initialize time.




回答5:


do like this

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView message;
Button btnShow;
public MyViewHolder(final View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    message = (TextView) itemView.findViewById(R.id.message);
    btnShow = (Button) itemView.findViewById(R.id.button);
}

 @Override
    public void onClick(View v) {
     btnShow.setVisibility(View.VISIBLE);
    }

}


来源:https://stackoverflow.com/questions/37363068/android-changing-visibility-of-a-view-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!