[英]Change button on click - Android Studio
我简要解释了我的问题,希望有人可以帮助我。 我有一个应用程序,用户可以在 PostActivity 屏幕中阅读其他用户的帖子并创建新帖子。 在每个用户的帖子下,我需要放置一个按钮:如果我没有找到该用户,则“关注”;如果我关注该用户,则“取消关注”; 当您单击按钮时(例如,我单击“关注”),该按钮会消失并出现“取消关注”按钮。 一切正常,但我意识到当我向上滚动按钮时随机更改而我没有决定关注或取消关注用户.. 我该如何解决这个问题?
帖子.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>
后期活动
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);
}
});
我假设您正在使用适配器方式来显示您的按钮,在这种情况下,您也必须设置 else 条件。
例如,如果您只想向特定项目显示关注和取消关注按钮,而不是将代码添加为
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.