У меня есть RecyclerView (RV), который отображает пользователей на основе их имен. Каждый элемент в RV имеет кнопку под названием «Отправить запрос» (и некоторые TextViews). Что мне нужно сделать, так это то, что когда я нажимаю кнопку отправки запроса, кнопка должна изменить свой цвет и обновить базу данных Firebase. Я попытался изменить цвет кнопки через onBIndViewHolder, однако он постоянно меняет цвет держателя вида. Есть ли простой способ добиться этого?
Схема действий
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddFriend">
<EditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etSearch"
android:text="Search" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnSearch">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
Cardview
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dp">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="120dp"
android:background="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="30dp" />
<TextView
android:id="@+id/tvAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age" />
<Button
android:id="@+id/btnRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Send Request" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Код активности
public class AddFriend extends AppCompatActivity {
private EditText searchText;
private Button searchButton, requestBtn;
private RecyclerView recyclerView;
private UserFriendAdapter adapter;
private List<UserModelFriend> userList;
private String search;
private int button_state = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_friend);
searchButton = findViewById(R.id.btnSearch);
searchText = findViewById(R.id.etSearch);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userList = new ArrayList<>();
adapter = new UserFriendAdapter(this, userList);
recyclerView.setAdapter(adapter);
requestBtn = findViewById(R.id.btnRequest);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search = searchText.getText().toString().trim();
Query query = FirebaseDatabase.getInstance().getReference("Users")
.orderByChild("userName")
.startAt(search)
.endAt(search + "\uf8ff");
query.addListenerForSingleValueEvent(valueEventListener);
}
});
}
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userList.clear();
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
UserModelFriend user = snapshot.getValue(UserModelFriend.class);
userList.add(user);
}
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
}
RVАдаптер
public class UserFriendAdapter extends RecyclerView.Adapter<UserFriendAdapter.UserViewHolder> {
private Context mCtx;
private List<UserModelFriend> UserList;
public UserFriendAdapter(Context mCtx, List<UserModelFriend> UserList) {
this.mCtx = mCtx;
this.UserList = UserList;
}
@NonNull
@Override
public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.addfriend_recyclerview, parent, false);
return new UserViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final UserViewHolder holder, int position) {
//Basic Binding
UserModelFriend user = UserList.get(position);
holder.textViewName.setText(user.userName);
holder.textViewAge.setText("Age: " + user.userAge);
Picasso.get()
.load(user.imageUri)
.fit()
//.centerCrop()
.into(holder.userImage);
//***************************************************
}
@Override
public int getItemCount() {
return UserList.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder {
TextView textViewName, textViewAge;
ImageView userImage;
Button requestB;
public UserViewHolder(@NonNull View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.tvName);
textViewAge = itemView.findViewById(R.id.tvAge);
userImage = itemView.findViewById(R.id.img);
requestB = itemView.findViewById((R.id.btnRequest));
}
}
}