简体   繁体   中英

Change button on click - Android Studio

I briefly explain my problem and hope someone can help me. I have an application where in the PostActivity screen users can read other users' posts and create new ones. Under each user's post I need to put a button: "follow" if I don't find that user and "unfollow" if I follow the user; when you click on the button (for example I click on "follow") this button disappears and the "unfollow" button appears. Everything works fine, but I realized that when I scroll up the buttons change randomly without me having decided to follow or unfollow a user.. How can I solve this problem?

Post.xml

<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="5dp">

<ImageView
    android:id="@+id/author_profile"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="fitXY"
    />

<TextView
    android:id="@+id/author_name"
    android:layout_width="355dp"
    android:layout_height="35dp"
    android:textStyle="bold"
    />

<TextView
    android:id="@+id/post_comment"
    android:layout_width="355dp"
    android:layout_height="35dp"
    />

<TextView
    android:id="@+id/post_status"
    android:layout_width="355dp"
    android:layout_height="35dp"
    />


<TextView
    android:id="@+id/post_delay"
    android:layout_width="355dp"
    android:layout_height="35dp"
    android:text=""
    />

<TextView
    android:id="@+id/post_datetime"
    android:layout_width="355dp"
    android:layout_height="35dp"
    android:text=""
    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/follow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Segui"
        android:visibility="gone"
        app:backgroundTint="#57c25c" />

    <Button
        android:id="@+id/unfollow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Non seguire più"
        android:visibility="gone"
        app:backgroundTint="#DA8F8F" />


</RelativeLayout>
</LinearLayout>

PostActivity

    if (following=="false") {
        follow.setVisibility(View.VISIBLE);
    }
    if (following=="true") {
        unfollow.setVisibility(View.VISIBLE);
    }    

follow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cc.follow(response -> informTheUserAboutTheFollow(response), error -> reportErrorToUser(error), SID, p.getPostUid());
            follow.setVisibility(View.GONE);
            unfollow.setVisibility(View.VISIBLE);
            //Log.d(TAG,"seguo l'utente?"+ following);
        }
    });

    unfollow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG,"premuto");
            cc.unfollow(response -> informTheUserAboutTheUnfollow(response), error -> reportErrorToUser(error), SID, p.getPostUid());
            follow.setVisibility(View.VISIBLE);
            unfollow.setVisibility(View.GONE);
            //Log.d(TAG,"seguo l'utente?"+ following);
        }
    });

I assume that you are using adapter way to show your buttons and in that case you have to put else condition too.

For example if you want to show only follow and unfollow button to specific item than add your code as

 if (following=="false") { follow.setVisibility(View.VISIBLE); } else { follow.setVisibility(View.GONE); } if (following=="true") { unfollow.setVisibility(View.VISIBLE); } else { unfollow.setVisibility(View.GONE); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM