简体   繁体   English

单击更改按钮 - Android Studio

[英]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.我有一个应用程序,用户可以在 PostActivity 屏幕中阅读其他用户的帖子并创建新帖子。 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

<?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.我假设您正在使用适配器方式来显示您的按钮,在这种情况下,您也必须设置 else 条件。

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); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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